From ad7fbcc364005f605f75fbb1149a0df71b0bc4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 12 Nov 2023 00:01:22 +0100 Subject: [PATCH] improved parsing of endgame data --- endgames.py | 41 ++++++++++++++++++++++++++++------------- get_sheet.py | 11 ++++++++--- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/endgames.py b/endgames.py index f667517..25748f5 100644 --- a/endgames.py +++ b/endgames.py @@ -16,8 +16,6 @@ with open(DATA_FILE, 'r') as f: def analyze_game(game_id: int): max_draw_pile_size = 15 - probabilities = {} - raw_output = None try: result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(max_draw_pile_size), '--interactive', '0', '--quiet', '-r'], stdout=subprocess.PIPE, timeout=10) raw_output = result.stdout @@ -27,13 +25,17 @@ def analyze_game(game_id: int): print(output) - # Check if the game was just over before reaching the specified draw pile size - if re.search(r'The given draw pile size \({}\) cannot be obtained with the specified replay.'.format(max_draw_pile_size), output): - print('detected empty output') - return {str(size): 0 for size in range(1, max_draw_pile_size + 1)} - # Now, parse all results that we obtained (unclear how many depending on whether we ran into the timeout) - x = 'Probability with 13 cards left in deck: 5855/6237 ~ 93.875%' + probabilities = {} + + # Check if the game was just over before reaching the specified draw pile size + for m in re.finditer(r'The given draw pile size \(([0-9]+)\) cannot be obtained with the specified replay.', output): + if m.group(1) == str(max_draw_pile_size): + print('detected empty output') + return {str(size): 0 for size in range(1, max_draw_pile_size + 1)} + else: + probabilities[str(m.group(1))] = 0 + for m in re.finditer('Probability with ([0-9]+) cards left in deck: .*/.* ~ ([0-9.]+)', output): probabilities[str(m.group(1))] = m.group(2) @@ -41,13 +43,26 @@ def analyze_game(game_id: int): def full_analyze_game(game_id: int): - probabilities = {} + max_draw_pile_size = 10 try: - result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(10), '-i', '0', '--all-clues', '-r', '--quiet'], stdout=subprocess.PIPE, timeout=180) - except subprocess.TimeoutExpired: - return probabilities - output = result.stdout.decode('utf-8') + result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(max_draw_pile_size), '-i', '0', '--all-clues', '-r', '--quiet'], stdout=subprocess.PIPE, timeout=180) + raw_output = result.stdout + except subprocess.TimeoutExpired as time_err: + raw_output = time_err.stdout + output = raw_output.decode('utf-8') + print(output) + + probabilities = {} + zero_dict = { + (('+' if clue_modifier >= 0 else '') + str(clue_modifier)): 0 for clue_modifier in range(-8, 9) + } + for m in re.finditer(r'The given draw pile size \(([0-9]+)\) cannot be obtained with the specified replay.', output): + if m.group(1) == str(max_draw_pile_size): + return {str(size): zero_dict for size in range(1, max_draw_pile_size + 1)} + else: + probabilities[str(m.group(1))] = zero_dict + for m in re.finditer('Probability with ([0-9]+) cards left in deck and [0-8] clues \((.[0-8])\).*: .*/.* ~ ([0-9.]*)', output): if m.group(1) not in probabilities.keys(): probabilities[m.group(1)] = {} diff --git a/get_sheet.py b/get_sheet.py index ca95634..6c51136 100644 --- a/get_sheet.py +++ b/get_sheet.py @@ -14,7 +14,7 @@ from endgames import analyze_game_cached, full_analyze_game_cached global_db_connection_manager.read_config() global_db_connection_manager.connect() -session = requests_cache.CachedSession('.hanab-live.cache',expire_after=300) +session = requests_cache.CachedSession('.hanab-live.cache',expire_after=30000) player_mapping = { 'RamaNoVarjan': 'Ramanujan', @@ -133,7 +133,8 @@ def sort_players_by_num_games(games_dict): return sorted(player_cols, key = lambda col: -nums[col]) -def lookup_val(endgame_dict, clue_modifier): +def lookup_val(endgame_dict, clue_modifier) -> str: + print('looking up val {} in {}'.format(clue_modifier, endgame_dict)) if clue_modifier > 0: for lookup in range(clue_modifier, 0, -1): val = endgame_dict.get('+' + str(lookup), None) @@ -144,7 +145,8 @@ def lookup_val(endgame_dict, clue_modifier): val = endgame_dict.get(str(lookup)) if val is not None: return val - return endgame_dict.get('+0', None) + retval = endgame_dict.get('+0', None) + return retval if __name__ == "__main__": @@ -197,6 +199,7 @@ if __name__ == "__main__": writer.writerow(endgame) all_endgames = full_analyze_endgames(games) + print(all_endgames.keys()) fieldnames = ['Game ID'] + [str(i) for i in range(1, 11)] for clue_modifier in range(-2, 3): filename = 'endgames{}.csv'.format(clue_modifier) @@ -205,9 +208,11 @@ if __name__ == "__main__": with open(filename, 'a') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) for game_id, endgame in sorted(all_endgames.items()): + print(endgame) row = {'Game ID': game_id} for deck_size in range(1, 11): val = lookup_val(endgame.get(str(deck_size), {}), clue_modifier) + print('looked up val {}'.format(val)) if val is not None: row[str(deck_size)] = val else: