add option to analyse cheat games. read games from disk

This commit is contained in:
Maximilian Keßler 2023-11-20 13:26:40 +01:00
parent 9028c7401a
commit 6e960e4425
Signed by: max
GPG Key ID: BCC5A619923C0BA5
2 changed files with 19 additions and 14 deletions

View File

@ -3,6 +3,8 @@ import re
import subprocess
from typing import Dict
from games import GAMES_PATH
from pathlib import Path
DATA_FILE = Path('endgame-data.json')
@ -14,10 +16,10 @@ with open(DATA_FILE, 'r') as f:
DATA: Dict = json.loads(f.read())
def analyze_game(game_id: int):
def analyze_game(filename: str):
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)
result = subprocess.run(['./endgame-analyzer', '-f', filename, '-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
@ -38,10 +40,10 @@ def analyze_game(game_id: int):
return probabilities
def full_analyze_game(game_id: int):
def full_analyze_game(filename: str):
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)
result = subprocess.run(['./endgame-analyzer', '-f', filename, '-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
@ -68,25 +70,27 @@ def full_analyze_game(game_id: int):
return probabilities
def full_analyze_game_cached(game_id: int):
def full_analyze_game_cached(game_id: int, cheat: bool = False):
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
result = full_analyze_game(str(GAMES_PATH / str(game_id)) + '-cheat' if cheat else '')
key = 'all' if not cheat else 'all-cheat'
if key not in DATA.keys():
DATA[key] = {}
DATA[key][game_id] = result
save_cache()
return result
def analyze_game_cached(game_id: int):
def analyze_game_cached(game_id: int, cheat: bool = False):
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
result = analyze_game(str(GAMES_PATH / str(game_id)) + '-cheat' if cheat else '')
key = 'normal' if not cheat else 'normal-cheat'
if key not in DATA.keys():
DATA[key] = {}
DATA[key][game_id] = result
save_cache()
return result

View File

@ -10,6 +10,7 @@ from hanabi.live.site_api import get
from bdr import describe_game
from endgames import analyze_game_cached, full_analyze_game_cached
from games import get_game_json
from bots import run_cheating_strategy
# Init db connection
global_db_connection_manager.read_config()