feat: Erweitere Rangliste um visuelle Punkte- und Joker-Details

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
torsten 2026-10-04 05:58:19 +02:00
parent 9d040d10e2
commit e92c2c92c8
3 changed files with 111 additions and 66 deletions

View file

@ -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) {

View file

@ -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];
}
}

View file

@ -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;
}
?>
<style>
/* Styling für die Punkt-Zellen in der Rangliste */
.tipp-cell { text-align: center; }
.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 */
</style>
<h1>Rangliste</h1>
<h2>Saison: <?php echo htmlspecialchars($currentSeason['name']); ?></h2>
<?php if (!empty($ranking)): ?>
<table>
<thead>
<tr>
<th>Platz</th>
<th>Name</th>
<th>Punkte</th>
</tr>
</thead>
<tbody>
<?php foreach ($ranking as $entry): ?>
<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><?php echo $entry['score']; ?></td>
<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>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Für diese Saison gibt es noch keine Rangliste.</p>
<?php endif; ?>
</thead>
<tbody>
<?php foreach ($ranking as $entry): ?>
<tr>
<td><?php echo $entry['rank']; ?>.</td>
<td><?php echo htmlspecialchars($entry['name']); ?></td>
<td><strong><?php echo $entry['score']; ?></strong></td>
<?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>
<?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 require __DIR__ . '/partials/footer.php'; ?>