int, 'platz1' => int, ..., 'platz10' => int] */ public static function computePayouts($seasonId) { $quotas = self::getMapBySeason($seasonId); $quota = fn($name) => $quotas[$name] ?? 0.0; $roundCount = count(Round::getBySeason($seasonId)); $pot = count(User::getPaid($seasonId)) * $quota('einsatz'); $dayWinSum = (int)($pot * $quota('tagessieg') / 100); $dayWin = $roundCount > 0 ? self::get6Divisor((int)($dayWinSum / $roundCount)) : 0; $placementPot = (int)($pot * $quota('platzierung') / 100); $placementPot += $dayWinSum - $dayWin * $roundCount; $payouts = ['tagessieg' => $dayWin]; $distributed = $dayWin * $roundCount; for ($place = 1; $place <= self::PLACES; $place++) { $amount = self::get6Divisor((int)($placementPot * $quota('platz' . $place) / 100)); $payouts['platz' . $place] = $amount; $distributed += $amount; } // Nicht verteilter Rest an Platz 1 $payouts['platz1'] += (int)$pot - $distributed; return $payouts; } /** * Senkt die Zahl, bis sie durch 6 teilbar ist (Abrundungsvorgabe für Ausschüttungen). * @param int $number * @return int */ public static function get6Divisor($number) { return max(0, $number - $number % 6); } /** * Holt alle Quoten für eine Saison. * @param int $seasonId * @return array */ public static function getBySeason($seasonId) { $db = Database::getConnection(); $stmt = $db->prepare("SELECT * FROM quotas WHERE season_id = ? ORDER BY `order` ASC, id ASC"); $stmt->execute([$seasonId]); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } /** * Quoten einer Saison als name => prc. * @param int $seasonId * @return array */ public static function getMapBySeason($seasonId) { $map = []; foreach (self::getBySeason($seasonId) as $quota) { $map[$quota['name']] = (float)$quota['prc']; } return $map; } /** * Namen der pflegbaren Quoten (Legacy SeasonsController::store_quota). * @return string[] */ public static function editableNames() { $names = ['einsatz', 'tagessieg', 'platzierung']; for ($place = 1; $place <= self::PLACES; $place++) { $names[] = 'platz' . $place; } return $names; } /** * Standardquoten für eine neue Saison (Legacy Quota::createDefaultQuota * ohne Grünes Trikot, Pokal und Liga). * @return array name => prc */ public static function defaults() { return [ 'einsatz' => 40, 'tagessieg' => 60, 'platzierung' => 40, 'platz1' => 21, 'platz2' => 17, 'platz3' => 13, 'platz4' => 10, 'platz5' => 9, 'platz6' => 8, 'platz7' => 7, 'platz8' => 6, 'platz9' => 5, 'platz10' => 4, ]; } /** * Prüft die Quoten: alle Werte >= 0, Tagessieg + Platzierung = 100 %, * Platz 1..10 zusammen = 100 %. * @param array $quotas name => prc * @return string|null Fehlermeldung oder null */ public static function validate(array $quotas) { foreach (self::editableNames() as $name) { if (!isset($quotas[$name]) || !is_numeric($quotas[$name]) || $quotas[$name] < 0) { return "Ungültiger Wert für \"$name\"."; } } if (abs($quotas['tagessieg'] + $quotas['platzierung'] - 100) > 0.001) { return 'Tagessieg und Platzierung müssen zusammen 100 % ergeben.'; } $placeSum = 0; for ($place = 1; $place <= self::PLACES; $place++) { $placeSum += $quotas['platz' . $place]; } if (abs($placeSum - 100) > 0.001) { return 'Die Platzierungsquoten müssen zusammen 100 % ergeben (aktuell ' . round($placeSum, 2) . ' %).'; } return null; } /** * Speichert die pflegbaren Quoten einer Saison (Upsert je Name wie Legacy Quota::setQuota). * Andere Einträge (z.B. Legacy gshirt, liga) bleiben unverändert. * @param int $seasonId * @param array $quotas name => prc */ public static function saveForSeason($seasonId, array $quotas) { $db = Database::getConnection(); $existing = []; foreach (self::getBySeason($seasonId) as $quota) { $existing[$quota['name']] = (int)$quota['id']; } $update = $db->prepare("UPDATE quotas SET prc = ?, `order` = ?, updated_at = NOW() WHERE id = ?"); $insert = $db->prepare( "INSERT INTO quotas (season_id, name, `order`, prc, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())" ); $db->beginTransaction(); try { foreach (self::editableNames() as $name) { $prc = (float)$quotas[$name]; $order = str_starts_with($name, 'platz') && $name !== 'platzierung' ? (int)substr($name, 5) : 0; if (isset($existing[$name])) { $update->execute([$prc, $order, $existing[$name]]); } else { $insert->execute([$seasonId, $name, $order, $prc]); } } $db->commit(); } catch (\Exception $e) { $db->rollBack(); throw $e; } } }