From 145142c4a9a812c49f54e81ac58a3f87d57cdcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 14 Jan 2024 13:38:18 +0100 Subject: [PATCH] improve game sites --- src/render_site.py | 45 ++++++++++++++++++++++++++++++++++++--------- templates/game.html | 6 +++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/render_site.py b/src/render_site.py index ee57017..46be44a 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -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) ) diff --git a/templates/game.html b/templates/game.html index e92a3d3..38a34e2 100644 --- a/templates/game.html +++ b/templates/game.html @@ -45,14 +45,14 @@ {% for (turn, best_action, other_actions) in data %} - {{ turn }} - {{ best_action.description }} + {{ turn }} + {% if best_action.marked %}{% endif %}{{ best_action.description }}{% if best_action.marked %}{% endif %} {{ best_action.enumerator }}/{{ best_action.denominator }} {{ best_action.win_rate }}% {% for action in other_actions %} - {{ action.description }} + {% if action.marked %}{% endif %}{{ action.description }}{% if action.marked %}{% endif %} {{ action.enumerator }}/{{ action.denominator }} {{ action.win_rate }}%