Functions to add players to database

This commit is contained in:
Maximilian Keßler 2023-11-22 18:22:53 +01:00
parent 22bfb38011
commit dbe5098ef6
Signed by: max
GPG key ID: BCC5A619923C0BA5
3 changed files with 61 additions and 3 deletions

View file

@ -1,5 +1,7 @@
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
import psycopg2.errors
import unidecode
import constants import constants
from config import read_db_config from config import read_db_config
@ -30,6 +32,11 @@ class DBConnectionManager:
self.connect() self.connect()
return self._conn return self._conn
def get_new_cursor(self) -> psycopg2.extensions.cursor:
if self._conn is None:
self.connect()
return self._conn.cursor()
# Global instance that will hold our DB connection # Global instance that will hold our DB connection
conn_manager = DBConnectionManager() conn_manager = DBConnectionManager()
@ -60,3 +67,54 @@ def init_database():
cur.execute(f.read()) cur.execute(f.read())
conn.commit() conn.commit()
logger.verbose("Initialized DB tables.") logger.verbose("Initialized DB tables.")
def add_player_name(player_name: str):
conn = conn_manager.get_connection()
cur = conn.cursor()
try:
cur.execute("INSERT INTO users (player_name) VALUES (%s)", (player_name,))
conn.commit()
except psycopg2.errors.UniqueViolation:
logger.warn("Player name {} already exists in the database, aborting insertion.".format(player_name))
conn.rollback()
def add_user_name_to_player(hanabi_username: str, player_name: str):
normalized_username = unidecode.unidecode(hanabi_username).lower()
cur = conn_manager.get_new_cursor()
cur.execute("SELECT id FROM users WHERE player_name = (%s)", (player_name,))
user_id = cur.fetchone()
if user_id is None:
logger.error("Display name {} not found in database, cannot add username to it.".format(player_name))
return
else:
cur.execute("SELECT username, player_name from user_accounts "
"INNER JOIN users"
" ON user_accounts.user_id = users.id "
"WHERE "
" normalized_username = (%s)",
(normalized_username,)
)
res = cur.fetchone()
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
))
else:
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)",
(hanabi_username, normalized_username, user_id)
)
conn_manager.get_connection().commit()
def add_player(player_name: str, user_name: str):
add_player_name(player_name)
add_user_name_to_player(user_name, player_name)

View file

@ -68,8 +68,7 @@
DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NOT NULL, player_name TEXT NOT NULL UNIQUE
discord_tag TEXT
); );
@ -246,7 +245,7 @@ DROP TABLE IF EXISTS variant_base_ratings CASCADE;
CREATE TABLE variant_base_ratings ( CREATE TABLE variant_base_ratings (
/* Note that a variant already includes the number of suits, so we do not have to add this here. */ /* Note that a variant already includes the number of suits, so we do not have to add this here. */
variant_id SMALLINT, variant_id SMALLINT,
player_count SMALLINT NOT NULL player_count SMALLINT NOT NULL,
rating REAL NOT NULL, rating REAL NOT NULL,
PRIMARY KEY (variant_id, player_count) PRIMARY KEY (variant_id, player_count)
); );

View file

@ -3,3 +3,4 @@ platformdirs
PyYAML PyYAML
verboselogs verboselogs
python-dateutil python-dateutil
unidecode