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:
megaproxy 2025-07-14 13:11:10 +01:00
parent 710ff5ac9c
commit 6053161b6e
2 changed files with 28 additions and 11 deletions

View file

@ -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