From f090babc604ba544376e6be7ed0fc0a70613181e Mon Sep 17 00:00:00 2001 From: Theouche Date: Thu, 12 Sep 2024 14:40:27 +0200 Subject: [PATCH] 10 try + raise error dans game.py --- pong/game/game.py | 59 ++++++++++++++++++++++++++++++++++++++-------- pong/game/utils.py | 1 + 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/pong/game/game.py b/pong/game/game.py index e17a8e7..a7ca65e 100644 --- a/pong/game/game.py +++ b/pong/game/game.py @@ -243,13 +243,52 @@ class Game: if not self.botgame: if not self.localgame: await self.player2.send(end_message) - if hasattr(self, 'tournament'): - print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} ENDED ***") - await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'], - self.game_state['player1_score'], self.game_state['player2_score'], - self.bt1, self.bt2, duration, True, self.tournament.tournoi_reg) - print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} is REGISTERED ***") - else: - await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'], - self.game_state['player1_score'], self.game_state['player2_score'], - self.bt1, self.bt2, duration, False, None) + + + + attempt = 0 + max_attempts = 10 # Limite le nombre de tentatives + success = False + + print(f"Try to save game #{self.game_id} ({self})") + while attempt < max_attempts and not success: + try: + if hasattr(self, 'tournament'): + print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} ENDED ***") + + # Essaye d'appeler handle_game_data + result = await sync_to_async(handle_game_data)( + self.game_state['player1_name'], self.game_state['player2_name'], + self.game_state['player1_score'], self.game_state['player2_score'], + self.bt1, self.bt2, duration, True, self.tournament.tournoi_reg + ) + + # Vérification explicite de la réussite + if result is not None: # Si handle_game_data peut retourner un résultat indicatif + success = True + print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} is REGISTERED ***") + else: + raise ValueError("handle_game_data returned an unexpected result") + + else: + print(f"*** Game #{self.game_id} simple game ENDED ***") + result = await sync_to_async(handle_game_data)( + self.game_state['player1_name'], self.game_state['player2_name'], + self.game_state['player1_score'], self.game_state['player2_score'], + self.bt1, self.bt2, duration, False, None + ) + + if result is not None: + success = True + print(f"Non-tournament game {self.game_id} data registered") + else: + raise ValueError("handle_game_data returned an unexpected result") + + except Exception as e: + attempt += 1 + print(f"Attempt {attempt}: Failed to register game data - {e}") + await asyncio.sleep(1) # Délai avant de réessayer + + if attempt >= max_attempts: + print("Max attempts reached. Could not register game data.") + break diff --git a/pong/game/utils.py b/pong/game/utils.py index 0f193d1..8113f7c 100644 --- a/pong/game/utils.py +++ b/pong/game/utils.py @@ -7,6 +7,7 @@ from channels.db import database_sync_to_async def handle_game_data(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament): try: + print("Here !!!!!!!!") player_1 = get_or_create_player(p1) player_2 = get_or_create_player(p2)