This commit is contained in:
torsten 2026-10-03 16:27:22 +02:00
parent 366935846c
commit 4bf3bbb978
8 changed files with 348 additions and 2 deletions

39
src/Core/Database.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace App\Core;
use PDO;
use PDOException;
class Database {
private static $instance = null;
private $pdo;
private function __construct() {
$host = getenv('DB_HOST') ?: '127.0.0.1';
$user = getenv('DB_USER') ?: 'root';
$pass = getenv('DB_PASS') ?: '';
$dbName = getenv('DB_NEW_NAME') ?: 'tippspiel2027';
$dsn = "mysql:host=$host;dbname=$dbName;charset=utf8mb4";
try {
$this->pdo = new PDO($dsn, $user, $pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Datenbankverbindung fehlgeschlagen: " . $e->getMessage());
}
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public static function getConnection() {
return self::getInstance()->pdo;
}
}

57
src/Models/Game.php Normal file
View file

@ -0,0 +1,57 @@
<?php
namespace App\Models;
use App\Core\Database;
use DateTime;
use DateInterval;
class Game {
/**
* Prüft ob ein Spiel bereits gelockt (Anpfiff steht kurz bevor) ist.
* Portiert aus dem Laravel Game Modell: Lock war 1 Minute vor Anpfiff.
*/
public static function isLocked($kickoffTime) {
$now = new DateTime();
$kickoff = new DateTime($kickoffTime);
// 1 Minute vor Anpfiff wird gelockt
$kickoff->sub(new DateInterval('PT1M'));
return $now >= $kickoff;
}
/**
* Gibt einen formatierten String für das Ergebnis eines Spiels zurück
*/
public static function getResultString($score1, $score2, $kickoffTime) {
if ($score1 > -1 && $score2 > -1) {
return "$score1 : $score2";
}
$now = new DateTime();
$start = new DateTime($kickoffTime);
$end = (clone $start)->add(new DateInterval('PT2H')); // ca. 2 Stunden nach Anpfiff
if ($start > $now) {
return "-";
}
if ($now > $start && $now < $end) {
return "Läuft";
}
return "(Eingeben)";
}
/**
* Holt alle Spiele einer Runde aus der Datenbank
*/
public static function getGamesByRound($roundId) {
$db = Database::getConnection();
$stmt = $db->prepare("SELECT * FROM games WHERE round_id = ? ORDER BY kickoff ASC");
$stmt->execute([$roundId]);
return $stmt->fetchAll();
}
}

28
src/Models/Round.php Normal file
View file

@ -0,0 +1,28 @@
<?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();
}
}

83
src/Models/Tipp.php Normal file
View file

@ -0,0 +1,83 @@
<?php
namespace App\Models;
class Tipp {
/**
* Berechnet die Punkte für einen Tipp im Vergleich zum echten Ergebnis.
* Portiert aus der alten Laravel-Anwendung.
*
* @param int $goals1 Tore Team 1
* @param int $goals2 Tore Team 2
* @param int $tipp1 Getippte Tore Team 1
* @param int $tipp2 Getippte Tore Team 2
* @param bool $joker Ob der Joker für diesen Tipp gesetzt wurde
* @return int Die erreichten Punkte
*/
public static function getScoreLocal($goals1, $goals2, $tipp1, $tipp2, $joker) {
$score = 0;
// Wenn noch kein Ergebnis vorliegt (-1 bedeutet in der DB meistens noch nicht gespielt)
if ($tipp1 == -1 || $tipp2 == -1 || $goals1 == -1 || $goals2 == -1) {
return 0;
}
$tippTendenz = $tipp1 - $tipp2;
if ($tippTendenz > 0) { $tippTendenz = 1; }
if ($tippTendenz < 0) { $tippTendenz = -1; }
$gameTendenz = $goals1 - $goals2;
if ($gameTendenz < 0) { $gameTendenz = -1; }
if ($gameTendenz > 0) { $gameTendenz = 1; }
// Tendenz richtig
if ($tippTendenz == $gameTendenz) {
$score = 1;
}
// Genaues Ergebnis richtig (gibt 2 Grundpunkte + Bonus)
if (($tipp1 == $goals1) && ($tipp2 == $goals2)) {
$bonus = max(0, $goals1 + $goals2 - 3);
$score = 2 + $bonus;
}
// Joker verdoppelt die Punktzahl
if ($joker == true) {
$score = $score * 2;
}
return $score;
}
/**
* Gibt das Styling/die Farbe für die Tendenz zurück (fürs Frontend)
*/
public static function getCol($goals1, $goals2, $tipp1, $tipp2, $joker) {
if ($tipp1 == -1 || $tipp2 == -1 || $goals1 == -1 || $goals2 == -1) {
return "";
}
$tippTendenz = $tipp1 - $tipp2;
if ($tippTendenz > 0) { $tippTendenz = 1; }
if ($tippTendenz < 0) { $tippTendenz = -1; }
$gameTendenz = $goals1 - $goals2;
if ($gameTendenz < 0) { $gameTendenz = -1; }
if ($gameTendenz > 0) { $gameTendenz = 1; }
if ($tippTendenz != $gameTendenz) {
return "";
}
$styling = 'background-color:grey;';
if (($tipp1 == $goals1) && ($tipp2 == $goals2)) {
$styling = 'background-color:orange;';
if ($joker) {
$styling = 'background-color:red';
}
}
return $styling;
}
}

26
src/autoload.php Normal file
View file

@ -0,0 +1,26 @@
<?php
spl_autoload_register(function ($class) {
// Projekt-Namespace Prefix
$prefix = 'App\\';
// Basisverzeichnis für den Namespace Prefix
$base_dir = __DIR__ . '/';
// Prüfen ob die Klasse das Prefix verwendet
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return; // Gehört nicht zu diesem Autoloader
}
// Den relativen Klassennamen extrahieren
$relative_class = substr($class, $len);
// Namespace-Trenner (\) durch Verzeichnis-Trenner (/) ersetzen und .php anhängen
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// Wenn die Datei existiert, einbinden
if (file_exists($file)) {
require $file;
}
});