From aa4b20df8ce572c259c82cf140e3ee46ca583a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 22 Nov 2023 18:56:08 +0100 Subject: [PATCH] Add code to initialize variants --- config.py | 20 +++++++++++++++++--- constants.py | 3 +++ database.py | 31 ++++++++++++++++++++++++++++++- install/database_schema.sql | 3 +-- install/default_config.yaml | 35 +++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 6 files changed, 88 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index 4841521..2e74ad5 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,5 @@ import shutil -from typing import Dict +from typing import Dict, List import yaml import platformdirs @@ -90,43 +90,57 @@ class Config: def __init__(self, config: Dict): self._config = config + @property @check_config_attr def player_base_rating(self) -> int: return self._config["player_base_rating"] + @property def min_player_count(self) -> int: return self._config["min_player_count"] + @property @check_config_attr def max_player_count(self) -> int: return self._config["max_player_count"] + @property @check_config_attr def min_suit_count(self) -> int: - return self._config["min_suit_count"] + return self._config["min_suits"] + @property @check_config_attr def max_suit_count(self) -> int: - return self._config["max_suit_count"] + return self._config["max_suits"] + @property @check_config_attr def starting_game_id(self) -> int: return self._config["starting_game_id"] + @property @check_config_attr def ending_game_id(self) -> int: return self._config["ending_game_id"] + @property @check_config_attr def starting_time(self) -> datetime.datetime: time = self._config["starting_time"] return dateutil.parser(time, tzinfos={'EST': 'US/Eastern'}) + @property @check_config_attr def ending_time(self) -> datetime.datetime: time = self._config["ending_time"] return dateutil.parser.parse(time, tzinfos={'EST': 'US/Eastern'}) + @property + @check_config_attr + def excluded_variants(self) -> List[str]: + return [var.lower() for var in self._config["excluded_variants"]] + @check_config_attr def variant_base_rating(self, variant_name: str, player_count: int) -> int: global_base_rating = self._config["variant_base_rating"] diff --git a/constants.py b/constants.py index f205ada..763cb7f 100644 --- a/constants.py +++ b/constants.py @@ -29,3 +29,6 @@ DB_TABLE_NAMES = [ DATABASE_SCHEMA_PATH = 'install/database_schema.sql' DEFAULT_DB_CONFIG_PATH = 'install/default_db_config.yaml' DEFAULT_CONFIG_PATH = 'install/default_config.yaml' + +VARIANTS_JSON_URL = 'https://raw.githubusercontent.com/Hanabi-Live/hanabi-live/main/packages/data/src/json/variants.json' + diff --git a/database.py b/database.py index 081a8df..0b81f45 100644 --- a/database.py +++ b/database.py @@ -1,10 +1,13 @@ +import json + import psycopg2 import psycopg2.extensions import psycopg2.errors +import requests import unidecode import constants -from config import read_db_config +from config import read_db_config, read_config from log_setup import logger @@ -118,3 +121,29 @@ def add_user_name_to_player(hanabi_username: str, player_name: str): def add_player(player_name: str, user_name: str): add_player_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() diff --git a/install/database_schema.sql b/install/database_schema.sql index 0f3bea5..aecec94 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -111,8 +111,7 @@ CREATE TABLE variants ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, num_suits INTEGER NOT NULL, - clue_starved BOOLEAN NOT NULL, - max_score SMALLINT NOT NULL + clue_starved BOOLEAN NOT NULL ); /** diff --git a/install/default_config.yaml b/install/default_config.yaml index 2425c8b..b90bbbe 100644 --- a/install/default_config.yaml +++ b/install/default_config.yaml @@ -21,7 +21,42 @@ min_player_count: 3 max_player_count: 5 min_suits: 5 max_suits: 6 +# Corresponds to game IDs from hanab.live starting_game_id: 1000000 ending_game_id: 9999999 +# EST = Eastern Standard Time, so USA/Eastern starting_time: "2023-10-10 00:00:00 EST" ending_time: "2023-12-10 00:00:00 EST" +# Any variant that contains one of these keywords will not be allowed for the league. +excluded_variants: + - Ambiguous + - Mix + - Evens + - Dark + - Cocoa + - Fives + - Ones + - Black + - Gray + - Matryoshka + - Dual + - Critical + - Blind + - Mute + - Alternating + - Duck + - Cow + - Synesthesia + - Reversed + - Down + - Throw + - Funnels + - Chimneys + - Omni + - White + - Brown + - Pink + - Rainbow + - Prism + - Sudoku + - "Null" # We have to enquote this here, since otherwise The YAML spec treats this as a null (=absent) value diff --git a/requirements.txt b/requirements.txt index 519bc15..59e53d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ PyYAML verboselogs python-dateutil unidecode +requests +requests_cache