Fix gym battle completion error handling
- Fixed tuple index out of range error in end_gym_battle() - Added proper row factory and named column access in database queries - Added exception handling and bounds checking in gym battle completion - Added debug logging to track gym battle state issues - Improved error messages for gym battle failures Fixes: "tuple index out of range" error when gym battles complete 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
710ff5ac9c
commit
6053161b6e
2 changed files with 28 additions and 11 deletions
|
|
@ -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,10 +209,17 @@ class BattleSystem(BaseModule):
|
|||
|
||||
async def handle_gym_battle_completion(self, channel, nickname, player, battle_result, gym_battle):
|
||||
"""Handle completion of a gym battle turn"""
|
||||
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']}!")
|
||||
|
|
@ -278,3 +286,9 @@ class BattleSystem(BaseModule):
|
|||
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!")
|
||||
|
||||
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()
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue