'Raid', 'event' => 'Event', 'other' => 'Sonstiges', ]; // Anlaesse, die nur das System anlegt (nicht im Formular waehlbar). Vergaben dieser Art // lassen sich nicht auf der Lumipunkte-Seite loeschen, sondern nur dort, wo sie entstanden sind. const CONTRIBUTION_SYSTEM_CATEGORIES = [ 'shop' => 'Lumishop', ]; // Anlaesse, die automatisch entstehen (nicht im Formular waehlbar), sich aber wie normale // Vergaben auf der Lumipunkte-Seite loeschen lassen. const CONTRIBUTION_AUTO_CATEGORIES = [ 'activity' => 'Aktivitätscheck', 'daily' => 'Tägliche Belohnung', ]; // Taegliche Belohnung: so viele Punkte beim ersten Besuch der Website an einem Tag const CONTRIBUTION_DAILY_POINTS = 5; // So viele Plaetze zeigt die Rangliste. Mit contributions.view sieht man nur diese, mit // contributions.view_all (oder contributions.manage) laesst sich der Rest aufklappen und durchsuchen. const CONTRIBUTION_PUBLIC_RANKS = 24; // Obergrenze pro Mitglied und Vergabe - schuetzt vor Tippfehlern wie 10000 statt 100. const CONTRIBUTION_MAX_POINTS = 10000; function contribution_category_label(string $key): string { return CONTRIBUTION_CATEGORIES[$key] ?? CONTRIBUTION_SYSTEM_CATEGORIES[$key] ?? CONTRIBUTION_AUTO_CATEGORIES[$key] ?? $key; } /** Das Muenz-Symbol der Lumipunkte. */ function contribution_coin(int $size = 20): string { return 'Lumipunkte'; } /** Punkte mit Tausenderpunkt, Abzuege mit echtem Minuszeichen. */ function contribution_format(int $points, bool $withSign = false): string { $formatted = number_format(abs($points), 0, ',', '.'); if ($points < 0) { return '−' . $formatted; } return ($withSign && $points > 0 ? '+' : '') . $formatted; } /** * Punktestand aller aktiven Mitglieder der Legion (auch 0), hoechster zuerst - Verbuendete aus dem Consilium sammeln * keine Lumipunkte. * * @return array> */ function contribution_balances(): array { return db()->query( "SELECT u.id, u.username, u.display_name, u.avatar_filename, COALESCE(SUM(e.points), 0) AS points, COUNT(e.id) AS award_count FROM users u LEFT JOIN contribution_entries e ON e.user_id = u.id WHERE u.status = 'active' AND " . sql_is_legion_member('u') . " GROUP BY u.id ORDER BY points DESC, COALESCE(NULLIF(u.display_name, ''), u.username)" )->fetchAll(); } function contribution_user_balance(int $userId): int { $stmt = db()->prepare('SELECT COALESCE(SUM(points), 0) FROM contribution_entries WHERE user_id = ?'); $stmt->execute([$userId]); return (int) $stmt->fetchColumn(); } function contribution_award_count(?int $userId): int { if ($userId === null) { return (int) db()->query('SELECT COUNT(*) FROM contribution_awards')->fetchColumn(); } $stmt = db()->prepare('SELECT COUNT(*) FROM contribution_entries WHERE user_id = ?'); $stmt->execute([$userId]); return (int) $stmt->fetchColumn(); } /** * Vergaben mit ihren Empfaengern, neueste zuerst. Mit $userId nur die Vergaben, an denen * dieses Mitglied beteiligt war. * * @return array> */ function contribution_awards(?int $userId, int $limit, int $offset): array { $join = $userId !== null ? 'JOIN contribution_entries mine ON mine.award_id = a.id AND mine.user_id = ?' : ''; $stmt = db()->prepare( "SELECT a.id, a.category, a.title, a.note, a.raid_id, a.awarded_at, COALESCE(NULLIF(cu.display_name, ''), cu.username) AS author FROM contribution_awards a $join LEFT JOIN users cu ON cu.id = a.created_by ORDER BY a.awarded_at DESC, a.id DESC LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset ); $stmt->execute($userId !== null ? [$userId] : []); $awards = []; foreach ($stmt->fetchAll() as $row) { $row['entries'] = []; $awards[(int) $row['id']] = $row; } if (empty($awards)) { return []; } $ids = array_keys($awards); $placeholders = implode(',', array_fill(0, count($ids), '?')); $entryStmt = db()->prepare( "SELECT e.id, e.award_id, e.user_id, e.points, COALESCE(NULLIF(u.display_name, ''), u.username) AS name FROM contribution_entries e JOIN users u ON u.id = e.user_id WHERE e.award_id IN ($placeholders) ORDER BY name" ); $entryStmt->execute($ids); foreach ($entryStmt->fetchAll() as $entry) { $awards[(int) $entry['award_id']]['entries'][] = $entry; } return array_values($awards); } /** Jetzt in deutscher Zeit fuer ein datetime-local-Feld. */ function contribution_now_input(): string { return (new DateTimeImmutable('now', new DateTimeZone('Europe/Berlin')))->format('Y-m-d\TH:i'); } /** Zeitpunkt aus einem datetime-local-Feld, null bei ungueltiger Eingabe oder weit in der Zukunft. */ function contribution_parse_time(string $value): ?string { $tz = new DateTimeZone('Europe/Berlin'); $time = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', trim($value), $tz); if ($time === false || $time > new DateTimeImmutable('+1 day', $tz)) { return null; } return $time->format('Y-m-d H:i:s'); } function contribution_format_time(?string $value): string { $time = $value ? strtotime($value) : false; return $time ? date('d.m.Y H:i', $time) : '-'; } /** * Taegliche Belohnung: beim ersten Seitenaufruf eines Tages CONTRIBUTION_DAILY_POINTS Punkte. * Pro Tag gibt es eine gemeinsame Vergabe ("Tägliche Belohnung"), an die jedes Mitglied beim * ersten Besuch angehaengt wird - so bleibt der Verlauf uebersichtlich. Der eindeutige Schluessel * (award_id, user_id) verhindert doppelte Punkte, auch bei mehreren Tabs oder Geraeten. * * @return bool true = gerade gutgeschrieben (fuer die Meldung) */ function contribution_daily_reward(int $userId): bool { $today = date('Y-m-d'); // Pro Sitzung nur einmal am Tag nachsehen if (($_SESSION['daily_reward_checked'] ?? '') === $today . ':' . $userId) { return false; } $_SESSION['daily_reward_checked'] = $today . ':' . $userId; $pdo = db(); try { $find = $pdo->prepare("SELECT id FROM contribution_awards WHERE category = 'daily' AND awarded_at = ? ORDER BY id LIMIT 1"); $find->execute([$today . ' 00:00:00']); $awardId = (int) $find->fetchColumn(); if ($awardId === 0) { // Gleichzeitige erste Besuche des Tages duerfen die Vergabe nicht doppelt anlegen $pdo->query("SELECT GET_LOCK('lumina_daily_reward', 5)"); $find->execute([$today . ' 00:00:00']); $awardId = (int) $find->fetchColumn(); if ($awardId === 0) { $pdo->prepare( "INSERT INTO contribution_awards (category, title, note, awarded_at, created_by) VALUES ('daily', ?, ?, ?, NULL)" )->execute([ 'Tägliche Belohnung ' . date('d.m.Y'), 'Automatisch beim ersten Besuch der Website an diesem Tag', $today . ' 00:00:00', ]); $awardId = (int) $pdo->lastInsertId(); } $pdo->query("SELECT RELEASE_LOCK('lumina_daily_reward')"); } $insert = $pdo->prepare('INSERT IGNORE INTO contribution_entries (award_id, user_id, points) VALUES (?, ?, ?)'); $insert->execute([$awardId, $userId, CONTRIBUTION_DAILY_POINTS]); return $insert->rowCount() > 0; } catch (PDOException $e) { return false; } }