CLI: add option to decompress hanab.live JSON links

This allows easy conversion of shortened JSON links
to the full-fledged JSON game format, which can be
used with other programs
This commit is contained in:
Maximilian Keßler 2024-06-04 00:02:57 +02:00
parent 1c656de615
commit a944bda70e
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -1,4 +1,5 @@
import argparse import argparse
import json
from typing import Optional from typing import Optional
import verboselogs import verboselogs
@ -38,6 +39,13 @@ def subcommand_analyze(game_id: int, download: bool = False):
) )
def subcommand_decompress(game_link: str):
parts = game_link.split('replay-json/')
game_str = parts[-1].rstrip('/')
game = compress.decompress_game_state(game_str)
print(json.dumps(game.to_json()))
def subcommand_init(force: bool, populate: bool): def subcommand_init(force: bool, populate: bool):
tables = init_database.get_existing_tables() tables = init_database.get_existing_tables()
if len(tables) > 0 and not force: if len(tables) > 0 and not force:
@ -139,6 +147,10 @@ def add_solve_subparser(subparsers):
parser = subparsers.add_parser('solve', help='Seed solving') parser = subparsers.add_parser('solve', help='Seed solving')
parser.add_argument('--var_id', type=int, help='Variant id to solve instances from.', default=0) parser.add_argument('--var_id', type=int, help='Variant id to solve instances from.', default=0)
def add_decompress_subparser(subparsers):
parser = subparsers.add_parser('decompress', help='Decompress a hanab.live JSON-encoded replay link')
parser.add_argument('game_link', type=str)
def main_parser() -> argparse.ArgumentParser: def main_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -153,6 +165,7 @@ def main_parser() -> argparse.ArgumentParser:
add_download_subparser(subparsers) add_download_subparser(subparsers)
add_config_gen_subparser(subparsers) add_config_gen_subparser(subparsers)
add_solve_subparser(subparsers) add_solve_subparser(subparsers)
add_decompress_subparser(subparsers)
return parser return parser
@ -164,7 +177,8 @@ def hanabi_cli():
'init': subcommand_init, 'init': subcommand_init,
'download': subcommand_download, 'download': subcommand_download,
'gen-config': subcommand_gen_config, 'gen-config': subcommand_gen_config,
'solve': subcommand_solve 'solve': subcommand_solve,
'decompress': subcommand_decompress
}[args.command] }[args.command]
if args.command != 'gen-config': if args.command != 'gen-config':