improve game sites

This commit is contained in:
Maximilian Keßler 2024-01-14 13:38:18 +01:00
parent 96c6fc0df2
commit 145142c4a9
Signed by: max
GPG Key ID: BCC5A619923C0BA5
2 changed files with 39 additions and 12 deletions

View File

@ -12,12 +12,14 @@ import requests_cache
import platformdirs
import stats
import hanabi.hanab_game
import constants
import config
import utils
from dataclasses import dataclass
import endgames
import games_db_interface
from database import conn_manager
@ -747,21 +749,36 @@ class EndgameActionRow:
description: str
enumerator: int
denominator: int
marked: bool = False
@property
def win_rate(self):
return round(100 * self.enumerator / self.denominator, 3)
def convert_endgame_action(endgame_action: endgames.EndgameAction) -> EndgameActionRow:
def convert_endgame_action(endgame_action: endgames.EndgameAction, game: hanabi.hanab_game.GameState, action: hanabi.hanab_game.Action) -> EndgameActionRow:
action_str = endgames.print_action_type(endgame_action.action_type)
target_str: str
if action_str != "Clue":
if endgame_action.action_type not in [hanabi.hanab_game.ActionType.ColorClue, hanabi.hanab_game.ActionType.RankClue]:
target_str = " {}".format(endgame_action.card)
else:
target_str = ""
description = action_str + target_str
return EndgameActionRow(description, endgame_action.enumerator, endgame_action.denominator)
marked = False
# To simplify comparisons, we only work with color clues here. Endgame actions always consist of color clues.
if action.type == hanabi.hanab_game.ActionType.RankClue:
action.type = hanabi.hanab_game.ActionType.ColorClue
if endgame_action.action_type == action.type:
if endgame_action.action_type == hanabi.hanab_game.ActionType.ColorClue:
marked = True
else:
game_target = game.instance.deck[action.target]
if game.is_trash(game_target):
game_target = hanabi.hanab_game.DeckCard(0, 0)
if endgame_action.card == game_target:
marked = True
return EndgameActionRow(description, endgame_action.enumerator, endgame_action.denominator, marked)
def get_endgame_page_data():
@ -776,15 +793,25 @@ def get_endgame_page_data():
ret = {}
for (game_id, ) in cur.fetchall():
ret[game_id] = []
actions = endgames.load_endgame_actions(game_id)
while len(actions) > 0:
cur_turn = actions[0].turn
actions_this_turn: List[endgames.EndgameAction] = []
while len(actions) > 0 and actions[0].turn == cur_turn:
instance, actions, _ = games_db_interface.load_game_parts(game_id)
game = hanabi.hanab_game.GameState(instance)
endgame_actions = endgames.load_endgame_actions(game_id)
while len(endgame_actions) > 0:
# Move to current turn and update game
cur_turn = endgame_actions[0].turn
# Note the -1 here since turns on hanab.live start to count at 1
while len(game.actions) < cur_turn - 1:
action, *actions = actions
game.make_action(action)
assert len(actions) > 0
actions_this_turn: List[endgames.EndgameAction] = []
while len(endgame_actions) > 0 and endgame_actions[0].turn == cur_turn:
action, *endgame_actions = endgame_actions
actions_this_turn.append(action)
actions_this_turn.sort(key=lambda a: -a.win_rate)
best_action, *other_actions = [convert_endgame_action(a) for a in actions_this_turn]
best_action, *other_actions = [convert_endgame_action(a, game, actions[0]) for a in actions_this_turn]
ret[game_id].append(
(cur_turn, best_action, other_actions)
)

View File

@ -45,14 +45,14 @@
</tr>
{% for (turn, best_action, other_actions) in data %}
<tr>
<td rowspan="{{ other_actions|length + 1 }}"><a href="https://hanab.live/replay/{{game_id}}#{{turn}}">{{ turn }}</td>
<td>{{ best_action.description }}</td>
<td rowspan="{{ other_actions|length + 1 }}"><a href="https://hanab.live/replay/{{game_id}}#{{turn}}">{{ turn }}</a></td>
<td>{% if best_action.marked %}<b>{% endif %}{{ best_action.description }}{% if best_action.marked %}</b>{% endif %}</td>
<td>{{ best_action.enumerator }}/{{ best_action.denominator }}</td>
<td>{{ best_action.win_rate }}%</td>
</tr>
{% for action in other_actions %}
<tr>
<td>{{ action.description }}</td>
<td>{% if action.marked %}<b>{% endif %}{{ action.description }}{% if action.marked %}</b>{% endif %}</td>
<td>{{ action.enumerator }}/{{ action.denominator }}</td>
<td>{{ action.win_rate }}%</td>
</tr>