prepare(' SELECT id, username, score, lifetime_score, prestige_level, prestige_bonus, game_state, last_played, created_at FROM lemon_players ORDER BY lifetime_score DESC LIMIT 15 '); $stmt->execute(); $lignes = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $lignes = []; } // On calcule le nombre total de bâtiments possédés à partir du game_state (stocké en JSON) : // une info que le classement n'affichait pas jusqu'ici, et qui donne un vrai aperçu de // l'avancée d'un joueur au-delà du seul score $classement = array_map(function ($ligne) { $totalBatiments = 0; if (!empty($ligne['game_state'])) { $etat = json_decode($ligne['game_state'], true); if (is_array($etat) && !empty($etat['buildings']) && is_array($etat['buildings'])) { foreach ($etat['buildings'] as $possedes) { $totalBatiments += (int) $possedes; } } } return [ 'id' => (int) $ligne['id'], 'username' => $ligne['username'], 'score' => (float) $ligne['score'], 'lifetime_score' => (float) $ligne['lifetime_score'], 'prestige_level' => (int) $ligne['prestige_level'], 'prestige_bonus' => (float) $ligne['prestige_bonus'], 'total_buildings' => $totalBatiments, 'last_played' => $ligne['last_played'], 'created_at' => $ligne['created_at'], ]; }, $lignes); echo json_encode($classement);