106 lines
3.9 KiB
Python
106 lines
3.9 KiB
Python
import json
|
|
import re
|
|
import subprocess
|
|
from typing import Dict, Optional
|
|
|
|
from games import GAMES_PATH
|
|
from bots import ensure_bot_game_exists
|
|
|
|
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(filename: str):
|
|
max_draw_pile_size = 15
|
|
try:
|
|
result = subprocess.run(['./endgame-analyzer', '-f', filename, '-d', str(max_draw_pile_size), '--interactive', '0', '--quiet', '-r'], stdout=subprocess.PIPE, timeout=30)
|
|
raw_output = result.stdout
|
|
except subprocess.TimeoutExpired as time_err:
|
|
raw_output = time_err.stdout
|
|
output = raw_output.decode('utf-8')
|
|
|
|
# Now, parse all results that we obtained (unclear how many depending on whether we ran into the timeout)
|
|
probabilities = {}
|
|
|
|
m = re.search(r"Specified draw pile size of ([0-9]+) cannot be reached with specified replay\.\nReplay ends at turn [0-9]+ with score of ([0-9]+)\.", output, re.M)
|
|
if m:
|
|
won = '100' if m.group(2) == '25' else 0
|
|
for draw_pile_size in range(1, int(m.group(1)) + 1):
|
|
probabilities[str(draw_pile_size)] = won
|
|
|
|
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(filename: str):
|
|
max_draw_pile_size = 10
|
|
try:
|
|
result = subprocess.run(['./endgame-analyzer', '-f', filename, '-d', str(max_draw_pile_size), '-i', '0', '--all-clues', '-r', '--quiet'], stdout=subprocess.PIPE, timeout=30)
|
|
raw_output = result.stdout
|
|
except subprocess.TimeoutExpired as time_err:
|
|
raw_output = time_err.stdout
|
|
output = raw_output.decode('utf-8')
|
|
|
|
probabilities = {}
|
|
zero_dict = {
|
|
(('+' if clue_modifier >= 0 else '') + str(clue_modifier)): 0 for clue_modifier in range(-8, 9)
|
|
}
|
|
hundred_dict = {
|
|
(('+' if clue_modifier >= 0 else '') + str(clue_modifier)): 100 for clue_modifier in range(-8, 9)
|
|
}
|
|
|
|
m = re.search(r"Specified draw pile size of ([0-9]+) cannot be reached with specified replay\.\nReplay ends at turn [0-9]+ with score of ([0-9]+)\.", output, re.M)
|
|
if m:
|
|
won = hundred_dict if m.group(2) == '25' else zero_dict
|
|
for draw_pile_size in range(1, int(m.group(1)) + 1):
|
|
probabilities[str(draw_pile_size)] = won
|
|
|
|
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, strategy: Optional[str] = None):
|
|
key = 'all' if strategy is None else 'all-{}'.format(strategy)
|
|
if strategy is not None:
|
|
ensure_bot_game_exists(game_id, strategy)
|
|
cached = DATA.get(key, {}).get(str(game_id), None)
|
|
if cached is not None:
|
|
return cached
|
|
result = full_analyze_game(str(GAMES_PATH / str(game_id)) + ('-{}'.format(strategy) if strategy is not None else ''))
|
|
if key not in DATA.keys():
|
|
DATA[key] = {}
|
|
DATA[key][game_id] = result
|
|
save_cache()
|
|
return result
|
|
|
|
|
|
def analyze_game_cached(game_id: int, strategy: Optional[str] = None):
|
|
key = 'normal' if strategy is None else 'normal-{}'.format(strategy)
|
|
if strategy is not None:
|
|
ensure_bot_game_exists(game_id, strategy)
|
|
cached = DATA.get(key, {}).get(str(game_id), None)
|
|
if cached is not None:
|
|
return cached
|
|
result = analyze_game(str(GAMES_PATH / str(game_id)) + ('-{}'.format(strategy) if strategy is not None else ''))
|
|
if key not in DATA.keys():
|
|
DATA[key] = {}
|
|
DATA[key][game_id] = result
|
|
save_cache()
|
|
return result
|
|
|
|
|
|
def save_cache():
|
|
with open(DATA_FILE, 'w') as f:
|
|
f.writelines(json.dumps(DATA, indent=2))
|