From 729984be668464507f681f648114e15dc8f87b41 Mon Sep 17 00:00:00 2001 From: megaproxy Date: Mon, 14 Jul 2025 16:36:00 +0100 Subject: [PATCH] Fix critical player profile crash with encounter data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- webserver.py | 76 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/webserver.py b/webserver.py index 7af62b2..2c646e2 100644 --- a/webserver.py +++ b/webserver.py @@ -1342,39 +1342,37 @@ class PetBotRequestHandler(BaseHTTPRequestHandler): } gym_badges.append(badge_dict) - # Get player encounters - 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() + # Get player encounters using database method encounters = [] - for row in encounters_rows: - encounter_dict = { - 'species_name': row[6], 'type1': row[7], 'type2': row[8], 'rarity': row[9], - 'total_encounters': row[4], 'caught_count': row[5], 'first_encounter_date': row[2] - } - encounters.append(encounter_dict) + try: + # Use the existing database method which handles row factory properly + temp_encounters = await database.get_player_encounters(player_dict['id']) + for enc in temp_encounters: + 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 - cursor = await db.execute(""" - SELECT COUNT(*) as species_encountered, - SUM(total_encounters) as total_encounters, - (SELECT COUNT(*) FROM pet_species) as total_species - FROM player_encounters - WHERE player_id = ? - """, (player_dict['id'],)) - stats_row = await cursor.fetchone() - encounter_stats = { - '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) + try: + encounter_stats = await database.get_encounter_stats(player_dict['id']) + except Exception as e: + print(f"Error fetching encounter stats: {e}") + encounter_stats = { + 'species_encountered': 0, + 'total_encounters': 0, + 'total_species': 0, + 'completion_percentage': 0.0 + } return { 'player': player_dict, @@ -1634,7 +1632,14 @@ class PetBotRequestHandler(BaseHTTPRequestHandler): badges_html = "" if 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"""
{badge['badge_icon']} {badge['badge_name']}
@@ -1657,7 +1662,14 @@ class PetBotRequestHandler(BaseHTTPRequestHandler): if 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"""