add code to analyze endgames

This commit is contained in:
Maximilian Keßler 2023-11-10 17:02:14 +01:00
parent 220f210f75
commit d1e0abca8b
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 34 additions and 5 deletions

2
bdr.py
View file

@ -25,8 +25,6 @@ def analyze_game(instance: HanabLiveInstance, actions: List[Action]) -> Tuple[Li
play = instance.deck[action.target] play = instance.deck[action.target]
if (not game.is_playable(play)) and game.is_critical(play): if (not game.is_playable(play)) and game.is_critical(play):
termination = 'Bomb crit' termination = 'Bomb crit'
print('Bombed crit {}'.format(play))
print(game.deck[game.progress:], game.stacks)
game.make_action(action) game.make_action(action)
if termination == '': if termination == '':
if game.strikes == 3: if game.strikes == 3:

View file

@ -4,10 +4,13 @@ import json
import csv import csv
import pandas import pandas
from bdr import describe_game from hanabi.database import global_db_connection_manager
from hanabi.live.site_api import get 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.read_config()
global_db_connection_manager.connect() global_db_connection_manager.connect()
@ -45,7 +48,8 @@ player_cols = set()
for _, p in player_mapping.items(): for _, p in player_mapping.items():
player_cols.add(p) player_cols.add(p)
class Entry():
class Entry:
def __init__(self, game_id, seed, score, players, bdr=0): def __init__(self, game_id, seed, score, players, bdr=0):
self.game_id = game_id self.game_id = game_id
self.seed = seed self.seed = seed
@ -60,6 +64,7 @@ def get_player_games(player: str):
if r.status_code == 200: if r.status_code == 200:
return json.loads(r.text) return json.loads(r.text)
def collect_player_games(): def collect_player_games():
global_games = {} global_games = {}
for player in player_mapping.keys(): for player in player_mapping.keys():
@ -89,6 +94,7 @@ def collect_player_games():
return global_games return global_games
def analyze_games(games): def analyze_games(games):
retval = {} retval = {}
for game_id in games.keys(): for game_id in games.keys():
@ -97,6 +103,15 @@ def analyze_games(games):
retval[game_id] = bdrs, termination retval[game_id] = bdrs, termination
return retval 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): def sort_players_by_num_games(games_dict):
nums = {} nums = {}
@ -108,9 +123,11 @@ def sort_players_by_num_games(games_dict):
nums[col] = num + 1 nums[col] = num + 1
return sorted(player_cols, key = lambda col: -nums[col]) return sorted(player_cols, key = lambda col: -nums[col])
if __name__ == "__main__": if __name__ == "__main__":
games = collect_player_games() games = collect_player_games()
analysis = analyze_games(games) analysis = analyze_games(games)
endgames = analyze_endgames(games)
streaks = {} streaks = {}
fieldnames = ['Game ID', 'Seed', 'Player #', 'Result', 'BDR'] fieldnames = ['Game ID', 'Seed', 'Player #', 'Result', 'BDR']
fieldnames += sort_players_by_num_games(games) fieldnames += sort_players_by_num_games(games)
@ -145,5 +162,19 @@ if __name__ == "__main__":
num_others = row.get('Other', 0) num_others = row.get('Other', 0)
row['Other'] = num_others + 1 row['Other'] = num_others + 1
writer.writerow(row) 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'] = "<a href='https://hanab.live/replay/{}'>{}</a>".format(game_id, game_id)
writer.writerow(endgame)
a = pandas.read_csv("games.csv") a = pandas.read_csv("games.csv")
a.to_html("games.html", escape=False) a.to_html("games.html", escape=False)
b = pandas.read_csv('endgames.csv')
b.to_html("endgames.html", escape=False)