adjust download method also for games whose seed is unknown before

This commit is contained in:
Maximilian Keßler 2023-05-06 17:42:27 +02:00
parent 3a00879e03
commit 71951ac929
Signed by: max
GPG Key ID: BCC5A619923C0BA5

View File

@ -2,6 +2,8 @@ import json
from site_api import get, api, replay from site_api import get, api, replay
from database import Game, store, load, commit, conn from database import Game, store, load, commit, conn
from compress import compress_deck, compress_actions, DeckCard, Action from compress import compress_deck, compress_actions, DeckCard, Action
from variants import variant_id
from hanabi import HanabiInstance, GameState, Action
with open('variants.json') as f: with open('variants.json') as f:
variants = json.loads(f.read()) variants = json.loads(f.read())
@ -31,37 +33,66 @@ def download_games(variant_id, name=None):
# requires seed AND game to already have an entry in database # requires seed AND game to already have an entry in database
# return: (successfully exported game, game without cheat options, null if not exported) # return: (successfully exported game, game without cheat options, null if not exported)
def export_game(game_id) -> [bool, bool]: def export_game(game_id) -> [bool, bool]:
r = get("export/{}".format(game_id))
if r is None:
print("Failed to export game id {}".format(game_id))
return False, None
assert(r['id'] == game_id)
deck = compress_deck([DeckCard.from_json(card) for card in r['deck']])
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute("UPDATE seeds SET deck=(%s) WHERE seed=(%s);", (deck, r['seed'])) cur.execute("SELECT deck_plays, one_extra_card, one_less_card, all_or_nothing, actions FROM games WHERE id = (%s)", (game_id,))
try: res = cur.fetchall()
actions = compress_actions([Action.from_json(a) for a in r['actions']], r['id']) if len(res) == 1:
except: print(res)
print("Unknown action while exporting game id {}".format(game_id)) else:
raise print('game is completely new')
return False, None # return
options = r.get('options', {})
deck_plays = options.get('deckPlays', False) r = get("export/{}".format(game_id))
one_extra_card = options.get('oneExtraCard', False) if r is None:
one_less_card = options.get('oneLessCard', False) print("Failed to export game id {}".format(game_id))
all_or_nothing = options.get('allOrNothing', False) return False, None
with conn.cursor() as cur: assert(r['id'] == game_id)
cur.execute( # print(r)
"UPDATE games SET "
"deck_plays = (%s)," try:
"one_extra_card = (%s)," num_players = len(r['players'])
"one_less_card = (%s)," seed = r['seed']
"all_or_nothing = (%s)," options = r.get('options', {})
"actions = (%s) " var_id = variant_id(options['variant'])
"WHERE id = (%s);", deck_plays = options.get('deckPlays', False)
(deck_plays, one_extra_card, one_less_card, all_or_nothing, actions, game_id)) one_extra_card = options.get('oneExtraCard', False)
one_less_card = options.get('oneLessCard', False)
all_or_nothing = options.get('allOrNothing', False)
actions = [Action.from_json(action) for action in r['actions']]
deck = [DeckCard.from_json(card) for card in r['deck']]
except KeyError:
print('Error parsing JSON when exporting game {}'.format(game_id))
# need to play through the game once to find out its score
game = GameState(HanabiInstance(deck, num_players))
for action in actions:
game.make_action(action)
try:
compressed_deck = compress_deck(deck)
except:
print("Failed to compress deck while exporting game {}".format(game_id))
raise
try:
compressed_actions = compress_actions(actions)
except:
print("Failed to compress actions while exporting game {}".format(game_id))
raise
with conn.cursor() as cur:
# cur.execute("UPDATE seeds SET deck=(%s) WHERE seed=(%s);", (deck, seed))
cur.execute(
"INSERT INTO games (id, num_players, score, seed, variant_id, deck_plays, one_extra_card, one_less_card, all_or_nothing, actions)"
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(game_id, num_players, game.score, seed, var_id, deck_plays, one_extra_card, one_less_card, all_or_nothing, compressed_actions))
cur.execute(
"INSERT INTO seeds (seed, num_players, variant_id, deck)"
"VALUES (%s, %s, %s, %s)"
"ON CONFLICT (seed) DO NOTHING",
(seed, num_players, var_id, compressed_deck)
)
conn.commit() conn.commit()
return True, not any([deck_plays, one_extra_card, one_less_card, all_or_nothing]) return True, not any([deck_plays, one_extra_card, one_less_card, all_or_nothing])
if __name__ == "__main__": if __name__ == "__main__":
export_game(913436) export_game(960753)