From d1e0abca8b9948f63ea5a8f63ccdf5197a2ff6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 10 Nov 2023 17:02:14 +0100 Subject: [PATCH] add code to analyze endgames --- bdr.py | 2 -- get_sheet.py | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/bdr.py b/bdr.py index 1e8e9bc..0cafdad 100644 --- a/bdr.py +++ b/bdr.py @@ -25,8 +25,6 @@ def analyze_game(instance: HanabLiveInstance, actions: List[Action]) -> Tuple[Li play = instance.deck[action.target] if (not game.is_playable(play)) and game.is_critical(play): termination = 'Bomb crit' - print('Bombed crit {}'.format(play)) - print(game.deck[game.progress:], game.stacks) game.make_action(action) if termination == '': if game.strikes == 3: diff --git a/get_sheet.py b/get_sheet.py index 4674427..14978d1 100644 --- a/get_sheet.py +++ b/get_sheet.py @@ -4,10 +4,13 @@ import json import csv import pandas -from bdr import describe_game +from hanabi.database import global_db_connection_manager from hanabi.live.site_api import get -from hanabi.database import global_db_connection_manager +from bdr import describe_game +from endgames import analyze_game_cached + +# Init db connection global_db_connection_manager.read_config() global_db_connection_manager.connect() @@ -45,7 +48,8 @@ player_cols = set() for _, p in player_mapping.items(): player_cols.add(p) -class Entry(): + +class Entry: def __init__(self, game_id, seed, score, players, bdr=0): self.game_id = game_id self.seed = seed @@ -60,6 +64,7 @@ def get_player_games(player: str): if r.status_code == 200: return json.loads(r.text) + def collect_player_games(): global_games = {} for player in player_mapping.keys(): @@ -89,6 +94,7 @@ def collect_player_games(): return global_games + def analyze_games(games): retval = {} for game_id in games.keys(): @@ -97,6 +103,15 @@ def analyze_games(games): retval[game_id] = bdrs, termination return retval +def analyze_endgames(games): + retval = {} + for game_id in games.keys(): + print('Analysing endgames of game {}'.format(game_id)) + result = analyze_game_cached(game_id) + retval[game_id] = result + print(result) + return retval + def sort_players_by_num_games(games_dict): nums = {} @@ -108,9 +123,11 @@ def sort_players_by_num_games(games_dict): nums[col] = num + 1 return sorted(player_cols, key = lambda col: -nums[col]) + if __name__ == "__main__": games = collect_player_games() analysis = analyze_games(games) + endgames = analyze_endgames(games) streaks = {} fieldnames = ['Game ID', 'Seed', 'Player #', 'Result', 'BDR'] fieldnames += sort_players_by_num_games(games) @@ -145,5 +162,19 @@ if __name__ == "__main__": num_others = row.get('Other', 0) row['Other'] = num_others + 1 writer.writerow(row) + + fieldnames = ['Game ID'] + [str(i) for i in range(1, 16)] + with open('endgames.csv', 'w', newline='') as f: + f.writelines([','.join(fieldnames), "\n"]) + + with open('endgames.csv', 'a', newline='') as f: + writer = csv.DictWriter(f, fieldnames=fieldnames) + for game_id, endgame in sorted(endgames.items()): + endgame['Game ID'] = "{}".format(game_id, game_id) + writer.writerow(endgame) + a = pandas.read_csv("games.csv") a.to_html("games.html", escape=False) + + b = pandas.read_csv('endgames.csv') + b.to_html("endgames.html", escape=False)