diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..af97f43 --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ + "name": "torsten/tippspiel2027", + "description": "Nachfolgeprojekt von tippspiel in Pure PHP", + "type": "project", + "autoload": { + "psr-4": { + "App\\": "src/" + } + } +} diff --git a/public/index.php b/public/index.php index 5c67ac3..5a373a1 100644 --- a/public/index.php +++ b/public/index.php @@ -1,13 +1,56 @@ Willkommen bei Tippspiel 2027"; echo "

Dies ist die Nachfolgeversion des alten Tippspiels (Pure PHP Variante).

"; + + try { + $db = Database::getConnection(); + $userCount = $db->query("SELECT COUNT(*) FROM users")->fetchColumn(); + echo "

Datenbank verbunden! Aktuell $userCount User im System.

"; + + echo "

Portierte Backend-Logik (Test)

"; + + // 1. Test der Score-Berechnung (getScoreLocal Logik aus Tipp.php) + echo "

Punkteberechnung

"; + $scoreTest = Tipp::getScoreLocal(2, 1, 2, 1, true); // Ergebnis: 2:1, Tipp: 2:1 (mit Joker) + echo "

Test: Spiel endet 2:1, getippt wurde 2:1 mit Joker. Erwartet: 4 Punkte. Berechnet: $scoreTest Punkte.

"; + + // 2. Test der Spiel-Logiken + echo "

Spiele der letzten gespielten Runde

"; + + // Finde zunächst die ID der letzten gespielten Runde (wo es Ergebnisse gibt) + $lastRoundId = $db->query("SELECT round_id FROM games WHERE score1 > -1 ORDER BY id DESC LIMIT 1")->fetchColumn(); + + if ($lastRoundId) { + // Hole alle Spiele exakt dieser Runde + $games = Game::getGamesByRound($lastRoundId); + + if ($games) { + echo ""; + } + } else { + echo "

Keine gespielten Spiele gefunden.

"; + } + + } catch (Exception $e) { + echo "

Datenbankfehler: " . $e->getMessage() . "

"; + } } elseif ($uri === '/api/status') { header('Content-Type: application/json'); echo json_encode(['status' => 'ok', 'version' => '2027.1']); diff --git a/schema.sql b/schema.sql index 11dc83c..8a0632a 100644 --- a/schema.sql +++ b/schema.sql @@ -1,3 +1,4 @@ +DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(201) COLLATE utf8mb4_unicode_ci NOT NULL, @@ -19,6 +20,7 @@ CREATE TABLE `users` ( UNIQUE KEY `users_email_unique` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +DROP TABLE IF EXISTS `tipps`; CREATE TABLE `tipps` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, @@ -31,6 +33,7 @@ CREATE TABLE `tipps` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +DROP TABLE IF EXISTS `games`; CREATE TABLE `games` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `round_id` int(11) NOT NULL, @@ -44,6 +47,7 @@ CREATE TABLE `games` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +DROP TABLE IF EXISTS `rounds`; CREATE TABLE `rounds` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, @@ -54,6 +58,7 @@ CREATE TABLE `rounds` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +DROP TABLE IF EXISTS `seasons`; CREATE TABLE `seasons` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, @@ -63,3 +68,58 @@ CREATE TABLE `seasons` ( `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +DROP TABLE IF EXISTS `settings`; +CREATE TABLE `settings` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `current_season_id` int(11) NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +DROP TABLE IF EXISTS `payments`; +CREATE TABLE `payments` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `season_id` int(11) NOT NULL, + `user_id` int(11) NOT NULL, + `paid_date` datetime DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +DROP TABLE IF EXISTS `quotas`; +CREATE TABLE `quotas` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `season_id` int(11) NOT NULL DEFAULT '0', + `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, + `order` int(11) NOT NULL DEFAULT '0', + `prc` double NOT NULL DEFAULT '0', + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +DROP TABLE IF EXISTS `roundmails`; +CREATE TABLE `roundmails` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `send_date` date NOT NULL, + `recipients` text COLLATE utf8mb4_unicode_ci NOT NULL, + `topic` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, + `message` text COLLATE utf8mb4_unicode_ci NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +DROP TABLE IF EXISTS `manual_payouts`; +CREATE TABLE `manual_payouts` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `season_id` int(11) NOT NULL, + `user_id` int(11) NOT NULL, + `quota_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + diff --git a/src/Core/Database.php b/src/Core/Database.php new file mode 100644 index 0000000..85e4c9a --- /dev/null +++ b/src/Core/Database.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/src/Models/Game.php b/src/Models/Game.php new file mode 100644 index 0000000..a0297e6 --- /dev/null +++ b/src/Models/Game.php @@ -0,0 +1,57 @@ +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(); + } +} diff --git a/src/Models/Round.php b/src/Models/Round.php new file mode 100644 index 0000000..016c4c7 --- /dev/null +++ b/src/Models/Round.php @@ -0,0 +1,28 @@ +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(); + } +} diff --git a/src/Models/Tipp.php b/src/Models/Tipp.php new file mode 100644 index 0000000..c559404 --- /dev/null +++ b/src/Models/Tipp.php @@ -0,0 +1,83 @@ + 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; + } +} diff --git a/src/autoload.php b/src/autoload.php new file mode 100644 index 0000000..79a7ff8 --- /dev/null +++ b/src/autoload.php @@ -0,0 +1,26 @@ +