import json import re import subprocess from typing import Dict from pathlib import Path DATA_FILE = Path('endgame-data.json') if not DATA_FILE.exists(): DATA_FILE.write_text('{}') with open(DATA_FILE, 'r') as f: DATA: Dict = json.loads(f.read()) def analyze_game(game_id: int): max_draw_pile_size = 15 try: result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(max_draw_pile_size), '--interactive', '0', '--quiet', '-r'], stdout=subprocess.PIPE, timeout=60*15) raw_output = result.stdout except subprocess.TimeoutExpired as time_err: raw_output = time_err.stdout output = raw_output.decode('utf-8') print(output) # Now, parse all results that we obtained (unclear how many depending on whether we ran into the timeout) 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) return probabilities def full_analyze_game(game_id: int): max_draw_pile_size = 10 try: 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)] = {} probabilities[m.group(1)][m.group(2)] = m.group(3) return probabilities def full_analyze_game_cached(game_id: int): cached = DATA.get('all', {}).get(str(game_id), None) if cached is not None: return cached result = full_analyze_game(game_id) if 'all' not in DATA.keys(): DATA['all'] = {} DATA['all'][game_id] = result save_cache() return result def analyze_game_cached(game_id: int): cached = DATA.get('normal', {}).get(str(game_id), None) if cached is not None: return cached result = analyze_game(game_id) if 'normal' not in DATA.keys(): DATA['normal'] = {} DATA['normal'][game_id] = result save_cache() return result def save_cache(): with open(DATA_FILE, 'w') as f: f.writelines(json.dumps(DATA, indent=2))