forked from Hanabi/hanabi-league
Add CLI option to fetch variants
This commit is contained in:
parent
dddb9de878
commit
01ebfa380d
2 changed files with 30 additions and 27 deletions
52
database.py
52
database.py
|
@ -72,6 +72,32 @@ def init_database():
|
||||||
logger.verbose("Initialized DB tables.")
|
logger.verbose("Initialized DB tables.")
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_and_initialize_variants():
|
||||||
|
response = requests.get(constants.VARIANTS_JSON_URL)
|
||||||
|
if not response.status_code == 200:
|
||||||
|
logger.error("Could not download variants.json file from github (tried url {})".format(constants.VARIANTS_JSON_URL))
|
||||||
|
return
|
||||||
|
variants = json.loads(response.text)
|
||||||
|
|
||||||
|
config = read_config()
|
||||||
|
|
||||||
|
for variant in variants:
|
||||||
|
variant_id = variant['id']
|
||||||
|
name = variant['name']
|
||||||
|
clue_starved = variant.get('clueStarved', False)
|
||||||
|
num_suits = len(variant['suits'])
|
||||||
|
|
||||||
|
if config.min_suit_count <= num_suits <= config.max_suit_count:
|
||||||
|
if any(var_name in name.lower() for var_name in config.excluded_variants):
|
||||||
|
continue
|
||||||
|
cur = conn_manager.get_new_cursor()
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO variants (id, name, num_suits, clue_starved) VALUES (%s, %s, %s, %s)",
|
||||||
|
(variant_id, name, num_suits, clue_starved)
|
||||||
|
)
|
||||||
|
conn_manager.get_connection().commit()
|
||||||
|
|
||||||
|
|
||||||
def add_player_name(player_name: str):
|
def add_player_name(player_name: str):
|
||||||
conn = conn_manager.get_connection()
|
conn = conn_manager.get_connection()
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
@ -121,29 +147,3 @@ def add_user_name_to_player(hanabi_username: str, player_name: str):
|
||||||
def add_player(player_name: str, user_name: str):
|
def add_player(player_name: str, user_name: str):
|
||||||
add_player_name(player_name)
|
add_player_name(player_name)
|
||||||
add_user_name_to_player(user_name, player_name)
|
add_user_name_to_player(user_name, player_name)
|
||||||
|
|
||||||
|
|
||||||
def fetch_and_initialize_variants():
|
|
||||||
response = requests.get(constants.VARIANTS_JSON_URL)
|
|
||||||
if not response.status_code == 200:
|
|
||||||
logger.error("Could not download variants.json file from github (tried url {})".format(constants.VARIANTS_JSON_URL))
|
|
||||||
return
|
|
||||||
variants = json.loads(response.text)
|
|
||||||
|
|
||||||
config = read_config()
|
|
||||||
|
|
||||||
for variant in variants:
|
|
||||||
variant_id = variant['id']
|
|
||||||
name = variant['name']
|
|
||||||
clue_starved = variant.get('clueStarved', False)
|
|
||||||
num_suits = len(variant['suits'])
|
|
||||||
|
|
||||||
if config.min_suit_count <= num_suits <= config.max_suit_count:
|
|
||||||
if any(var_name in name.lower() for var_name in config.excluded_variants):
|
|
||||||
continue
|
|
||||||
cur = conn_manager.get_new_cursor()
|
|
||||||
cur.execute(
|
|
||||||
"INSERT INTO variants (id, name, num_suits, clue_starved) VALUES (%s, %s, %s, %s)",
|
|
||||||
(variant_id, name, num_suits, clue_starved)
|
|
||||||
)
|
|
||||||
conn_manager.get_connection().commit()
|
|
||||||
|
|
5
main.py
5
main.py
|
@ -11,7 +11,7 @@ import log_setup
|
||||||
from log_setup import logger
|
from log_setup import logger
|
||||||
|
|
||||||
|
|
||||||
def subcommand_init(force: bool):
|
def subcommand_init(force: bool, fetch_variants: bool):
|
||||||
tables = database.get_existing_tables()
|
tables = database.get_existing_tables()
|
||||||
if len(tables) > 0 and not force:
|
if len(tables) > 0 and not force:
|
||||||
logger.info(
|
logger.info(
|
||||||
|
@ -29,6 +29,8 @@ def subcommand_init(force: bool):
|
||||||
return
|
return
|
||||||
database.init_database()
|
database.init_database()
|
||||||
logger.info("Successfully initialized database tables")
|
logger.info("Successfully initialized database tables")
|
||||||
|
if fetch_variants:
|
||||||
|
database.fetch_and_initialize_variants()
|
||||||
|
|
||||||
|
|
||||||
def subcommand_generate_config():
|
def subcommand_generate_config():
|
||||||
|
@ -47,6 +49,7 @@ def get_parser() -> argparse.ArgumentParser:
|
||||||
|
|
||||||
init_parser = subparsers.add_parser('init', help='Initialize database.')
|
init_parser = subparsers.add_parser('init', help='Initialize database.')
|
||||||
init_parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true')
|
init_parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true')
|
||||||
|
init_parser.add_argument('--fetch-variants', '-v', help='Fetch and initialize variants', action='store_true')
|
||||||
|
|
||||||
subparsers.add_parser('generate-config', help='Generate config file at default location')
|
subparsers.add_parser('generate-config', help='Generate config file at default location')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue