enable teams to register for tournaments (#53)

Co-authored-by: David Rodenkirchen <davidr.develop@gmail.com>
Reviewed-on: #53
This commit was merged in pull request #53.
This commit is contained in:
2026-02-22 00:45:11 +00:00
parent f4db57b2ff
commit d57f4baedd
7 changed files with 227 additions and 40 deletions
@@ -859,6 +859,7 @@ class DatabaseService:
t.status AS tournament_status,
t.max_participants,
t.created_at,
t.participant_type AS tournament_participant_type,
/* =======================
Game Title
@@ -874,6 +875,7 @@ class DatabaseService:
======================= */
tp.id AS participant_id,
tp.user_id,
tp.team_id,
tp.participant_type,
tp.seed,
tp.joined_at
@@ -907,6 +909,8 @@ class DatabaseService:
if current_tournament is None or current_tournament.id != row["tournament_id"]:
if current_tournament is not None:
tournaments.append(current_tournament)
participant_type = self._parse_participant_type(row["tournament_participant_type"])
id_accessor = "user_id" if participant_type == ParticipantType.PLAYER else "team_id"
current_tournament = Tournament(
id_=row["tournament_id"],
name=row["tournament_name"],
@@ -920,14 +924,16 @@ class DatabaseService:
format_=self._parse_tournament_format(row["tournament_format"]),
start_time=row["start_time"],
status=self._parse_tournament_status(row["tournament_status"]),
participants=[Participant(id_=row["user_id"], participant_type=self._parse_participant_type(row["participant_type"]))] if row["user_id"] is not None else [],
participants=[Participant(id_=row[id_accessor], participant_type=self._parse_participant_type(row["participant_type"]))] if row[id_accessor] is not None else [],
matches=None, # ToDo: Implement
rounds=[], # ToDo: Implement
max_participants=row["max_participants"]
max_participants=row["max_participants"],
participant_type=participant_type
)
else:
id_accessor = "user_id" if current_tournament.participant_type == ParticipantType.PLAYER else "team_id"
current_tournament.add_participant(
Participant(id_=row["user_id"], participant_type=self._parse_participant_type(row["participant_type"]))
Participant(id_=row[id_accessor], participant_type=self._parse_participant_type(row["participant_type"]))
)
else:
tournaments.append(current_tournament)
@@ -935,11 +941,14 @@ class DatabaseService:
return tournaments
async def add_participant_to_tournament(self, participant: Participant, tournament: Tournament) -> None:
if participant.participant_type != tournament.participant_type:
raise ValueError(f"Can not add {participant.participant_type.name} to {tournament.participant_type.name} tournament")
accessor = "user_id" if participant.participant_type == ParticipantType.PLAYER else "team_id"
async with self._connection_pool.acquire() as conn:
async with conn.cursor(aiomysql.Cursor) as cursor:
try:
await cursor.execute(
"INSERT INTO tournament_participants (tournament_id, user_id, participant_type) VALUES (%s, %s, %s);",
f"INSERT INTO tournament_participants (tournament_id, {accessor}, participant_type) VALUES (%s, %s, %s);",
(tournament.id, participant.id, participant.participant_type.name)
)
await conn.commit()
@@ -952,11 +961,12 @@ class DatabaseService:
logger.warning(f"Error adding participant to tournament: {e}")
async def remove_participant_from_tournament(self, participant: Participant, tournament: Tournament) -> None:
accessor = "user_id" if participant.participant_type == ParticipantType.PLAYER else "team_id"
async with self._connection_pool.acquire() as conn:
async with conn.cursor(aiomysql.Cursor) as cursor:
try:
await cursor.execute(
"DELETE FROM tournament_participants WHERE (tournament_id = %s AND user_id = %s);",
f"DELETE FROM tournament_participants WHERE (tournament_id = %s AND {accessor} = %s);",
(tournament.id, participant.id)
)
await conn.commit()