feat: Implementiere Rangliste
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
parent
19e40998c2
commit
fdf47f5aa2
4 changed files with 157 additions and 1 deletions
103
src/Models/Ranking.php
Normal file
103
src/Models/Ranking.php
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
class Ranking {
|
||||
|
||||
/**
|
||||
* Berechnet die Rangliste für eine gegebene Saison.
|
||||
*
|
||||
* @param int $seasonId Die ID der Saison.
|
||||
* @return array Die sortierte Rangliste.
|
||||
*/
|
||||
public static function getRanking($seasonId) {
|
||||
$db = Database::getConnection();
|
||||
|
||||
// 1. Hole alle aktiven Benutzer
|
||||
$stmt_users = $db->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;
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,6 @@
|
|||
<a href="/">Start</a>
|
||||
<a href="#">Administration</a>
|
||||
<a href="#">Tipps</a>
|
||||
<a href="#">Rangliste</a>
|
||||
<a href="/index.php?page=ranking">Rangliste</a>
|
||||
<a href="#">Info</a>
|
||||
</nav>
|
||||
|
|
|
|||
38
src/Views/ranking.php
Normal file
38
src/Views/ranking.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
$pageTitle = 'Rangliste';
|
||||
require __DIR__ . '/partials/header.php';
|
||||
|
||||
// Stellt sicher, dass die benötigten Variablen vorhanden sind
|
||||
if (!isset($currentSeason) || !isset($ranking)) {
|
||||
echo "Fehler: Daten für die Rangliste nicht gefunden.";
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
<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): ?>
|
||||
<tr>
|
||||
<td><?php echo $entry['rank']; ?>.</td>
|
||||
<td><?php echo htmlspecialchars($entry['name']); ?></td>
|
||||
<td><?php echo $entry['score']; ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p>Für diese Saison gibt es noch keine Rangliste.</p>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php require __DIR__ . '/partials/footer.php'; ?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue