feat: port legacy season, tipp scoring and user logic to models

Co-authored-by: aider (anthropic/claude-opus-5-5) <aider@aider.chat>
This commit is contained in:
torsten 2026-10-04 20:57:52 +02:00
parent f00770f73f
commit 7ecd38cb20
3 changed files with 630 additions and 93 deletions

View file

@ -5,36 +5,32 @@ namespace App\Models;
use App\Core\Database;
class Season {
/** Legacy-Default für seasons.max_identical_jokertipps */
const DEFAULT_MAX_IDENTICAL_JOKERTIPPS = 6;
/** Jede Runde besteht aus 4 Spielen (Legacy-Konvention) */
const GAMES_PER_ROUND = 4;
/**
* Holt die aktuell selektierte Saison aus den `settings`.
* Annahme: Es gibt eine Tabelle `settings` mit dem Feld `current_season_id`.
* Holt die aktuell selektierte Saison (settings.current_season_id).
* Fallback: Die Saison mit der höchsten ID.
*/
public static function getCurrent() {
$db = Database::getConnection();
// Lese die current_season_id aus der settings Tabelle
$stmt = $db->prepare("SELECT current_season_id FROM settings LIMIT 1");
$stmt->execute();
$setting = $stmt->fetch();
if ($setting && !empty($setting['current_season_id'])) {
$seasonId = $setting['current_season_id'];
// Hole die Saison mit der entsprechenden ID
$stmt = $db->prepare("SELECT * FROM seasons WHERE id = ?");
$stmt->execute([$seasonId]);
$season = $stmt->fetch();
if ($season) {
return $season;
}
$stmt = $db->query(
"SELECT s.* FROM settings st
JOIN seasons s ON s.id = st.current_season_id
ORDER BY st.id
LIMIT 1"
);
$season = $stmt->fetch();
if ($season) {
return $season;
}
// Fallback: Wenn keine Saison in den Settings gefunden wird oder die ID ungültig ist, nimm die mit der höchsten ID
$stmt = $db->prepare("SELECT * FROM seasons ORDER BY id DESC LIMIT 1");
$stmt->execute();
$stmt = $db->query("SELECT * FROM seasons ORDER BY id DESC LIMIT 1");
return $stmt->fetch();
}
@ -64,79 +60,117 @@ class Season {
}
/**
* Erstellt eine neue Saison und die zugehörigen Runden.
* Erstellt eine neue Saison mit Platzhalter-Runden und je 4 Platzhalter-Spielen
* (wie im Legacy SeasonsController::store). Die Platzhalter-Spiele liegen 7 Monate
* in der Zukunft, damit die Runden nicht gesperrt sind, bis der Admin die echten
* Paarungen und Anstoßzeiten einträgt.
*
* @param array $data
* Tipps werden NICHT vorab angelegt - sie entstehen beim ersten Tippen (siehe Tipp::saveRound).
*
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps']
* @return int|false Die ID der neuen Saison oder false bei einem Fehler.
*/
public static function create($data) {
$db = Database::getConnection();
$roundCount = (int)$data['rounds'];
$maxJoker = (int)($data['max_identical_jokertipps'] ?? self::DEFAULT_MAX_IDENTICAL_JOKERTIPPS);
$db->beginTransaction();
try {
$stmt = $db->prepare(
"INSERT INTO seasons (name, rounds, created_at, updated_at) VALUES (:name, :rounds, NOW(), NOW())"
"INSERT INTO seasons (name, rounds, max_identical_jokertipps, created_at, updated_at)
VALUES (:name, :rounds, :max_joker, NOW(), NOW())"
);
$stmt->execute([
':name' => $data['name'],
':rounds' => $data['rounds'],
':rounds' => $roundCount,
':max_joker' => $maxJoker,
]);
$seasonId = $db->lastInsertId();
$seasonId = (int)$db->lastInsertId();
for ($i = 1; $i <= $data['rounds']; $i++) {
$roundName = $i . ". Spieltag";
$stmt_round = $db->prepare(
"INSERT INTO rounds (name, season_id, created_at, updated_at) VALUES (?, ?, NOW(), NOW())"
);
$stmt_round->execute([$roundName, $seasonId]);
$placeholderKickoff = (new \DateTime('+7 months'))->format('Y-m-d H:i:s');
$stmtRound = $db->prepare(
"INSERT INTO rounds (name, season_id, created_at, updated_at) VALUES (?, ?, NOW(), NOW())"
);
$stmtGame = $db->prepare(
"INSERT INTO games (round_id, team1, team2, kickoff, created_at, updated_at)
VALUES (?, ?, ?, ?, NOW(), NOW())"
);
for ($i = 1; $i <= $roundCount; $i++) {
$stmtRound->execute([$i . ". Spieltag", $seasonId]);
$roundId = (int)$db->lastInsertId();
for ($g = 1; $g <= self::GAMES_PER_ROUND; $g++) {
$stmtGame->execute([
$roundId,
"Mannschaft " . (2 * $g - 1),
"Mannschaft " . (2 * $g),
$placeholderKickoff,
]);
}
}
$db->commit();
return (int)$seasonId;
return $seasonId;
} catch (\Exception $e) {
$db->rollBack();
error_log("Season::create fehlgeschlagen: " . $e->getMessage());
return false;
}
}
/**
* Aktualisiert eine Saison.
* Hinweis: Eine Änderung von 'rounds' legt keine Runden an oder löscht welche.
*
* @param int $id
* @param array $data
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps']
* @return bool
*/
public static function update($id, $data) {
$db = Database::getConnection();
$stmt = $db->prepare(
"UPDATE seasons SET name = :name, rounds = :rounds, updated_at = NOW() WHERE id = :id"
);
return $stmt->execute([
$sql = "UPDATE seasons SET name = :name, rounds = :rounds, updated_at = NOW()";
$params = [
':id' => $id,
':name' => $data['name'],
':rounds' => $data['rounds'],
]);
':rounds' => (int)$data['rounds'],
];
if (isset($data['max_identical_jokertipps'])) {
$sql .= ", max_identical_jokertipps = :max_joker";
$params[':max_joker'] = (int)$data['max_identical_jokertipps'];
}
$sql .= " WHERE id = :id";
$stmt = $db->prepare($sql);
return $stmt->execute($params);
}
/**
* Setzt die aktive Saison in der `settings`-Tabelle.
* Es wird (wie im Legacy-Code) immer die erste Zeile von settings verwendet.
*
* @param int $id
* @return bool
*/
public static function setCurrent($id) {
$db = Database::getConnection();
// Annahme: Es gibt immer genau einen Eintrag in settings
$stmt = $db->prepare("UPDATE settings SET current_season_id = ? WHERE id = 1");
if (!$stmt->execute([$id])) {
return false;
}
// Wenn die Zeile nicht existiert, füge sie ein.
if ($stmt->rowCount() === 0) {
$stmt = $db->prepare("INSERT INTO settings (id, current_season_id) VALUES (1, ?)");
$settingsId = $db->query("SELECT id FROM settings ORDER BY id LIMIT 1")->fetchColumn();
if ($settingsId === false) {
$stmt = $db->prepare(
"INSERT INTO settings (current_season_id, created_at, updated_at) VALUES (?, NOW(), NOW())"
);
return $stmt->execute([$id]);
}
return true;
$stmt = $db->prepare("UPDATE settings SET current_season_id = ?, updated_at = NOW() WHERE id = ?");
return $stmt->execute([$id, $settingsId]);
}
}