feat: Rangliste: Rundenfilter, Tagessieger & Joker-Anzeige
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
parent
e92c2c92c8
commit
693ee86836
3 changed files with 170 additions and 120 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,76 +1,94 @@
|
|||
<?php
|
||||
$pageTitle = 'Rangliste';
|
||||
if ($selectedRound) {
|
||||
$pageTitle .= ' - ' . htmlspecialchars($selectedRound['name']);
|
||||
}
|
||||
require __DIR__ . '/partials/header.php';
|
||||
|
||||
// Stellt sicher, dass die benötigten Variablen vorhanden sind
|
||||
if (!isset($currentSeason) || !isset($ranking) || !isset($games)) {
|
||||
if (!isset($currentSeason) || !isset($ranking) || !isset($games) || !isset($allRounds) || !isset($selectedRound)) {
|
||||
echo "Fehler: Daten für die Rangliste nicht gefunden.";
|
||||
exit;
|
||||
// exit; // Besser, eine freundliche Nachricht im Layout anzuzeigen
|
||||
}
|
||||
?>
|
||||
<style>
|
||||
/* Styling für die Punkt-Zellen in der Rangliste */
|
||||
.tipp-cell { text-align: center; }
|
||||
.tipp-cell { text-align: center; position: relative; }
|
||||
.col_exact { background-color: #28a745 !important; color: white; } /* Exaktes Ergebnis */
|
||||
.col_diff { background-color: #ffc107 !important; } /* Richtige Tordifferenz */
|
||||
.col_tendenz { background-color: #17a2b8 !important; color: white; } /* Richtige Tendenz */
|
||||
.joker-tipp { border: 2px solid #007bff; } /* Blauer Rahmen für Joker */
|
||||
|
||||
/* Damit die Tabelle auf kleineren Bildschirmen scrollbar wird */
|
||||
.table-container {
|
||||
width: 100%;
|
||||
overflow-x: auto;
|
||||
margin-top: 20px;
|
||||
}
|
||||
th.vertical-text {
|
||||
writing-mode: vertical-rl;
|
||||
transform: rotate(180deg);
|
||||
white-space: nowrap;
|
||||
text-align: left;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
table { width: auto; } /* Erlaubt der Tabelle, breiter als der Container zu sein */
|
||||
.round-filter { margin-bottom: 20px; }
|
||||
.joker-icon { font-size: 0.7em; vertical-align: top; }
|
||||
th.game-header { white-space: normal; } /* Erlaubt Umbruch im Header */
|
||||
.table-container { width: 100%; overflow-x: auto; margin-top: 20px; }
|
||||
</style>
|
||||
|
||||
<h1>Rangliste</h1>
|
||||
<h2>Saison: <?php echo htmlspecialchars($currentSeason['name']); ?></h2>
|
||||
|
||||
<div class="table-container">
|
||||
<?php if (!empty($ranking)): ?>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Platz</th>
|
||||
<th>Name</th>
|
||||
<th>Punkte</th>
|
||||
<?php foreach ($games as $game): ?>
|
||||
<th class="vertical-text" title="<?php echo htmlspecialchars($game['team1'] . ' vs ' . $game['team2']); ?>">
|
||||
<span><?php echo htmlspecialchars($game['team1'] . ' - ' . $game['team2']); ?></span>
|
||||
</th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($ranking as $entry): ?>
|
||||
<?php if ($selectedRound): ?>
|
||||
<div class="round-filter">
|
||||
<form method="get">
|
||||
<input type="hidden" name="page" value="ranking">
|
||||
<label for="round_id">Runde auswählen:</label>
|
||||
<select name="round_id" id="round_id" onchange="this.form.submit()">
|
||||
<?php foreach ($allRounds as $round): ?>
|
||||
<option value="<?php echo $round['id']; ?>" <?php echo ($round['id'] == $selectedRound['id']) ? 'selected' : ''; ?>>
|
||||
<?php echo htmlspecialchars($round['name']); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="table-container">
|
||||
<?php if (!empty($ranking)): ?>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td><?php echo $entry['rank']; ?>.</td>
|
||||
<td><?php echo htmlspecialchars($entry['name']); ?></td>
|
||||
<td><strong><?php echo $entry['score']; ?></strong></td>
|
||||
<th>Platz</th>
|
||||
<th>Name</th>
|
||||
<th>🏆</th>
|
||||
<th>Punkte (Gesamt)</th>
|
||||
<th>Punkte (Runde)</th>
|
||||
<?php foreach ($games as $game): ?>
|
||||
<?php $tipp = $entry['tipps'][$game['id']]; ?>
|
||||
<td class="tipp-cell <?php echo htmlspecialchars($tipp['color']); ?> <?php echo $tipp['is_joker'] ? 'joker-tipp' : ''; ?>">
|
||||
<span title="Dein Tipp: <?php echo htmlspecialchars($tipp['text']); ?><?php echo $tipp['is_joker'] ? ' (Joker)' : ''; ?>">
|
||||
<?php echo $tipp['points']; ?>
|
||||
</span>
|
||||
</td>
|
||||
<th class="game-header">
|
||||
<?php echo htmlspecialchars($game['team1']); ?><br>
|
||||
<?php echo htmlspecialchars($game['team2']); ?><br>
|
||||
<small>(<?php echo App\Models\Game::getResultString($game['score1'], $game['score2'], $game['kickoff']); ?>)</small>
|
||||
</th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p>Für diese Saison gibt es noch keine Rangliste (möglicherweise haben noch keine Spieler bezahlt).</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($ranking as $entry): ?>
|
||||
<tr>
|
||||
<td><?php echo $entry['rank']; ?>.</td>
|
||||
<td><?php echo htmlspecialchars($entry['name']); ?></td>
|
||||
<td><?php echo in_array($entry['id'], $dailyWinners) ? '🏆' : ''; ?></td>
|
||||
<td><strong><?php echo $entry['score']; ?></strong></td>
|
||||
<td><?php echo $entry['round_score']; ?></td>
|
||||
<?php foreach ($games as $game): ?>
|
||||
<?php $tipp = $entry['tipps'][$game['id']]; ?>
|
||||
<td class="tipp-cell <?php echo htmlspecialchars($tipp['color']); ?>">
|
||||
<span title="Dein Tipp: <?php echo htmlspecialchars($tipp['text']); ?>">
|
||||
<?php echo $tipp['points']; ?>
|
||||
</span>
|
||||
<?php if ($tipp['is_joker']): ?>
|
||||
<span class="joker-icon" title="Joker">🃏</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p>Für diese Saison gibt es noch keine Rangliste (möglicherweise haben noch keine Spieler bezahlt).</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<p>Keine Runde für die Anzeige ausgewählt oder gefunden.</p>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php require __DIR__ . '/partials/footer.php'; ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue