From 9c8d1940fe95a553eb85ce67ecacd205efe4909a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 9 Oct 2024 17:45:51 +0200 Subject: [PATCH] CLI: Add option to show seeds easily --- src/hanabi/cli.py | 23 ++++++++++++++++++++++- src/hanabi/database/games_db_interface.py | 15 ++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/hanabi/cli.py b/src/hanabi/cli.py index 2398917..6b2ceb2 100755 --- a/src/hanabi/cli.py +++ b/src/hanabi/cli.py @@ -10,8 +10,10 @@ from hanabi.live import check_game from hanabi.live import download_data from hanabi.live import compress from hanabi.live import instance_finder +from hanabi.hanab_game import GameState from hanabi.database import init_database from hanabi.database import global_db_connection_manager +from hanabi.database.games_db_interface import load_instance """ Commands supported: @@ -23,6 +25,16 @@ analyze single game """ +def subcommand_show(seed: str): + inst = load_instance(seed) + if inst is None: + logger.info("This seed does not exist in the database") + return + game = GameState(inst) + game.terminate() + l = compress.link(game) + logger.info("Deck: {}\nReplay Link: {}".format(inst.deck, l)) + def subcommand_analyze(game_id: int, download: bool = False): if download: @@ -94,6 +106,13 @@ def subcommand_solve(var_id): def subcommand_gen_config(): global_db_connection_manager.create_config_file() +def add_show_seed_subparser(subparsers): + parser = subparsers.add_parser( + 'show', + help='Show a seed and output a replay link for it' + ) + parser.add_argument('seed', type=str) + def add_init_subparser(subparsers): parser = subparsers.add_parser( @@ -166,6 +185,7 @@ def main_parser() -> argparse.ArgumentParser: add_config_gen_subparser(subparsers) add_solve_subparser(subparsers) add_decompress_subparser(subparsers) + add_show_seed_subparser(subparsers) return parser @@ -178,7 +198,8 @@ def hanabi_cli(): 'download': subcommand_download, 'gen-config': subcommand_gen_config, 'solve': subcommand_solve, - 'decompress': subcommand_decompress + 'decompress': subcommand_decompress, + 'show': subcommand_show }[args.command] if args.command != 'gen-config': diff --git a/src/hanabi/database/games_db_interface.py b/src/hanabi/database/games_db_interface.py index 8df26cf..1b9d0ca 100644 --- a/src/hanabi/database/games_db_interface.py +++ b/src/hanabi/database/games_db_interface.py @@ -1,4 +1,4 @@ -from typing import List, Tuple +from typing import List, Tuple, Optional import psycopg2.extras @@ -76,6 +76,19 @@ def load_deck(seed: str) -> List[hanabi.hanab_game.DeckCard]: raise ValueError(err_msg) return deck +def load_instance(seed: str) -> Optional[hanabi.live.hanab_live.HanabLiveInstance]: + cur.execute( + "SELECT num_players, variant_id " + "FROM seeds WHERE seed = %s ", + (seed,) + ) + res = cur.fetchone() + if res is None: + return None + (num_players, var_id) = res + deck = load_deck(seed) + return hanabi.live.hanab_live.HanabLiveInstance(deck, num_players, var_id) + def load_game_parts(game_id: int) -> Tuple[hanabi.live.hanab_live.HanabLiveInstance, List[hanabi.hanab_game.Action]]: """