tippspiel2027/tippspiel/src/Models/Round.php
2026-10-27 09:38:05 +01:00

179 lines
5.4 KiB
PHP

<?php
namespace App\Models;
use App\Core\Database;
class Round {
/**
* Holt eine Runde anhand der ID
*/
public static function getById($id) {
$db = Database::getConnection();
$stmt = $db->prepare("SELECT * FROM rounds WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
/**
* Holt alle Runden einer Season
*/
public static function getBySeason($seasonId) {
$db = Database::getConnection();
$stmt = $db->prepare("SELECT * FROM rounds WHERE season_id = ? ORDER BY id ASC");
$stmt->execute([$seasonId]);
return $stmt->fetchAll();
}
/**
* Aktualisiert den Namen einer Runde.
*
* @param int $id
* @param string $name
* @return bool
*/
public static function updateName($id, $name) {
$db = Database::getConnection();
$stmt = $db->prepare("UPDATE rounds SET name = ?, updated_at = NOW() WHERE id = ?");
return $stmt->execute([$name, $id]);
}
/**
* Speichert den Rundenbericht. Zeilenumbrüche werden wie im Legacy als <br/> gespeichert.
*
* @param int $id
* @param string $report Klartext mit Zeilenumbrüchen
* @return bool
*/
public static function updateReport($id, $report) {
$report = str_replace(["\r\n", "\r", "\n"], "<br/>", trim($report));
$db = Database::getConnection();
$stmt = $db->prepare("UPDATE rounds SET report = ?, updated_at = NOW() WHERE id = ?");
return $stmt->execute([$report, $id]);
}
/**
* Rundenbericht als Klartext für Formulare (Gegenstück zu updateReport).
*
* @param string|null $report
* @return string
*/
public static function reportToText($report) {
return str_replace(["<br/>", "<br>", "<br />"], "\n", (string)$report);
}
/**
* Sperrzeitpunkt der Runde: 1 Minute vor dem frühesten Anstoß (Legacy Round::getLockTime).
*
* @param array $games Spiele der Runde
* @return \DateTime|null
*/
public static function getLockTime(array $games) {
if (empty($games)) {
return null;
}
$earliest = min(array_column($games, 'kickoff'));
return (new \DateTime($earliest))->modify('-1 minute');
}
/**
* Eine Runde ist abgeschlossen, wenn alle Spiele ein Ergebnis haben (Legacy Round::isCompleted).
*
* @param array $games Spiele der Runde
* @return bool
*/
public static function isCompleted(array $games) {
foreach ($games as $game) {
if ((int)$game['score1'] < 0 || (int)$game['score2'] < 0) {
return false;
}
}
return true;
}
/**
* Eine Runde ist gesperrt, sobald eines ihrer Spiele gesperrt ist (Legacy Round::hasLockedGame).
*
* @param array $games Spiele der Runde
* @return bool
*/
public static function isLocked(array $games) {
foreach ($games as $game) {
if (Game::isLocked($game['kickoff'])) {
return true;
}
}
return false;
}
/**
* Erste Runde der Saison, in der noch kein Spiel gesperrt ist (Legacy Round::getFirstTippableRound).
*
* @param int $seasonId
* @return array|null Runde inkl. 'games'
*/
public static function getFirstTippable($seasonId) {
foreach (self::getBySeason($seasonId) as $round) {
$games = Game::getGamesByRound($round['id']);
if (!self::isLocked($games)) {
$round['games'] = $games;
return $round;
}
}
return null;
}
/** Ab dieser Restzeit erscheint der Hinweis auf der Startseite (Legacy: 49 Stunden). */
const LOCK_WARNING_HOURS = 49;
/**
* Hinweis für die Startseite: fehlende Tipps/Joker und Sperrzeitpunkt der
* nächsten tippbaren Runde (Legacy: UsersController::main und User::hasMissing*).
*
* @param int $seasonId
* @param int $userId
* @return array|null Null, wenn kein Hinweis angezeigt werden soll
*/
public static function getLockWarning($seasonId, $userId) {
$round = self::getFirstTippable($seasonId);
if (!$round || empty($round['games'])) {
return null;
}
$lockTime = self::getLockTime($round['games']);
if (!$lockTime) {
return null;
}
$secondsLeft = $lockTime->getTimestamp() - time();
if ($secondsLeft <= 0 || $secondsLeft >= self::LOCK_WARNING_HOURS * 3600) {
return null;
}
$missing = Tipp::getMissing((int)$round['id'], [(int)$userId]);
return [
'name' => (string)$round['name'],
'lock_time' => $lockTime,
'remaining' => self::formatRemainingTime($secondsLeft),
'missing_tipps' => in_array((int)$userId, $missing['tipps'], true),
'missing_joker' => in_array((int)$userId, $missing['joker'], true),
];
}
/**
* Formatiert die Restzeit wie im Legacy ("8 Stunden", "12 Minuten").
*
* @param int $seconds
* @return string
*/
private static function formatRemainingTime($seconds) {
if ($seconds >= 3600) {
$hours = (int)floor($seconds / 3600);
return $hours . ' ' . ($hours === 1 ? 'Stunde' : 'Stunden');
}
$minutes = max(1, (int)floor($seconds / 60));
return $minutes . ' ' . ($minutes === 1 ? 'Minute' : 'Minuten');
}
}