From 8ae4f0989ae7100c1c99b5f93e6b02847177a61a Mon Sep 17 00:00:00 2001 From: aldo Date: Mon, 15 Dec 2025 22:59:56 +0700 Subject: [PATCH] leaderboard --- leaderboard.php | 306 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 248 insertions(+), 58 deletions(-) diff --git a/leaderboard.php b/leaderboard.php index df4f5e3..7c7e67b 100644 --- a/leaderboard.php +++ b/leaderboard.php @@ -1,67 +1,257 @@ query("SHOW TABLES LIKE 'leaderboard_sudoku'"); + if ($checkTable->rowCount() == 0) { + $createSql = " + CREATE TABLE leaderboard_sudoku ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50), + difficulty VARCHAR(10), + time_seconds INT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"; + $conn->exec($createSql); + } +} catch (PDOException $e) { + // Fail silently or log, but attempting to continue might show empty leaderboard instead of crash +} -$stmt = $conn->query($sql); -$data = $stmt->fetchAll(); +// Helper function to get top 10 by difficulty +function getLeaderboard($conn, $difficulty) { + $sql = " + SELECT username, time_seconds, created_at + FROM leaderboard_sudoku + WHERE difficulty = :difficulty + ORDER BY time_seconds ASC + LIMIT 10 + "; + $stmt = $conn->prepare($sql); + $stmt->execute(['difficulty' => $difficulty]); + return $stmt->fetchAll(PDO::FETCH_ASSOC); +} + +// Fetch data for each level +$easyData = getLeaderboard($conn, 'easy'); +$mediumData = getLeaderboard($conn, 'medium'); +$hardData = getLeaderboard($conn, 'hard'); ?> - + + + + + + Leaderboard Sudoku + + + + +

🏆 Leaderboard

+ +
+ +
+ + + +
+ + +
+

Level Mudah

+ +
+ + +
+

Level Sedang

+ +
+ + +
+

Level Sulit

+ +
+
+ + Kembali ke Game + + + + + + +Belum ada data.

"; + return; + } + + echo ""; + echo ""; + + $rank = 1; + foreach ($data as $row) { + $timeStr = sprintf("%02d:%02d", floor($row['time_seconds'] / 60), $row['time_seconds'] % 60); + + $rankClass = ''; + if ($rank == 1) $rankClass = 'rank-1'; + elseif ($rank == 2) $rankClass = 'rank-2'; + elseif ($rank == 3) $rankClass = 'rank-3'; + + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + $rank++; + } + echo "
RankPlayerWaktu
#{$rank}" . htmlspecialchars($row['username']) . "{$timeStr}
"; +} +?> \ No newline at end of file