From a15059318c4e1442978a86e535bc3da6c8794295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 23 Nov 2023 01:36:23 +0100 Subject: [PATCH] Initialize player base ratings on sign-up --- database.py | 65 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/database.py b/database.py index dea42fa..8bc7d26 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,5 @@ import json +from typing import Optional import psycopg2 import psycopg2.extensions @@ -76,7 +77,8 @@ def init_database(): 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)) + logger.error( + "Could not download variants.json file from github (tried url {})".format(constants.VARIANTS_JSON_URL)) return variants = json.loads(response.text) @@ -120,11 +122,18 @@ def initialize_variant_base_ratings(): ) conn.commit() + def normalize_username(username: str) -> str: decoded = unidecode.unidecode(username) return decoded.lower() +def get_user_id(player_name: str) -> Optional[id]: + cur = conn_manager.get_new_cursor() + cur.execute("SELECT id FROM users WHERE player_name = (%s)", (player_name,)) + return cur.fetchone() + + def add_player_name(player_name: str): conn = conn_manager.get_connection() cur = conn.cursor() @@ -138,13 +147,12 @@ def add_player_name(player_name: str): def add_user_name_to_player(hanabi_username: str, player_name: str): normalized_username = normalize_username(hanabi_username) - cur = conn_manager.get_new_cursor() - cur.execute("SELECT id FROM users WHERE player_name = (%s)", (player_name,)) - user_id = cur.fetchone() + user_id = get_user_id(player_name) if user_id is None: - logger.error("Display name {} not found in database, cannot add username to it.".format(player_name)) + logger.error("Player {} not found in database, cannot add username to it.".format(player_name)) return else: + cur = conn_manager.get_new_cursor() cur.execute("SELECT username, player_name from user_accounts " "INNER JOIN users" " ON user_accounts.user_id = users.id " @@ -156,13 +164,15 @@ def add_user_name_to_player(hanabi_username: str, player_name: str): if res is not None: existing_username, existing_player_name = res if existing_player_name == player_name: - logger.warn("Hanabi username {} is already registered to player {}, attempted to re-register it.".format( - existing_username, existing_player_name - )) + logger.warn( + "Hanabi username {} is already registered to player {}, attempted to re-register it.".format( + existing_username, existing_player_name + )) else: - logger.error("Hanabi username {} is already associated to player {}, cannot register it to player {}.".format( - res[0], res[1], player_name - )) + logger.error( + "Hanabi username {} is already associated to player {}, cannot register it to player {}.".format( + res[0], res[1], player_name + )) return cur.execute( "INSERT INTO user_accounts (username, normalized_username, user_id) VALUES (%s, %s, %s)", @@ -171,9 +181,40 @@ def add_user_name_to_player(hanabi_username: str, player_name: str): conn_manager.get_connection().commit() -def add_player(user_name: str, player_name: str): +def init_player_base_rating(player_name: str, base_rating: Optional[int] = None): + config = config_manager.get_config() + if base_rating is None: + base_rating = config.player_base_rating + cur = conn_manager.get_new_cursor() + vals = [] + user_id = get_user_id(player_name) + if user_id is None: + err_msg = "Cannot initialise base rating for player {}: No such registered player.".format(player_name) + logger.error(err_msg) + raise ValueError(err_msg) + for rating_type in range(0, 2): + vals.append((user_id, rating_type, base_rating)) + try: + psycopg2.extras.execute_values( + cur, + "INSERT INTO user_base_ratings (user_id, type, rating) VALUES %s", + vals + ) + conn_manager.get_connection().commit() + except psycopg2.errors.UniqueViolation as e: + err_msg = "Failed to initialize base ratings for player {}: Ratings already exist".format(player_name) + logger.error("{}:\n{}".format(err_msg, e)) + raise ValueError(err_msg) from e + + +def add_player(user_name: str, player_name: str, base_rating: Optional[int] = None): + """ + Convenience function: Adds a player to the database, along with associated username and + initializes the rating for this player. + """ add_player_name(player_name) add_user_name_to_player(user_name, player_name) + init_player_base_rating(player_name, base_rating) def get_variant_ids():