add method to export games and store actions/seed in database

This commit is contained in:
Maximilian Keßler 2023-03-02 22:16:25 +01:00
parent a8a53c7690
commit 9b976f6552
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -1,6 +1,7 @@
import json
from site_api import get, api, replay
from database import Game, store, load, commit
from database import Game, store, load, commit, conn
from compress import compress_deck, compress_actions, DeckCard, Action
with open('variants.json') as f:
variants = json.loads(f.read())
@ -27,7 +28,24 @@ def download_games(variant_id, name=None):
print('Downloaded and stored {} entries for variant {} ({})'.format(num_entries, variant_id, name))
commit()
def export_game(game_id) -> bool:
r = get("export/{}".format(game_id))
if r is None:
print("Failed to export game id {}".format(game_id))
return False
assert(r['id'] == game_id)
deck = compress_deck([DeckCard.from_json(card) for card in r['deck']])
with conn.cursor() as cur:
cur.execute("UPDATE seeds SET deck=(%s) WHERE seed=(%s);", (deck, r['seed']))
try:
actions = compress_actions([Action.from_json(a) for a in r['actions']])
except:
print("Unknown action while exporting game id {}".format(game_id))
return False
with conn.cursor() as cur:
cur.execute("UPDATE games SET actions=(%s) WHERE id=(%s);", (actions, game_id))
conn.commit()
return True
if __name__ == "__main__":
for var in variants:
download_games(var['id'], var['name'])
export_game(913436)