From fb175c38fe572a02c43bc6350d29d071f03a279d Mon Sep 17 00:00:00 2001 From: craisin Date: Fri, 28 Nov 2025 22:05:35 -0800 Subject: [PATCH] add tournament creation --- allfence.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++------ app.py | 2 +- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/allfence.py b/allfence.py index 45ca469..e2778b1 100644 --- a/allfence.py +++ b/allfence.py @@ -1,6 +1,7 @@ import bcrypt import datetime import psycopg2 +from textwrap import dedent def create_database_connection(db_name, user, password, host, port=5432): @@ -18,14 +19,14 @@ def create_database_connection(db_name, user, password, host, port=5432): """ connection = psycopg2.connect( - db_name=db_name, + dbname=db_name, user=user, password=password, host=host, port=port ) with connection: - with connection as curs: + with connection.cursor() as curs: curs.execute(dedent(""" CREATE TABLE IF NOT EXISTS Users ( id SERIAL PRIMARY KEY, @@ -37,7 +38,7 @@ def create_database_connection(db_name, user, password, host, port=5432): CREATE TABLE IF NOT EXISTS Events ( id SERIAL PRIMARY KEY, name varchar(255), - datetime datetime, + datetime timestamp, max_age int, completed boolean, registration varchar(255), @@ -111,7 +112,7 @@ class Users: Args: db (psycopg2.connection): connection to postgres database - id (int): user id + user_id (int): user id Returns: bool, dict of str to str, dict of str to value: user existance, login info (username, password), user info (name (str), dob (datetime.date)) @@ -122,7 +123,7 @@ class Users: curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,)) user = curs.fetchall() if not user: - return False, {}, {} + return False, None, None user = user[0] return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]} @@ -149,11 +150,58 @@ class Tournaments: event_id = curs.fetchone()[0] db.commit() return event_id + + @staticmethod + def get_event(db, event_id): + + """Gets the details of an event based on its id + + Args: + db (psycopg2.connection): connection to postgres database + event_id (int): event id + + Returns: + bool, str, datetime.datetime, int, bool, list of ints, list of ints: existance of event, name, datetime of event, max age, event completion, registration, results + """ + + with db: + with db.cursor() as curs: + curs.execute("SELECT * FROM Events WHERE id = %s", (event_id,)) + event = curs.fetchall() + if not event: + return False, None, None, None, None, None, None + event = event[0] + return True, event[1], event[2], event[3], event[4], list(map(int, event[5].split())), list(map(int, event[6].split())) @staticmethod - def create_tournament(db, events): + def create_tournament(db, name, event_ids): + + """Create a new tournament in the database + + Args: + db (psycopg2.connection): connection to postgres database + name (str): name of tournament + event_ids (list of ints): list of events in the tournament + + Returns: + int: id of event + """ + + event_str = " ".join(map(str, event_ids)) + events = [Tournaments.get_event(db, event_id)[2] for event_id in event_ids] + print(events) + start_date, end_date = min(events).date(), max(events).date() + with db: + with db.cursor() as curs: + curs.execute("INSERT INTO Tournaments (name, start_date, end_date, events) VALUES (%s, %s, %s, %s) RETURNING id", (name, start_date, end_date, event_str)) + tournament_id = curs.fetchone()[0] + db.commit() + return tournament_id + + @staticmethod + def add_event_registration(db, event_id, user_id): pass @staticmethod - def finish_event(db, event): + def add_event_results(db, event): pass \ No newline at end of file diff --git a/app.py b/app.py index 5023040..80b873a 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ import psycopg2 as sql from textwrap import dedent DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT = "database", "user", "password", "localhost", "5432" -connection = allfence.connect( +connection = allfence.create_database_connection( db_name=DB_NAME, user=DB_USER, password=DB_PASSWORD,