34464-vm/leaderboard.php
2025-09-28 23:02:21 +00:00

98 lines
3.4 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Gatekeeper: redirect if not logged in
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$pdo = db();
$stmt = $pdo->query("
SELECT
s.score,
s.time_taken,
s.moves,
s.completed_at,
u.username,
p.name as puzzle_name
FROM scores s
JOIN users u ON s.user_id = u.id
JOIN puzzles p ON s.puzzle_id = p.id
ORDER BY s.score DESC
LIMIT 100
");
$scores = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Classifica</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="index.php">Puzzle Game</a>
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Gioca</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="leaderboard.php">Classifica</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</nav>
<div class="container mt-5">
<h1 class="mb-4 text-center">Classifica Generale</h1>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Utente</th>
<th>Puzzle</th>
<th>Punteggio</th>
<th>Tempo</th>
<th>Mosse</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<?php if (empty($scores)): ?>
<tr>
<td colspan="7" class="text-center">Nessun punteggio registrato.</td>
</tr>
<?php else: ?>
<?php foreach ($scores as $index => $score): ?>
<tr>
<td><?php echo $index + 1; ?></td>
<td><?php echo htmlspecialchars($score['username']); ?></td>
<td><?php echo htmlspecialchars($score['puzzle_name']); ?></td>
<td><?php echo htmlspecialchars($score['score']); ?></td>
<td><?php echo htmlspecialchars($score['time_taken']); ?>s</td>
<td><?php echo htmlspecialchars($score['moves']); ?></td>
<td><?php echo htmlspecialchars(date("d/m/Y H:i", strtotime($score['completed_at']))); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<footer class="text-center mt-5 py-3 bg-light">
<p>&copy; <?php echo date("Y"); ?> Puzzle Game</p>
</footer>
</body>
</html>