Add code to initialize variants

This commit is contained in:
Maximilian Keßler 2023-11-22 18:56:08 +01:00
parent dbe5098ef6
commit aa4b20df8c
Signed by: max
GPG key ID: BCC5A619923C0BA5
6 changed files with 88 additions and 6 deletions

View file

@ -1,5 +1,5 @@
import shutil import shutil
from typing import Dict from typing import Dict, List
import yaml import yaml
import platformdirs import platformdirs
@ -90,43 +90,57 @@ class Config:
def __init__(self, config: Dict): def __init__(self, config: Dict):
self._config = config self._config = config
@property
@check_config_attr @check_config_attr
def player_base_rating(self) -> int: def player_base_rating(self) -> int:
return self._config["player_base_rating"] return self._config["player_base_rating"]
@property
def min_player_count(self) -> int: def min_player_count(self) -> int:
return self._config["min_player_count"] return self._config["min_player_count"]
@property
@check_config_attr @check_config_attr
def max_player_count(self) -> int: def max_player_count(self) -> int:
return self._config["max_player_count"] return self._config["max_player_count"]
@property
@check_config_attr @check_config_attr
def min_suit_count(self) -> int: def min_suit_count(self) -> int:
return self._config["min_suit_count"] return self._config["min_suits"]
@property
@check_config_attr @check_config_attr
def max_suit_count(self) -> int: def max_suit_count(self) -> int:
return self._config["max_suit_count"] return self._config["max_suits"]
@property
@check_config_attr @check_config_attr
def starting_game_id(self) -> int: def starting_game_id(self) -> int:
return self._config["starting_game_id"] return self._config["starting_game_id"]
@property
@check_config_attr @check_config_attr
def ending_game_id(self) -> int: def ending_game_id(self) -> int:
return self._config["ending_game_id"] return self._config["ending_game_id"]
@property
@check_config_attr @check_config_attr
def starting_time(self) -> datetime.datetime: def starting_time(self) -> datetime.datetime:
time = self._config["starting_time"] time = self._config["starting_time"]
return dateutil.parser(time, tzinfos={'EST': 'US/Eastern'}) return dateutil.parser(time, tzinfos={'EST': 'US/Eastern'})
@property
@check_config_attr @check_config_attr
def ending_time(self) -> datetime.datetime: def ending_time(self) -> datetime.datetime:
time = self._config["ending_time"] time = self._config["ending_time"]
return dateutil.parser.parse(time, tzinfos={'EST': 'US/Eastern'}) 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 @check_config_attr
def variant_base_rating(self, variant_name: str, player_count: int) -> int: def variant_base_rating(self, variant_name: str, player_count: int) -> int:
global_base_rating = self._config["variant_base_rating"] global_base_rating = self._config["variant_base_rating"]

View file

@ -29,3 +29,6 @@ DB_TABLE_NAMES = [
DATABASE_SCHEMA_PATH = 'install/database_schema.sql' DATABASE_SCHEMA_PATH = 'install/database_schema.sql'
DEFAULT_DB_CONFIG_PATH = 'install/default_db_config.yaml' DEFAULT_DB_CONFIG_PATH = 'install/default_db_config.yaml'
DEFAULT_CONFIG_PATH = 'install/default_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'

View file

@ -1,10 +1,13 @@
import json
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
import psycopg2.errors import psycopg2.errors
import requests
import unidecode import unidecode
import constants import constants
from config import read_db_config from config import read_db_config, read_config
from log_setup import logger 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): 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()

View file

@ -111,8 +111,7 @@ CREATE TABLE variants (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
num_suits INTEGER NOT NULL, num_suits INTEGER NOT NULL,
clue_starved BOOLEAN NOT NULL, clue_starved BOOLEAN NOT NULL
max_score SMALLINT NOT NULL
); );
/** /**

View file

@ -21,7 +21,42 @@ min_player_count: 3
max_player_count: 5 max_player_count: 5
min_suits: 5 min_suits: 5
max_suits: 6 max_suits: 6
# Corresponds to game IDs from hanab.live
starting_game_id: 1000000 starting_game_id: 1000000
ending_game_id: 9999999 ending_game_id: 9999999
# EST = Eastern Standard Time, so USA/Eastern
starting_time: "2023-10-10 00:00:00 EST" starting_time: "2023-10-10 00:00:00 EST"
ending_time: "2023-12-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

View file

@ -4,3 +4,5 @@ PyYAML
verboselogs verboselogs
python-dateutil python-dateutil
unidecode unidecode
requests
requests_cache