diff --git a/data/data_api.py b/data/data_api.py index cb88fb5..fdb1ec4 100644 --- a/data/data_api.py +++ b/data/data_api.py @@ -242,21 +242,60 @@ def get_player_rank(player_id, system_id): return row['rank'] return None + + def join_league(player_id, gamesystem_id): + try: + # 1. Die richtige Season für dieses System holen + season_id = get_current_season_id(gamesystem_id) + + connection = db_connection() + cursor = connection.cursor() + + query = """ + INSERT INTO player_game_statistic + (player_id, gamesystem_id, season_id) + VALUES (%s, %s, %s) + """ + cursor.execute(query, (player_id, gamesystem_id, season_id)) + connection.commit() + + logger.log(f"Erfolg: Spieler {player_id} joined System {gamesystem_id} in Season {season_id}") + + except Exception as e: + logger.log(f"FEHLER beim Beitreten: {e}") + raise + finally: + # Hier ist ein kleiner Check, ob die connection überhaupt existiert + if 'connection' in locals() and connection: + cursor.close() + connection.close() + + + +def get_current_season_id(gamesystem_id): connection = db_connection() - cursor = connection.cursor() + cursor = connection.cursor(dictionary=True) query = """ - INSERT INTO player_game_statistic (player_id, gamesystem_id) - VALUES (%s, %s) + SELECT id FROM seasons + WHERE gamesystem_id = %s + ORDER BY start_date DESC + LIMIT 1 """ - logger.log(f"{get_player_name(player_id)} joined {gamesystem_id}") - cursor.execute(query, (player_id, gamesystem_id)) - connection.commit() + cursor.execute(query, (gamesystem_id,)) + result = cursor.fetchone() cursor.close() connection.close() + if result: + return result['id'] + else: + # Hier wissen wir jetzt genau, welches Spielsystem keine Saison hat + raise RuntimeError(f"Keine Saison für {gamesystem_id} gefunden!") + + # ----------------------------------------------------- # Matches: Lesen diff --git a/gui/main_gui.py b/gui/main_gui.py index 19052c4..bdb841e 100644 --- a/gui/main_gui.py +++ b/gui/main_gui.py @@ -244,8 +244,6 @@ def setup_routes(admin_discord_id): ui.label(text=f"MMR: {stat['mmr']}").classes("text-lg font-bold text-accenttext") ui.label(text=f"Spiele: {stat['games_in_system']}").classes("text-lg font-bold text-accenttext") - - # --------------------------- # Match Historie # ---------------------------