From 072d450fe636996d8c0e5460749a7605800672c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 23 Nov 2023 14:00:25 +0100 Subject: [PATCH] Add retrieval methods for actions and decks --- fetch_games.py | 4 ++++ games_db_interface.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/fetch_games.py b/fetch_games.py index e11b342..c46b53a 100644 --- a/fetch_games.py +++ b/fetch_games.py @@ -269,6 +269,10 @@ def detailed_fetch_game(game_id: int) -> bool: games_db_interface.store_deck_for_seed(seed, instance.deck) games_db_interface.store_actions(game_id, actions) logger.debug("Fetched all game details of game {}.".format(game_id)) + + # Do some sanity checks that loading the stored data did not change it + assert actions == games_db_interface.load_actions(game_id) + assert instance.deck == games_db_interface.load_deck(seed) return True diff --git a/games_db_interface.py b/games_db_interface.py index 7c305ba..13b3313 100644 --- a/games_db_interface.py +++ b/games_db_interface.py @@ -40,3 +40,33 @@ def store_deck_for_seed(seed: str, deck: List[hanabi.hanab_game.DeckCard]): vals ) conn.commit() + + +def load_actions(game_id: int) -> List[hanabi.hanab_game.Action]: + cur = conn_manager.get_new_cursor() + cur.execute("SELECT type, target, value FROM game_actions " + "WHERE game_id = %s " + "ORDER BY turn ASC", + (game_id,)) + actions = [] + for action_type, target, value in cur.fetchall(): + actions.append( + hanabi.hanab_game.Action(hanabi.hanab_game.ActionType(action_type), target, value) + ) + return actions + + +def load_deck(seed: str) -> List[hanabi.hanab_game.DeckCard]: + cur = conn_manager.get_new_cursor() + cur.execute("SELECT card_index, suit_index, rank FROM seeds " + "WHERE seed = %s " + "ORDER BY card_index ASC", + (seed,) + ) + deck = [] + for index, (card_index, suit_index, rank) in enumerate(cur.fetchall()): + assert index == card_index + deck.append( + hanabi.hanab_game.DeckCard(suit_index, rank, card_index) + ) + return deck