['Wartet auf Prüfung', 'badge-pending'], 'accepted' => ['Anerkannt', 'badge-active'], 'rejected' => ['Abgelehnt', 'badge-disabled'], ]; // Bilder liegen NICHT oeffentlich abrufbar im Webroot-Ordner (eigene .htaccess sperrt ihn), sondern werden ueber // /hunt_image.php ausgeliefert: die gesuchten Orte erst ab dem Start, die Einsendungen nur an die Leitung und // an den, der sie geschickt hat. const HUNT_UPLOAD_DIR = __DIR__ . '/../assets/uploads/hunt/'; const HUNT_MAX_UPLOAD = 10 * 1024 * 1024; // Laengste Seite gespeicherter Bilder - Uhrzeit und Koordinaten auf den Screenshots muessen lesbar bleiben const HUNT_MAX_SIDE = 2560; function hunt_can_manage(): bool { return user_has_permission('hunt.manage'); } /** Mitmachen darf, wer hunt.view hat - so schaltet die Leitung die Schnitzeljagd erst frei, wenn es losgehen soll. */ function hunt_can_take_part(): bool { return user_has_permission('hunt.view'); } /** Hat die Jagd begonnen (Bilder sichtbar)? */ function hunt_started(array $hunt): bool { return strtotime((string) $hunt['starts_at']) <= time(); } /** Ist Einsendeschluss vorbei? Ohne Ende bleibt die Jagd offen, bis die Leitung sie schliesst. */ function hunt_closed(array $hunt): bool { return !empty($hunt['ends_at']) && strtotime((string) $hunt['ends_at']) <= time(); } /** Hat die Leitung die Jagd als ausgewertet markiert (offiziell vorbei)? */ function hunt_evaluated(array $hunt): bool { return !empty($hunt['evaluated_at']); } /** Laeuft die Jagd gerade (Einsendungen moeglich)? */ function hunt_running(array $hunt): bool { return !empty($hunt['is_published']) && hunt_started($hunt) && !hunt_closed($hunt) && !hunt_evaluated($hunt); } /** Bis wann zaehlt das Bonusbild? */ function hunt_bonus_deadline(array $hunt): int { return strtotime((string) $hunt['starts_at']) + HUNT_BONUS_HOURS * 3600; } /** Jagd laden - Mitglieder sehen nur veroeffentlichte. */ function hunt_find(int $id): ?array { $stmt = db()->prepare('SELECT * FROM hunts WHERE id = ?'); $stmt->execute([$id]); $hunt = $stmt->fetch(); if (!$hunt || (empty($hunt['is_published']) && !hunt_can_manage())) { return null; } return $hunt; } /** @return array Orte der Jagd in Reihenfolge, das Bonusbild zuletzt */ function hunt_spots(int $huntId): array { $stmt = db()->prepare('SELECT * FROM hunt_spots WHERE hunt_id = ? ORDER BY is_bonus, sort_order, id'); $stmt->execute([$huntId]); return $stmt->fetchAll(); } /** Anzeigename eines Ortes: "Ort 3" bzw. "Bonusbild", mit Titel, falls die Leitung einen vergeben hat. */ function hunt_spot_label(array $spot, int $number): string { $base = !empty($spot['is_bonus']) ? 'Bonusbild' : 'Ort ' . $number; return trim((string) $spot['title']) !== '' ? $base . ': ' . $spot['title'] : $base; } function hunt_status_badge(string $status): string { [$label, $class] = HUNT_SUBMISSION_STATUSES[$status] ?? [$status, '']; return '' . e($label) . ''; } /** * Alle Einsendungen einer Jagd mit Mitglied, je Ort nach Zeitpunkt sortiert - dazu Platz und Punkte der anerkannten. * * @return array> Ort-ID => Einsendungen */ function hunt_submissions_by_spot(int $huntId): array { $stmt = db()->prepare( 'SELECT s.*, u.username, u.display_name, u.discord_id, u.avatar_filename, sp.is_bonus, r.username AS reviewer_username, r.display_name AS reviewer_display_name FROM hunt_submissions s JOIN hunt_spots sp ON sp.id = s.spot_id LEFT JOIN users u ON u.id = s.user_id LEFT JOIN users r ON r.id = s.reviewed_by WHERE sp.hunt_id = ? ORDER BY COALESCE(s.ingame_at, s.submitted_at), s.submitted_at, s.id' ); $stmt->execute([$huntId]); $hunt = db()->prepare('SELECT starts_at FROM hunts WHERE id = ?'); $hunt->execute([$huntId]); $bonusDeadline = hunt_bonus_deadline(['starts_at' => (string) $hunt->fetchColumn()]); $bySpot = []; $ranks = []; foreach ($stmt->fetchAll() as $row) { $spotId = (int) $row['spot_id']; $row['rank'] = null; $row['points'] = 0; if ($row['status'] === 'accepted') { if (!empty($row['is_bonus'])) { $row['points'] = strtotime(hunt_effective_time($row)) <= $bonusDeadline ? HUNT_BONUS_POINTS : 0; } else { $ranks[$spotId] = ($ranks[$spotId] ?? 0) + 1; $row['rank'] = $ranks[$spotId]; $row['points'] = HUNT_RANK_POINTS[$ranks[$spotId] - 1] ?? 0; } } $bySpot[$spotId][] = $row; } return $bySpot; } /** * Rangliste: Punkte je Mitglied ueber alle Orte, absteigend; bei Gleichstand zaehlt die fruehere letzte Einsendung. * * @param array> $bySpot aus hunt_submissions_by_spot() * @return array */ function hunt_leaderboard(array $bySpot): array { $board = []; foreach ($bySpot as $submissions) { foreach ($submissions as $row) { if ($row['status'] !== 'accepted' || $row['user_id'] === null) { continue; } $userId = (int) $row['user_id']; $board[$userId] ??= [ 'user' => [ 'id' => $userId, 'username' => $row['username'], 'display_name' => $row['display_name'], 'discord_id' => $row['discord_id'], 'avatar_filename' => $row['avatar_filename'], ], 'points' => 0, 'found' => 0, 'last' => '', ]; $board[$userId]['points'] += (int) $row['points']; $board[$userId]['found']++; $board[$userId]['last'] = max($board[$userId]['last'], hunt_effective_time($row)); } } usort($board, static fn (array $a, array $b): int => [$b['points'], $a['last']] <=> [$a['points'], $b['last']]); return $board; } /** @return array Eigene Einsendungen einer Jagd: Ort-ID => Einsendung */ function hunt_own_submissions(int $huntId, int $userId): array { $stmt = db()->prepare( 'SELECT s.* FROM hunt_submissions s JOIN hunt_spots sp ON sp.id = s.spot_id WHERE sp.hunt_id = ? AND s.user_id = ?' ); $stmt->execute([$huntId, $userId]); $own = []; foreach ($stmt->fetchAll() as $row) { $own[(int) $row['spot_id']] = $row; } return $own; } /** Anzahl Einsendungen, die noch auf Pruefung warten (Abzeichen in der Seitenleiste) - ausgewertete Jagden zaehlen nicht. */ function hunt_pending_count(): int { try { return (int) db()->query( "SELECT COUNT(*) FROM hunt_submissions s JOIN hunt_spots sp ON sp.id = s.spot_id JOIN hunts h ON h.id = sp.hunt_id WHERE s.status = 'pending' AND h.evaluated_at IS NULL" )->fetchColumn(); } catch (PDOException $e) { return 0; } } /** * Menuepunkt fuer Mitglieder nur, solange es eine Jagd gibt, die bald beginnt, laeuft oder vor kurzem * zu Ende gegangen ist - die Leitung sieht ihn immer. */ function hunt_nav_visible(): bool { if (hunt_can_manage()) { return true; } if (!hunt_can_take_part()) { return false; } try { return (bool) db()->query( 'SELECT 1 FROM hunts WHERE is_published = 1 AND (COALESCE(evaluated_at, ends_at) IS NULL OR COALESCE(evaluated_at, ends_at) > NOW() - INTERVAL 14 DAY) LIMIT 1' )->fetchColumn(); } catch (PDOException $e) { return false; } } function hunt_image_url(string $kind, int $id): string { return '/hunt_image.php?' . http_build_query(['kind' => $kind, 'id' => $id]); } /** Vorschaubild mit Grossansicht (assets/js/lightbox.js). */ function hunt_thumb_html(string $kind, int $id, string $group, string $caption, string $class = ''): string { $url = e(hunt_image_url($kind, $id)); return '' . e($caption) . ''; } /** Massgebliche Zeit einer Einsendung: die Uhrzeit im Spiel, solange die Leitung keine eingetragen hat das Hochladen. */ function hunt_effective_time(array $submission): string { return (string) ($submission['ingame_at'] ?? $submission['submitted_at']); } /** Uhrzeit im Spiel - Sekunden nur, wenn die Leitung welche eingetragen hat (die Uhr im Spiel zeigt meist nur Minuten). */ function hunt_ingame_time(string $datetime): string { $ts = strtotime($datetime); return date((int) date('s', $ts) === 0 ? 'd.m.Y H:i' : 'd.m.Y H:i:s', $ts); } /** Datum mit Sekunden - fuer die Reihenfolge der Einsendungen kommt es darauf an. */ function hunt_time(string $datetime): string { return date('d.m.Y H:i:s', strtotime($datetime)); } /** "Fr, 24.10. um 20:00 Uhr" */ function hunt_date(string $datetime): string { $ts = strtotime($datetime); return ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'][(int) date('w', $ts)] . ', ' . date('d.m.Y', $ts) . ' um ' . date('H:i', $ts) . ' Uhr'; } /** Ein einzelnes Bild aus $_FILES (leeres Feld = null). */ function hunt_uploaded_file(string $field): ?array { $file = $_FILES[$field] ?? null; if (!is_array($file) || is_array($file['name'] ?? null) || (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) { return null; } return $file; } /** * Hochgeladenes Bild pruefen, neu speichern (entfernt EXIF/angehaengten Code, verkleinert auf HUNT_MAX_SIDE) * und den Dateinamen zurueckgeben. * * @param array $file Eintrag aus $_FILES * @throws RuntimeException mit verstaendlicher Meldung */ function hunt_store_upload(array $file, string $prefix): string { if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) { throw new RuntimeException(in_array($file['error'] ?? 0, [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true) ? 'Ein Bild ist zu groß.' : 'Ein Bild konnte nicht hochgeladen werden.'); } if ((int) $file['size'] > HUNT_MAX_UPLOAD) { throw new RuntimeException('Ein Bild ist zu groß (höchstens 10 MB).'); } $info = @getimagesize((string) $file['tmp_name']); $types = [IMAGETYPE_PNG => 'png', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_GIF => 'gif', IMAGETYPE_WEBP => 'webp']; if ($info === false || !isset($types[$info[2]])) { throw new RuntimeException('Nur PNG, JPG, GIF oder WEBP sind als Bild erlaubt.'); } if (!is_dir(HUNT_UPLOAD_DIR)) { @mkdir(HUNT_UPLOAD_DIR, 0755, true); } $name = $prefix . '_' . bin2hex(random_bytes(8)) . '.' . $types[$info[2]]; $error = store_uploaded_image((string) $file['tmp_name'], HUNT_UPLOAD_DIR . $name, $info[2], HUNT_MAX_SIDE, true); if ($error !== null) { throw new RuntimeException($error); } return $name; } function hunt_delete_file(?string $filename): void { if ($filename !== null && $filename !== '' && strpos($filename, '/') === false && is_file(HUNT_UPLOAD_DIR . $filename)) { @unlink(HUNT_UPLOAD_DIR . $filename); } } /** Dateien aller Einsendungen (und Ortsbilder) einer Jagd loeschen - die Zeilen fallen per Fremdschluessel weg. */ function hunt_delete_files(int $huntId): void { $stmt = db()->prepare( 'SELECT s.screenshot_filename, s.map_filename FROM hunt_submissions s JOIN hunt_spots sp ON sp.id = s.spot_id WHERE sp.hunt_id = ? UNION ALL SELECT image_filename, NULL FROM hunt_spots WHERE hunt_id = ?' ); $stmt->execute([$huntId, $huntId]); foreach ($stmt->fetchAll(PDO::FETCH_NUM) as $row) { hunt_delete_file($row[0]); hunt_delete_file($row[1]); } }