streak-hunting-spreadsheet/games.py

28 lines
768 B
Python
Raw Normal View History

2023-11-20 13:05:20 +01:00
import json
2023-11-20 16:45:43 +01:00
from typing import Dict, Optional
2023-11-20 13:05:20 +01:00
from pathlib import Path
from hanabi.live.site_api import get
GAMES_PATH = Path('games')
if not GAMES_PATH.exists():
GAMES_PATH.mkdir(parents=True)
2023-11-20 16:45:43 +01:00
def get_game_json(game_id: int, strategy: Optional[str] = None) -> Dict:
filename = GAMES_PATH / (str(game_id) + ('-{}'.format(strategy) if strategy is not None else ''))
2023-11-20 13:05:20 +01:00
if filename.exists():
with open(filename, 'r') as f:
return json.load(f)
2023-11-20 16:45:43 +01:00
if strategy is not None:
print('Failed to load replay of {} strategy version of game with id {}'.format(strategy, game_id))
return {}
2023-11-20 13:05:20 +01:00
game = get("export/" + str(game_id))
with open(filename, 'w') as f:
f.write(json.dumps(game, indent=2))
return game