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 platformdirs
import stats import stats
import hanabi.hanab_game
import constants import constants
import config import config
import utils import utils
from dataclasses import dataclass from dataclasses import dataclass
import endgames import endgames
import games_db_interface
from database import conn_manager from database import conn_manager
@ -747,21 +749,36 @@ class EndgameActionRow:
description: str description: str
enumerator: int enumerator: int
denominator: int denominator: int
marked: bool = False
@property @property
def win_rate(self): def win_rate(self):
return round(100 * self.enumerator / self.denominator, 3) 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) action_str = endgames.print_action_type(endgame_action.action_type)
target_str: str 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) target_str = " {}".format(endgame_action.card)
else: else:
target_str = "" target_str = ""
description = action_str + 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(): def get_endgame_page_data():
@ -776,15 +793,25 @@ def get_endgame_page_data():
ret = {} ret = {}
for (game_id, ) in cur.fetchall(): for (game_id, ) in cur.fetchall():
ret[game_id] = [] ret[game_id] = []
actions = endgames.load_endgame_actions(game_id) instance, actions, _ = games_db_interface.load_game_parts(game_id)
while len(actions) > 0: game = hanabi.hanab_game.GameState(instance)
cur_turn = actions[0].turn
actions_this_turn: List[endgames.EndgameAction] = [] endgame_actions = endgames.load_endgame_actions(game_id)
while len(actions) > 0 and actions[0].turn == cur_turn: 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 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.append(action)
actions_this_turn.sort(key=lambda a: -a.win_rate) 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( ret[game_id].append(
(cur_turn, best_action, other_actions) (cur_turn, best_action, other_actions)
) )

View file

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