diff --git a/public/index.php b/public/index.php index b72041b..8dc09d0 100644 --- a/public/index.php +++ b/public/index.php @@ -8,6 +8,7 @@ use App\Models\Game; use App\Models\Tipp; use App\Models\Round; use App\Models\Season; +use App\Models\Ranking; $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); @@ -40,6 +41,20 @@ if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { http_response_code(500); echo "
Fehler: " . htmlspecialchars($e->getMessage()) . "
"; } +} elseif (($_GET['page'] ?? null) === 'ranking') { + try { + $currentSeason = Season::getCurrent(); + if (!$currentSeason) { + throw new \Exception("Keine aktuelle Saison gefunden."); + } + + $ranking = Ranking::getRanking($currentSeason['id']); + + require __DIR__ . '/../src/Views/ranking.php'; + } catch (Throwable $e) { + http_response_code(500); + echo "Fehler: " . htmlspecialchars($e->getMessage()) . "
"; + } } elseif ($uri === '/' || $uri === '/index.php') { try { $db = Database::getConnection(); diff --git a/src/Models/Ranking.php b/src/Models/Ranking.php new file mode 100644 index 0000000..28ea436 --- /dev/null +++ b/src/Models/Ranking.php @@ -0,0 +1,103 @@ +query("SELECT id, name FROM users WHERE is_active = 1"); + $users = $stmt_users->fetchAll(\PDO::FETCH_KEY_PAIR); // [id => name] + + // 2. Hole alle beendeten Spiele der Saison + $stmt_games = $db->prepare(" + SELECT g.id, g.score1, g.score2 + FROM games g + JOIN rounds r ON g.round_id = r.id + WHERE r.season_id = ? AND g.score1 > -1 AND g.score2 > -1 + "); + $stmt_games->execute([$seasonId]); + $games = $stmt_games->fetchAll(\PDO::FETCH_ASSOC); + + // Wenn keine Spiele beendet sind, haben alle 0 Punkte + if (empty($games)) { + $ranking = []; + $rank = 1; + foreach ($users as $id => $name) { + $ranking[] = ['rank' => $rank, 'name' => $name, 'score' => 0]; + } + return $ranking; + } + + $game_ids = array_column($games, 'id'); + $games_by_id = array_combine($game_ids, $games); + + // 3. Hole alle Tipps für diese Spiele + $placeholders = implode(',', array_fill(0, count($game_ids), '?')); + $stmt_tipps = $db->prepare(" + SELECT user_id, game_id, tipp1, tipp2, joker + FROM tipps + WHERE game_id IN ($placeholders) + "); + $stmt_tipps->execute($game_ids); + $tipps = $stmt_tipps->fetchAll(\PDO::FETCH_ASSOC); + + // 4. Berechne die Punkte pro Benutzer + $scores = array_fill_keys(array_keys($users), 0); + + foreach ($tipps as $tipp) { + $user_id = $tipp['user_id']; + if (!isset($scores[$user_id])) continue; + + $game = $games_by_id[$tipp['game_id']]; + $points = Tipp::getScoreLocal( + $game['score1'], $game['score2'], + $tipp['tipp1'], $tipp['tipp2'], + $tipp['joker'] + ); + $scores[$user_id] += $points; + } + + // 5. Baue das Ranking zusammen und sortiere es + $rankingData = []; + foreach ($scores as $user_id => $score) { + $rankingData[] = [ + 'name' => $users[$user_id], + 'score' => $score + ]; + } + + // Sortiere nach Punkten (absteigend), dann nach Name (aufsteigend) + usort($rankingData, function($a, $b) { + if ($a['score'] == $b['score']) { + return strcmp($a['name'], $b['name']); + } + return $b['score'] <=> $a['score']; + }); + + // 6. Füge den Rang hinzu (behandelt gleiche Plätze) + $rank = 0; + $last_score = -1; + $ranked_list = []; + foreach ($rankingData as $i => $entry) { + if ($entry['score'] !== $last_score) { + $rank = $i + 1; + $last_score = $entry['score']; + } + $entry['rank'] = $rank; + $ranked_list[] = $entry; + } + + return $ranked_list; + } +} diff --git a/src/Views/partials/header.php b/src/Views/partials/header.php index ddc1c84..f91bce6 100644 --- a/src/Views/partials/header.php +++ b/src/Views/partials/header.php @@ -19,6 +19,6 @@ Start Administration Tipps - Rangliste + Rangliste Info diff --git a/src/Views/ranking.php b/src/Views/ranking.php new file mode 100644 index 0000000..788cff3 --- /dev/null +++ b/src/Views/ranking.php @@ -0,0 +1,38 @@ + + +| Platz | +Name | +Punkte | +
|---|---|---|
| . | ++ | + |
Für diese Saison gibt es noch keine Rangliste.
+ + +