diff --git a/public/index.php b/public/index.php index 8dc09d0..78ff964 100644 --- a/public/index.php +++ b/public/index.php @@ -48,7 +48,9 @@ if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { throw new \Exception("Keine aktuelle Saison gefunden."); } - $ranking = Ranking::getRanking($currentSeason['id']); + $rankingData = Ranking::getRanking($currentSeason['id']); + $ranking = $rankingData['ranking']; + $games = $rankingData['games']; require __DIR__ . '/../src/Views/ranking.php'; } catch (Throwable $e) { diff --git a/src/Models/Ranking.php b/src/Models/Ranking.php index 1cda6e9..c09ff6c 100644 --- a/src/Models/Ranking.php +++ b/src/Models/Ranking.php @@ -7,96 +7,101 @@ use App\Core\Database; class Ranking { /** - * Berechnet die Rangliste für eine gegebene Saison. + * Berechnet die detaillierte Rangliste für eine gegebene Saison. * * @param int $seasonId Die ID der Saison. - * @return array Die sortierte Rangliste. + * @return array Ein Array mit 'ranking' und 'games'. */ public static function getRanking($seasonId) { $db = Database::getConnection(); // 1. Hole alle Benutzer, die für diese Saison bezahlt haben - // Annahme: Es gibt eine `payments` Tabelle, die User- und Saison-ID speichert. $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); // [id => name] + $users = $stmt_users->fetchAll(\PDO::FETCH_KEY_PAIR); - // Wenn keine User teilnehmen, direkt eine leere Rangliste zurückgeben if (empty($users)) { - return []; + return ['ranking' => [], 'games' => []]; } + $user_ids = array_keys($users); - // 2. Hole alle beendeten Spiele der Saison + // 2. Hole ALLE Spiele der Saison $stmt_games = $db->prepare(" - SELECT g.id, g.score1, g.score2 + 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 = ? AND g.score1 > -1 AND g.score2 > -1 + WHERE r.season_id = ? ORDER BY g.kickoff ASC, g.id ASC "); $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]; + $ranking[] = ['rank' => 1, 'name' => $name, 'score' => 0, 'tipps' => []]; } - return $ranking; + return ['ranking' => $ranking, 'games' => []]; } - $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), '?')); + // 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 ($placeholders) + WHERE game_id IN ($game_placeholders) AND user_id IN ($user_placeholders) "); - $stmt_tipps->execute($game_ids); - $tipps = $stmt_tipps->fetchAll(\PDO::FETCH_ASSOC); + $stmt_tipps->execute(array_merge($game_ids, $user_ids)); + $all_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; + $tipps_map = []; + foreach ($all_tipps as $tipp) { + $tipps_map[$tipp['user_id']][$tipp['game_id']] = $tipp; } - // 5. Baue das Ranking zusammen und sortiere es + // 4. Berechne Punkte und Details für jeden User $rankingData = []; - foreach ($scores as $user_id => $score) { - $rankingData[] = [ - 'name' => $users[$user_id], - 'score' => $score - ]; + foreach ($users as $user_id => $name) { + $total_score = 0; + $tipps_details = []; + + foreach ($games as $game) { + $game_id = $game['id']; + $tipp = $tipps_map[$user_id][$game_id] ?? null; + + 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 = ''; + } + + $tipps_details[$game_id] = [ + 'text' => $tipp ? $tipp['tipp1'] . ':' . $tipp['tipp2'] : '-', + 'points' => $points, + 'color' => $color, + 'is_joker' => $tipp ? (bool)$tipp['joker'] : false, + ]; + } + $rankingData[] = ['name' => $name, 'score' => $total_score, 'tipps' => $tipps_details]; } - // Sortiere nach Punkten (absteigend), dann nach Name (aufsteigend) - usort($rankingData, function($a, $b) { + // 5. Sortiere nach Punkten, dann nach Name + 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) + // 6. Füge den Rang hinzu $rank = 0; $last_score = -1; $ranked_list = []; @@ -109,6 +114,6 @@ class Ranking { $ranked_list[] = $entry; } - return $ranked_list; + return ['ranking' => $ranked_list, 'games' => $games]; } } diff --git a/src/Views/ranking.php b/src/Views/ranking.php index 788cff3..ec1ea32 100644 --- a/src/Views/ranking.php +++ b/src/Views/ranking.php @@ -3,36 +3,74 @@ $pageTitle = 'Rangliste'; require __DIR__ . '/partials/header.php'; // Stellt sicher, dass die benötigten Variablen vorhanden sind -if (!isset($currentSeason) || !isset($ranking)) { +if (!isset($currentSeason) || !isset($ranking) || !isset($games)) { echo "Fehler: Daten für die Rangliste nicht gefunden."; exit; } ?> +
| Platz | -Name | -Punkte | -
|---|
| . | -- | + | Platz | +Name | +Punkte | + ++ + | +
|---|
Für diese Saison gibt es noch keine Rangliste.
- + + + +Für diese Saison gibt es noch keine Rangliste (möglicherweise haben noch keine Spieler bezahlt).
+ +