2023-05-14 19:11:51 +02:00
|
|
|
#! /usr/bin/python3
|
|
|
|
|
|
|
|
import argparse
|
2023-05-20 14:32:42 +02:00
|
|
|
|
|
|
|
import verboselogs
|
2023-05-14 19:11:51 +02:00
|
|
|
|
|
|
|
from check_game import check_game
|
2023-07-04 18:52:59 +02:00
|
|
|
from hanabi.live.download_data import detailed_export_game
|
|
|
|
from hanabi.live.compress import link
|
|
|
|
from hanabi.log_setup import logger, logger_manager
|
2023-05-14 19:11:51 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
init db + populate tables
|
|
|
|
download games of variant
|
|
|
|
download single game
|
|
|
|
analyze single game
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def add_init_subparser(subparsers):
|
|
|
|
parser = subparsers.add_parser(
|
|
|
|
'init',
|
|
|
|
help='Init database tables, retrieve variant and suit information from hanab.live'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_download_subparser(subparsers):
|
|
|
|
parser = subparsers.add_parser('download', help='Download games from hanab.live')
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('--var', '-v', type=int)
|
|
|
|
group.add_argument('--id', '-i', type=int)
|
|
|
|
|
|
|
|
|
|
|
|
def add_analyze_subparser(subparsers):
|
|
|
|
parser = subparsers.add_parser('analyze', help='Analyze a game and find the last winning state')
|
|
|
|
parser.add_argument('game_id', type=int)
|
|
|
|
parser.add_argument('--download', '-d', help='Download game if not in database', action='store_true')
|
|
|
|
|
|
|
|
|
|
|
|
def analyze_game(game_id: int, download: bool = False):
|
|
|
|
if download:
|
|
|
|
detailed_export_game(game_id)
|
2023-05-20 14:32:42 +02:00
|
|
|
logger.info('Analyzing game {}'.format(game_id))
|
2023-05-14 19:11:51 +02:00
|
|
|
turn, sol = check_game(game_id)
|
|
|
|
if turn == 0:
|
2023-05-20 14:32:42 +02:00
|
|
|
logger.info('Instance is unfeasible')
|
2023-05-14 19:11:51 +02:00
|
|
|
else:
|
2023-05-20 14:32:42 +02:00
|
|
|
logger.info('Game was first lost after {} turns.'.format(turn))
|
|
|
|
logger.info(
|
|
|
|
'A replay achieving perfect score from the previous turn onwards is: {}#{}'
|
|
|
|
.format(link(sol), turn)
|
2023-05-14 19:11:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main_parser() -> argparse.ArgumentParser:
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog='hanabi_suite',
|
|
|
|
description='High-level interface for analysis of hanabi instances.'
|
|
|
|
)
|
2023-05-20 14:38:38 +02:00
|
|
|
parser.add_argument('--verbose', '-v', help='Enable verbose logging to console', action='store_true')
|
2023-05-14 19:11:51 +02:00
|
|
|
subparsers = parser.add_subparsers(dest='command', required=True, help='select subcommand')
|
|
|
|
|
|
|
|
add_init_subparser(subparsers)
|
|
|
|
add_analyze_subparser(subparsers)
|
|
|
|
add_download_subparser(subparsers)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = main_parser().parse_args()
|
|
|
|
switcher = {
|
|
|
|
'analyze': analyze_game
|
|
|
|
}
|
2023-05-20 14:38:38 +02:00
|
|
|
if args.verbose:
|
|
|
|
logger_manager.set_console_level(verboselogs.VERBOSE)
|
2023-05-14 19:11:51 +02:00
|
|
|
method_args = dict(vars(args))
|
|
|
|
method_args.pop('command')
|
2023-05-20 14:38:38 +02:00
|
|
|
method_args.pop('verbose')
|
2023-05-14 19:11:51 +02:00
|
|
|
switcher[args.command](**method_args)
|