diff --git a/modules/battle_system.py b/modules/battle_system.py index d64783f..54c26b6 100644 --- a/modules/battle_system.py +++ b/modules/battle_system.py @@ -128,6 +128,7 @@ class BattleSystem(BaseModule): gym_battle = await self.database.get_active_gym_battle(player["id"]) if gym_battle: + print(f"DEBUG: Gym battle completion - player: {player['id']}, result: {result.get('winner')}") await self.handle_gym_battle_completion(channel, nickname, player, result, gym_battle) else: # Regular wild battle @@ -208,11 +209,18 @@ class BattleSystem(BaseModule): async def handle_gym_battle_completion(self, channel, nickname, player, battle_result, gym_battle): """Handle completion of a gym battle turn""" - if battle_result["winner"] == "player": - # Player won this individual battle - current_pet_index = gym_battle["current_pet_index"] - gym_team = gym_battle["gym_team"] - defeated_pet = gym_team[current_pet_index] + try: + if battle_result["winner"] == "player": + # Player won this individual battle + current_pet_index = gym_battle["current_pet_index"] + gym_team = gym_battle["gym_team"] + + # Safety check for index bounds + if current_pet_index >= len(gym_team): + self.send_message(channel, f"❌ {nickname}: Gym battle state error - please !forfeit and try again") + return + + defeated_pet = gym_team[current_pet_index] self.send_message(channel, f"🎉 {nickname}: You defeated {defeated_pet['species_name']}!") @@ -277,4 +285,10 @@ class BattleSystem(BaseModule): self.send_message(channel, f"{gym_battle['badge_icon']} {gym_battle['leader_name']}: \"Good battle! Train more and come back stronger!\"") self.send_message(channel, - f"💡 {nickname}: Try leveling up your pets or bringing items to heal during battle!") \ No newline at end of file + f"💡 {nickname}: Try leveling up your pets or bringing items to heal during battle!") + + except Exception as e: + self.send_message(channel, f"❌ {nickname}: Gym battle error occurred - please !forfeit and try again") + print(f"Gym battle completion error: {e}") + import traceback + traceback.print_exc() \ No newline at end of file diff --git a/src/database.py b/src/database.py index cbc9437..2d94f4c 100644 --- a/src/database.py +++ b/src/database.py @@ -1065,9 +1065,10 @@ class Database: async def end_gym_battle(self, player_id: int, victory: bool = False) -> Optional[Dict]: """End gym battle and return final status""" async with aiosqlite.connect(self.db_path) as db: + db.row_factory = aiosqlite.Row # Get battle info before ending it cursor = await db.execute(""" - SELECT agb.*, g.name as gym_name + SELECT agb.gym_id, agb.difficulty_level, g.name as gym_name FROM active_gym_battles agb JOIN gyms g ON agb.gym_id = g.id WHERE agb.player_id = ? AND agb.battle_status = 'active' @@ -1077,6 +1078,8 @@ class Database: if not battle: return None + battle_dict = dict(battle) + # End the battle await db.execute(""" UPDATE active_gym_battles @@ -1085,15 +1088,15 @@ class Database: """, (player_id,)) result = { - "gym_id": battle[2], - "gym_name": battle[9], - "difficulty_level": battle[3], + "gym_id": battle_dict["gym_id"], + "gym_name": battle_dict["gym_name"], + "difficulty_level": battle_dict["difficulty_level"], "victory": victory } # Record victory if successful if victory: - await self.record_gym_victory(player_id, battle[2]) + await self.record_gym_victory(player_id, battle_dict["gym_id"]) await db.commit() return result \ No newline at end of file