From 78a43d7d24d7ea5d4ea8c7a28d790a9227f0f78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 23 Nov 2023 01:18:26 +0100 Subject: [PATCH] Initialize base ratings for variants --- database.py | 22 ++++++++++++++++++++++ install/default_config.yaml | 10 +++++----- main.py | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/database.py b/database.py index b30f951..dea42fa 100644 --- a/database.py +++ b/database.py @@ -2,6 +2,7 @@ import json import psycopg2 import psycopg2.extensions +import psycopg2.extras import psycopg2.errors import requests import unidecode @@ -98,6 +99,27 @@ def fetch_and_initialize_variants(): conn_manager.get_connection().commit() +def initialize_variant_base_ratings(): + config = config_manager.get_config() + conn = conn_manager.get_connection() + cur = conn.cursor() + cur.execute("SELECT id, name from variants") + ratings = [] + for variant_id, variant_name in cur.fetchall(): + for num_players in range(config.min_player_count, config.max_player_count + 1): + rating = config.variant_base_rating(variant_name, num_players) + ratings.append((variant_id, num_players, rating)) + + psycopg2.extras.execute_values( + cur, + "INSERT INTO variant_base_ratings (variant_id, player_count, rating)" + "VALUES %s " + "ON CONFLICT (variant_id, player_count) " + "DO UPDATE SET rating = EXCLUDED.rating", + ratings + ) + conn.commit() + def normalize_username(username: str) -> str: decoded = unidecode.unidecode(username) return decoded.lower() diff --git a/install/default_config.yaml b/install/default_config.yaml index b90bbbe..fb82741 100644 --- a/install/default_config.yaml +++ b/install/default_config.yaml @@ -4,19 +4,19 @@ variant_base_ratings: No Variant: 3p: 1500 4p: 1500 - 5p: 1500 + 5p: 1600 6 Suits: 3p: 1500 4p: 1500 - 5p: 1500 - Clue Starved: + 5p: 1600 + Clue Starved (5 Suits): 3p: 1500 4p: 1500 - 6p: 1700 + 5p: 1700 Clue Starved (6 Suits): 3p: 1500 4p: 1500 - 6p: 1700 + 5p: 1700 min_player_count: 3 max_player_count: 5 min_suits: 5 diff --git a/main.py b/main.py index e84bbe1..0c40c39 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ def subcommand_init(force: bool, fetch_variants: bool): logger.info("Successfully initialized database tables") if fetch_variants: database.fetch_and_initialize_variants() + database.initialize_variant_base_ratings() def subcommand_generate_config():