improved parsing of endgame data

This commit is contained in:
Maximilian Keßler 2023-11-12 00:01:22 +01:00
parent eb0699e775
commit ad7fbcc364
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 36 additions and 16 deletions

View file

@ -16,8 +16,6 @@ with open(DATA_FILE, 'r') as f:
def analyze_game(game_id: int): def analyze_game(game_id: int):
max_draw_pile_size = 15 max_draw_pile_size = 15
probabilities = {}
raw_output = None
try: 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) 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 raw_output = result.stdout
@ -27,13 +25,17 @@ def analyze_game(game_id: int):
print(output) 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) # 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): for m in re.finditer('Probability with ([0-9]+) cards left in deck: .*/.* ~ ([0-9.]+)', output):
probabilities[str(m.group(1))] = m.group(2) 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): def full_analyze_game(game_id: int):
probabilities = {} max_draw_pile_size = 10
try: try:
result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(10), '-i', '0', '--all-clues', '-r', '--quiet'], stdout=subprocess.PIPE, timeout=180) 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)
except subprocess.TimeoutExpired: raw_output = result.stdout
return probabilities except subprocess.TimeoutExpired as time_err:
output = result.stdout.decode('utf-8') raw_output = time_err.stdout
output = raw_output.decode('utf-8')
print(output) 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): 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(): if m.group(1) not in probabilities.keys():
probabilities[m.group(1)] = {} probabilities[m.group(1)] = {}

View file

@ -14,7 +14,7 @@ from endgames import analyze_game_cached, full_analyze_game_cached
global_db_connection_manager.read_config() global_db_connection_manager.read_config()
global_db_connection_manager.connect() 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 = { player_mapping = {
'RamaNoVarjan': 'Ramanujan', 'RamaNoVarjan': 'Ramanujan',
@ -133,7 +133,8 @@ def sort_players_by_num_games(games_dict):
return sorted(player_cols, key = lambda col: -nums[col]) 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: if clue_modifier > 0:
for lookup in range(clue_modifier, 0, -1): for lookup in range(clue_modifier, 0, -1):
val = endgame_dict.get('+' + str(lookup), None) val = endgame_dict.get('+' + str(lookup), None)
@ -144,7 +145,8 @@ def lookup_val(endgame_dict, clue_modifier):
val = endgame_dict.get(str(lookup)) val = endgame_dict.get(str(lookup))
if val is not None: if val is not None:
return val return val
return endgame_dict.get('+0', None) retval = endgame_dict.get('+0', None)
return retval
if __name__ == "__main__": if __name__ == "__main__":
@ -197,6 +199,7 @@ if __name__ == "__main__":
writer.writerow(endgame) writer.writerow(endgame)
all_endgames = full_analyze_endgames(games) all_endgames = full_analyze_endgames(games)
print(all_endgames.keys())
fieldnames = ['Game ID'] + [str(i) for i in range(1, 11)] fieldnames = ['Game ID'] + [str(i) for i in range(1, 11)]
for clue_modifier in range(-2, 3): for clue_modifier in range(-2, 3):
filename = 'endgames{}.csv'.format(clue_modifier) filename = 'endgames{}.csv'.format(clue_modifier)
@ -205,9 +208,11 @@ if __name__ == "__main__":
with open(filename, 'a') as f: with open(filename, 'a') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames) writer = csv.DictWriter(f, fieldnames=fieldnames)
for game_id, endgame in sorted(all_endgames.items()): for game_id, endgame in sorted(all_endgames.items()):
print(endgame)
row = {'Game ID': game_id} row = {'Game ID': game_id}
for deck_size in range(1, 11): for deck_size in range(1, 11):
val = lookup_val(endgame.get(str(deck_size), {}), clue_modifier) val = lookup_val(endgame.get(str(deck_size), {}), clue_modifier)
print('looked up val {}'.format(val))
if val is not None: if val is not None:
row[str(deck_size)] = val row[str(deck_size)] = val
else: else: