Initialize player base ratings on sign-up
This commit is contained in:
parent
78a43d7d24
commit
a15059318c
1 changed files with 53 additions and 12 deletions
65
database.py
65
database.py
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions
|
import psycopg2.extensions
|
||||||
|
@ -76,7 +77,8 @@ def init_database():
|
||||||
def fetch_and_initialize_variants():
|
def fetch_and_initialize_variants():
|
||||||
response = requests.get(constants.VARIANTS_JSON_URL)
|
response = requests.get(constants.VARIANTS_JSON_URL)
|
||||||
if not response.status_code == 200:
|
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
|
return
|
||||||
variants = json.loads(response.text)
|
variants = json.loads(response.text)
|
||||||
|
|
||||||
|
@ -120,11 +122,18 @@ def initialize_variant_base_ratings():
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def normalize_username(username: str) -> str:
|
def normalize_username(username: str) -> str:
|
||||||
decoded = unidecode.unidecode(username)
|
decoded = unidecode.unidecode(username)
|
||||||
return decoded.lower()
|
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):
|
def add_player_name(player_name: str):
|
||||||
conn = conn_manager.get_connection()
|
conn = conn_manager.get_connection()
|
||||||
cur = conn.cursor()
|
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):
|
def add_user_name_to_player(hanabi_username: str, player_name: str):
|
||||||
normalized_username = normalize_username(hanabi_username)
|
normalized_username = normalize_username(hanabi_username)
|
||||||
cur = conn_manager.get_new_cursor()
|
user_id = get_user_id(player_name)
|
||||||
cur.execute("SELECT id FROM users WHERE player_name = (%s)", (player_name,))
|
|
||||||
user_id = cur.fetchone()
|
|
||||||
if user_id is None:
|
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
|
return
|
||||||
else:
|
else:
|
||||||
|
cur = conn_manager.get_new_cursor()
|
||||||
cur.execute("SELECT username, player_name from user_accounts "
|
cur.execute("SELECT username, player_name from user_accounts "
|
||||||
"INNER JOIN users"
|
"INNER JOIN users"
|
||||||
" ON user_accounts.user_id = users.id "
|
" 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:
|
if res is not None:
|
||||||
existing_username, existing_player_name = res
|
existing_username, existing_player_name = res
|
||||||
if existing_player_name == player_name:
|
if existing_player_name == player_name:
|
||||||
logger.warn("Hanabi username {} is already registered to player {}, attempted to re-register it.".format(
|
logger.warn(
|
||||||
existing_username, existing_player_name
|
"Hanabi username {} is already registered to player {}, attempted to re-register it.".format(
|
||||||
))
|
existing_username, existing_player_name
|
||||||
|
))
|
||||||
else:
|
else:
|
||||||
logger.error("Hanabi username {} is already associated to player {}, cannot register it to player {}.".format(
|
logger.error(
|
||||||
res[0], res[1], player_name
|
"Hanabi username {} is already associated to player {}, cannot register it to player {}.".format(
|
||||||
))
|
res[0], res[1], player_name
|
||||||
|
))
|
||||||
return
|
return
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO user_accounts (username, normalized_username, user_id) VALUES (%s, %s, %s)",
|
"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()
|
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_player_name(player_name)
|
||||||
add_user_name_to_player(user_name, player_name)
|
add_user_name_to_player(user_name, player_name)
|
||||||
|
init_player_base_rating(player_name, base_rating)
|
||||||
|
|
||||||
|
|
||||||
def get_variant_ids():
|
def get_variant_ids():
|
||||||
|
|
Loading…
Reference in a new issue