diff --git a/public/index.php b/public/index.php index 78ff964..de18632 100644 --- a/public/index.php +++ b/public/index.php @@ -48,9 +48,14 @@ if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { throw new \Exception("Keine aktuelle Saison gefunden."); } - $rankingData = Ranking::getRanking($currentSeason['id']); - $ranking = $rankingData['ranking']; - $games = $rankingData['games']; + $selectedRoundId = isset($_GET['round_id']) ? (int)$_GET['round_id'] : null; + + $data = Ranking::getRanking($currentSeason['id'], $selectedRoundId); + $ranking = $data['ranking']; + $games = $data['games']; + $allRounds = $data['allRounds']; + $selectedRound = $data['selectedRound']; + $dailyWinners = $data['dailyWinners']; require __DIR__ . '/../src/Views/ranking.php'; } catch (Throwable $e) { diff --git a/src/Models/Ranking.php b/src/Models/Ranking.php index c09ff6c..870971f 100644 --- a/src/Models/Ranking.php +++ b/src/Models/Ranking.php @@ -7,101 +7,122 @@ use App\Core\Database; class Ranking { /** - * Berechnet die detaillierte Rangliste für eine gegebene Saison. + * Berechnet die detaillierte Rangliste für eine gegebene Saison, gefiltert nach einer Runde. * * @param int $seasonId Die ID der Saison. - * @return array Ein Array mit 'ranking' und 'games'. + * @param int|null $selectedRoundId Die ID der anzuzeigenden Runde. + * @return array Ein Array mit allen benötigten Daten für die Ansicht. */ - public static function getRanking($seasonId) { + public static function getRanking($seasonId, $selectedRoundId = null) { $db = Database::getConnection(); // 1. Hole alle Benutzer, die für diese Saison bezahlt haben - $stmt_users = $db->prepare(" - SELECT u.id, u.name FROM users u - JOIN payments p ON u.id = p.user_id - WHERE p.season_id = ? - "); + $stmt_users = $db->prepare("SELECT u.id, u.name FROM users u JOIN payments p ON u.id = p.user_id WHERE p.season_id = ?"); $stmt_users->execute([$seasonId]); - $users = $stmt_users->fetchAll(\PDO::FETCH_KEY_PAIR); + $users = $stmt_users->fetchAll(\PDO::FETCH_ASSOC); if (empty($users)) { - return ['ranking' => [], 'games' => []]; + return ['ranking' => [], 'games' => [], 'allRounds' => [], 'selectedRound' => null, 'dailyWinners' => []]; } - $user_ids = array_keys($users); + $user_ids = array_column($users, 'id'); - // 2. Hole ALLE Spiele der Saison - $stmt_games = $db->prepare(" - SELECT g.id, g.team1, g.team2, g.score1, g.score2 - FROM games g - JOIN rounds r ON g.round_id = r.id - WHERE r.season_id = ? ORDER BY g.kickoff ASC, g.id ASC - "); - $stmt_games->execute([$seasonId]); - $games = $stmt_games->fetchAll(\PDO::FETCH_ASSOC); + // 2. Hole alle Runden der Saison für den Filter + $allRounds = Round::getBySeason($seasonId); - if (empty($games)) { - $ranking = []; - foreach ($users as $id => $name) { - $ranking[] = ['rank' => 1, 'name' => $name, 'score' => 0, 'tipps' => []]; - } - return ['ranking' => $ranking, 'games' => []]; + // 3. Bestimme die anzuzeigende Runde + if ($selectedRoundId) { + $selectedRound = Round::getById($selectedRoundId); + } else { + // Finde die letzte gespielte Runde + $stmt_last_round = $db->prepare("SELECT r.* FROM rounds r JOIN games g ON r.id = g.round_id WHERE r.season_id = ? AND g.score1 > -1 ORDER BY r.id DESC LIMIT 1"); + $stmt_last_round->execute([$seasonId]); + $selectedRound = $stmt_last_round->fetch(); + } + // Fallback: Erste Runde der Saison, wenn keine Runde gefunden wurde + if (!$selectedRound && !empty($allRounds)) { + $selectedRound = $allRounds[0]; + } + if (!$selectedRound) { + return ['ranking' => [], 'games' => [], 'allRounds' => $allRounds, 'selectedRound' => null, 'dailyWinners' => []]; } - $game_ids = array_column($games, 'id'); - // 3. Hole alle relevanten Tipps - $game_placeholders = implode(',', array_fill(0, count($game_ids), '?')); - $user_placeholders = implode(',', array_fill(0, count($user_ids), '?')); - $stmt_tipps = $db->prepare(" - SELECT user_id, game_id, score1 AS tipp1, score2 AS tipp2, joker - FROM tipps - WHERE game_id IN ($game_placeholders) AND user_id IN ($user_placeholders) - "); - $stmt_tipps->execute(array_merge($game_ids, $user_ids)); - $all_tipps = $stmt_tipps->fetchAll(\PDO::FETCH_ASSOC); + // 4. Hole die Spiele der ausgewählten Runde + $gamesOfSelectedRound = Game::getGamesByRound($selectedRound['id']); + // 5. Hole ALLE beendeten Spiele der Saison für die Gesamtpunktzahl + $stmt_finished_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"); + $stmt_finished_games->execute([$seasonId]); + $finished_games_all = $stmt_finished_games->fetchAll(\PDO::FETCH_ASSOC); + $finished_games_map = array_column($finished_games_all, null, 'id'); + + // 6. Hole alle relevanten Tipps $tipps_map = []; - foreach ($all_tipps as $tipp) { - $tipps_map[$tipp['user_id']][$tipp['game_id']] = $tipp; + if (!empty($finished_games_all)) { + $game_ids = array_column($finished_games_all, 'id'); + $game_placeholders = implode(',', array_fill(0, count($game_ids), '?')); + $user_placeholders = implode(',', array_fill(0, count($user_ids), '?')); + $stmt_tipps = $db->prepare("SELECT user_id, game_id, score1 AS tipp1, score2 AS tipp2, joker FROM tipps WHERE game_id IN ($game_placeholders) AND user_id IN ($user_placeholders)"); + $stmt_tipps->execute(array_merge($game_ids, $user_ids)); + foreach ($stmt_tipps->fetchAll(\PDO::FETCH_ASSOC) as $tipp) { + $tipps_map[$tipp['user_id']][$tipp['game_id']] = $tipp; + } } - // 4. Berechne Punkte und Details für jeden User + // 7. Berechne Punkte und Details für jeden User $rankingData = []; - foreach ($users as $user_id => $name) { + $round_scores = []; + foreach ($users as $user) { + $user_id = $user['id']; $total_score = 0; + $round_score = 0; $tipps_details = []; - foreach ($games as $game) { + // Gesamtpunkte über alle Spiele berechnen + foreach ($finished_games_map as $game_id => $game) { + if (isset($tipps_map[$user_id][$game_id])) { + $tipp = $tipps_map[$user_id][$game_id]; + $total_score += Tipp::getScoreLocal($game['score1'], $game['score2'], $tipp['tipp1'], $tipp['tipp2'], $tipp['joker']); + } + } + + // Details für die Spiele der ausgewählten Runde + foreach ($gamesOfSelectedRound as $game) { $game_id = $game['id']; $tipp = $tipps_map[$user_id][$game_id] ?? null; + $points = 0; + $color = ''; + $text = '-'; + $is_joker = false; - if ($game['score1'] > -1 && $tipp) { - $points = Tipp::getScoreLocal($game['score1'], $game['score2'], $tipp['tipp1'], $tipp['tipp2'], $tipp['joker']); - $color = Tipp::getCol($game['score1'], $game['score2'], $tipp['tipp1'], $tipp['tipp2'], $tipp['joker']); - $total_score += $points; - } else { - $points = 0; - $color = ''; + if (isset($finished_games_map[$game_id]) && $tipp) { + $finished_game = $finished_games_map[$game_id]; + $points = Tipp::getScoreLocal($finished_game['score1'], $finished_game['score2'], $tipp['tipp1'], $tipp['tipp2'], $tipp['joker']); + $color = Tipp::getCol($finished_game['score1'], $finished_game['score2'], $tipp['tipp1'], $tipp['tipp2'], $tipp['joker']); + $round_score += $points; } - - $tipps_details[$game_id] = [ - 'text' => $tipp ? $tipp['tipp1'] . ':' . $tipp['tipp2'] : '-', - 'points' => $points, - 'color' => $color, - 'is_joker' => $tipp ? (bool)$tipp['joker'] : false, - ]; + if ($tipp) { + $text = $tipp['tipp1'] . ':' . $tipp['tipp2']; + $is_joker = (bool)$tipp['joker']; + } + $tipps_details[$game_id] = compact('text', 'points', 'color', 'is_joker'); } - $rankingData[] = ['name' => $name, 'score' => $total_score, 'tipps' => $tipps_details]; + $rankingData[] = ['id' => $user_id, 'name' => $user['name'], 'score' => $total_score, 'round_score' => $round_score, 'tipps' => $tipps_details]; + $round_scores[$user_id] = $round_score; } - // 5. Sortiere nach Punkten, dann nach Name - usort($rankingData, function ($a, $b) { - if ($a['score'] == $b['score']) { - return strcmp($a['name'], $b['name']); + // 8. Tagessieger ermitteln + $dailyWinners = []; + if (!empty($round_scores)) { + $max_round_score = max($round_scores); + if ($max_round_score > 0) { + $dailyWinners = array_keys($round_scores, $max_round_score); } - return $b['score'] <=> $a['score']; - }); + } - // 6. Füge den Rang hinzu + // 9. Sortieren nach Gesamtpunkten + usort($rankingData, fn($a, $b) => $b['score'] <=> $a['score'] ?: strcmp($a['name'], $b['name'])); + + // 10. Rang hinzufügen $rank = 0; $last_score = -1; $ranked_list = []; @@ -114,6 +135,12 @@ class Ranking { $ranked_list[] = $entry; } - return ['ranking' => $ranked_list, 'games' => $games]; + return [ + 'ranking' => $ranked_list, + 'games' => $gamesOfSelectedRound, + 'allRounds' => $allRounds, + 'selectedRound' => $selectedRound, + 'dailyWinners' => $dailyWinners, + ]; } } diff --git a/src/Views/ranking.php b/src/Views/ranking.php index ec1ea32..65b7e36 100644 --- a/src/Views/ranking.php +++ b/src/Views/ranking.php @@ -1,76 +1,94 @@
| Platz | -Name | -Punkte | - -- - | - -
|---|
| . | -- | + | Platz | +Name | +🏆 | +Punkte (Gesamt) | +Punkte (Runde) | - -- - - - | +
+ + + () + |
|---|
Für diese Saison gibt es noch keine Rangliste (möglicherweise haben noch keine Spieler bezahlt).
- -Für diese Saison gibt es noch keine Rangliste (möglicherweise haben noch keine Spieler bezahlt).
+ +Keine Runde für die Anzeige ausgewählt oder gefunden.
+