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