Fix critical player profile crash with encounter data

Bug Fix:
- Fixed 'int' object has no attribute 'split' error when viewing player profiles
- Issue was incorrect column mapping in encounter data SQL query
- species_id (integer) was being treated as first_encounter_date (string)

Technical Changes:
- Use existing database.get_player_encounters() method with proper row factory
- Use existing database.get_encounter_stats() method for consistency
- Added robust error handling for date formatting in both encounters and gym badges
- Added try/catch blocks to prevent profile crashes from data issues

Data Safety:
- Added isinstance() checks before calling .split() on date strings
- Graceful fallback to 'Unknown' for malformed dates
- Error handling ensures other users won't experience crashes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
megaproxy 2025-07-14 16:36:00 +01:00
parent 1ce7158200
commit 729984be66

View file

@ -1342,39 +1342,37 @@ class PetBotRequestHandler(BaseHTTPRequestHandler):
} }
gym_badges.append(badge_dict) gym_badges.append(badge_dict)
# Get player encounters # Get player encounters using database method
cursor = await db.execute("""
SELECT pe.*, ps.name as species_name, ps.type1, ps.type2, ps.rarity
FROM player_encounters pe
JOIN pet_species ps ON pe.species_id = ps.id
WHERE pe.player_id = ?
ORDER BY pe.first_encounter_date ASC
""", (player_dict['id'],))
encounters_rows = await cursor.fetchall()
encounters = [] encounters = []
for row in encounters_rows: try:
encounter_dict = { # Use the existing database method which handles row factory properly
'species_name': row[6], 'type1': row[7], 'type2': row[8], 'rarity': row[9], temp_encounters = await database.get_player_encounters(player_dict['id'])
'total_encounters': row[4], 'caught_count': row[5], 'first_encounter_date': row[2] for enc in temp_encounters:
} encounter_dict = {
encounters.append(encounter_dict) 'species_name': enc['species_name'],
'type1': enc['type1'],
'type2': enc['type2'],
'rarity': enc['rarity'],
'total_encounters': enc['total_encounters'],
'caught_count': enc['caught_count'],
'first_encounter_date': enc['first_encounter_date']
}
encounters.append(encounter_dict)
except Exception as e:
print(f"Error fetching encounters: {e}")
encounters = []
# Get encounter stats # Get encounter stats
cursor = await db.execute(""" try:
SELECT COUNT(*) as species_encountered, encounter_stats = await database.get_encounter_stats(player_dict['id'])
SUM(total_encounters) as total_encounters, except Exception as e:
(SELECT COUNT(*) FROM pet_species) as total_species print(f"Error fetching encounter stats: {e}")
FROM player_encounters encounter_stats = {
WHERE player_id = ? 'species_encountered': 0,
""", (player_dict['id'],)) 'total_encounters': 0,
stats_row = await cursor.fetchone() 'total_species': 0,
encounter_stats = { 'completion_percentage': 0.0
'species_encountered': stats_row[0] if stats_row[0] else 0, }
'total_encounters': stats_row[1] if stats_row[1] else 0,
'total_species': stats_row[2] if stats_row[2] else 0
}
completion_percentage = (encounter_stats['species_encountered'] / encounter_stats['total_species'] * 100) if encounter_stats['total_species'] > 0 else 0
encounter_stats['completion_percentage'] = round(completion_percentage, 1)
return { return {
'player': player_dict, 'player': player_dict,
@ -1634,7 +1632,14 @@ class PetBotRequestHandler(BaseHTTPRequestHandler):
badges_html = "" badges_html = ""
if gym_badges: if gym_badges:
for badge in gym_badges: for badge in gym_badges:
badge_date = badge['first_victory_date'].split()[0] if badge['first_victory_date'] else 'Unknown' # Safely handle date formatting
try:
if badge['first_victory_date'] and isinstance(badge['first_victory_date'], str):
badge_date = badge['first_victory_date'].split()[0]
else:
badge_date = 'Unknown'
except (AttributeError, IndexError):
badge_date = 'Unknown'
badges_html += f""" badges_html += f"""
<div style="background: var(--bg-tertiary); padding: 15px; border-radius: 8px; margin: 10px 0; border-left: 4px solid gold;"> <div style="background: var(--bg-tertiary); padding: 15px; border-radius: 8px; margin: 10px 0; border-left: 4px solid gold;">
<strong>{badge['badge_icon']} {badge['badge_name']}</strong><br> <strong>{badge['badge_icon']} {badge['badge_name']}</strong><br>
@ -1657,7 +1662,14 @@ class PetBotRequestHandler(BaseHTTPRequestHandler):
if encounter['type2']: if encounter['type2']:
type_str += f"/{encounter['type2']}" type_str += f"/{encounter['type2']}"
encounter_date = encounter['first_encounter_date'].split()[0] if encounter['first_encounter_date'] else 'Unknown' # Safely handle date formatting
try:
if encounter['first_encounter_date'] and isinstance(encounter['first_encounter_date'], str):
encounter_date = encounter['first_encounter_date'].split()[0]
else:
encounter_date = 'Unknown'
except (AttributeError, IndexError):
encounter_date = 'Unknown'
encounters_html += f""" encounters_html += f"""
<div style="background: var(--bg-tertiary); padding: 15px; border-radius: 8px; margin: 10px 0; border-left: 4px solid {rarity_color};"> <div style="background: var(--bg-tertiary); padding: 15px; border-radius: 8px; margin: 10px 0; border-left: 4px solid {rarity_color};">