add method to export games and store actions/seed in database
This commit is contained in:
parent
a8a53c7690
commit
9b976f6552
1 changed files with 21 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
from site_api import get, api, replay
|
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:
|
with open('variants.json') as f:
|
||||||
variants = json.loads(f.read())
|
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))
|
print('Downloaded and stored {} entries for variant {} ({})'.format(num_entries, variant_id, name))
|
||||||
commit()
|
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__":
|
if __name__ == "__main__":
|
||||||
for var in variants:
|
export_game(913436)
|
||||||
download_games(var['id'], var['name'])
|
|
||||||
|
|
Loading…
Reference in a new issue