[ ['id' => 'duty', 'amt' => '5×', 'name' => 'Pflicht-Quests', 'm' => true, 't' => false], ['id' => 'feld', 'amt' => '2×', 'name' => 'Feldbosse', 'm' => true, 't' => false], ], 'weekly' => [ ['id' => 'dd', 'amt' => '14×', 'name' => 'Täglicher Dungeon', 'm' => true, 't' => false], ['id' => 'asz', 'amt' => '3×', 'name' => 'Aszensionsritus', 'm' => true, 't' => true], ['id' => 'sf', 'amt' => '3×', 'name' => 'Schlachtfeld', 'm' => true, 't' => true], ['id' => 'weih', 'amt' => '1×', 'name' => 'Weihstätte', 'm' => true, 't' => true], ['id' => 'pve', 'amt' => '12×', 'name' => 'PvE-Händler-Aufträge', 'm' => true, 't' => false], ['id' => 'pvp', 'amt' => '20×', 'name' => 'PvP-Händler-Aufträge', 'm' => true, 't' => false], ['id' => 'konv16', 'amt' => '16×', 'name' => 'Konvertierung Energie', 'm' => true, 't' => false], ['id' => 'konv4', 'amt' => '4×', 'name' => 'Konvertierung Energie', 'm' => true, 't' => true], ['id' => 'penr16', 'amt' => '16×', 'name' => 'Pass Energie', 'm' => true, 't' => false], ['id' => 'penr4', 'amt' => '4×', 'name' => 'Pass Energie', 'm' => true, 't' => true], ['id' => 'pexp', 'amt' => 'X', 'name' => 'Pass EXP', 'm' => true, 't' => true], ['id' => 'geist', 'amt' => '10×', 'name' => 'Geiststein der Wiederbelebung', 'm' => true, 't' => true, 'section' => 'Shop'], ['id' => 'geistev', 'amt' => '10×', 'name' => 'Geiststein der Wiederbelebung (Event)', 'm' => true, 't' => false, 'section' => 'Shop', 'until' => '2026-12-16 09:00'], ['id' => 'seele', 'amt' => '1000×', 'name' => 'Seelenkristall', 'm' => true, 't' => false, 'section' => 'Shop'], ], ]; const CHECKLIST_KIND_LABELS = ['daily' => 'Täglich', 'weekly' => 'Wöchentlich']; // Was sich von selbst auflaedt - nur zur Info, ohne Haken const CHECKLIST_INFO = [ ['name' => 'Expedition / Transzendenz', 'val' => '15 Energie / 3 Std', 'who' => 'Main + Twink'], ['name' => 'Alptraum', 'val' => '2 Tickets / Tag', 'who' => 'Main + Twink'], ['name' => 'Shugo', 'val' => '3 Schlüssel / Tag', 'who' => 'Nur Main'], ['name' => 'Beritra', 'val' => '1 Schlüssel / Tag', 'who' => 'Nur Main'], ['name' => 'Abyss', 'val' => '0–3 Artefakte / Periode', 'who' => 'Main + Twink'], ]; // Einstellung je Charakter (member_characters.checklist_role); '' = automatisch const CHECKLIST_ROLES = ['' => 'Automatisch', 'main' => 'Main', 'twink' => 'Twink', 'hidden' => 'Ausblenden']; /** * Beginn des laufenden taeglichen und woechentlichen Zeitraums (Datum des Reset-Tages) und die naechsten Resets. * * @return array{daily: string, weekly: string, next_daily: int, next_weekly: int} */ function checklist_periods(?int $now = null): array { $tz = new DateTimeZone('Europe/Berlin'); $nowDate = (new DateTimeImmutable('@' . ($now ?? time())))->setTimezone($tz); // Vor der Reset-Uhrzeit gehoert die Zeit noch zum Vortag $day = $nowDate->modify('-' . CHECKLIST_RESET_HOUR . ' hours')->setTime(0, 0); $week = $day->modify('-' . ((((int) $day->format('N')) - CHECKLIST_WEEKLY_DAY + 7) % 7) . ' days'); return [ 'daily' => $day->format('Y-m-d'), 'weekly' => $week->format('Y-m-d'), 'next_daily' => $day->modify('+1 day')->setTime(CHECKLIST_RESET_HOUR, 0)->getTimestamp(), 'next_weekly' => $week->modify('+7 days')->setTime(CHECKLIST_RESET_HOUR, 0)->getTimestamp(), ]; } // Persoenliche Notiz je Charakter (checklist_notes) - hoechstens so viele Zeichen const CHECKLIST_NOTE_MAX = 2000; /** Aufgaben einer Art (daily/weekly) ohne abgelaufene (until). */ function checklist_tasks(string $kind): array { $now = time(); return array_values(array_filter(CHECKLIST_TASKS[$kind], static fn (array $task): bool => !isset($task['until']) || (new DateTimeImmutable($task['until'], new DateTimeZone('Europe/Berlin')))->getTimestamp() > $now)); } /** Aufgabe nach id, mit ihrer Art (daily/weekly) - oder null. */ function checklist_task(string $id): ?array { foreach (array_keys(CHECKLIST_TASKS) as $kind) { foreach (checklist_tasks($kind) as $task) { if ($task['id'] === $id) { return $task + ['kind' => $kind]; } } } return null; } function checklist_applies(array $task, string $role): bool { return $role === 'main' ? $task['m'] : ($role === 'twink' && $task['t']); } /** * Charaktere eines Mitglieds mit ihrer Rolle in der Checkliste ('main', 'twink' oder 'hidden'). * Ohne Einstellung: der Hauptcharakter ist Main, die anderen Twinks; ist keiner als Hauptcharakter * markiert, gilt der erste als Main. * * @return list> */ function checklist_characters(int $userId): array { $stmt = db()->prepare( 'SELECT id, name, class_name, level, is_main, checklist_role FROM member_characters WHERE user_id = ? ORDER BY is_main DESC, sort_order, id' ); $stmt->execute([$userId]); $characters = $stmt->fetchAll(); $hasMain = (bool) array_filter($characters, static fn (array $c): bool => (int) $c['is_main'] === 1); foreach ($characters as $i => &$character) { $setting = (string) ($character['checklist_role'] ?? ''); $character['setting'] = isset(CHECKLIST_ROLES[$setting]) ? $setting : ''; $character['auto_role'] = ((int) $character['is_main'] === 1 || (!$hasMain && $i === 0)) ? 'main' : 'twink'; $character['role'] = $character['setting'] !== '' ? $character['setting'] : $character['auto_role']; } unset($character); return $characters; } /** * Haken des laufenden Zeitraums: ["|" => true]. * * @return array */ function checklist_ticks(int $userId, array $periods): array { $stmt = db()->prepare('SELECT task_id, character_id, period FROM checklist_ticks WHERE user_id = ? AND period IN (?, ?)'); $stmt->execute([$userId, $periods['daily'], $periods['weekly']]); $ticks = []; foreach ($stmt->fetchAll() as $row) { $task = checklist_task((string) $row['task_id']); if ($task !== null && $row['period'] === $periods[$task['kind']]) { $ticks[$row['task_id'] . '|' . $row['character_id']] = true; } } return $ticks; } /** Haken setzen oder entfernen (Charakter und Aufgabe pruefen die Aufrufer). */ function checklist_set(int $userId, int $characterId, array $task, bool $done): void { $periods = checklist_periods(); $pdo = db(); if ($done) { $pdo->prepare('INSERT IGNORE INTO checklist_ticks (user_id, character_id, task_id, period) VALUES (?, ?, ?, ?)') ->execute([$userId, $characterId, $task['id'], $periods[$task['kind']]]); } else { $pdo->prepare('DELETE FROM checklist_ticks WHERE character_id = ? AND task_id = ? AND period = ?') ->execute([$characterId, $task['id'], $periods[$task['kind']]]); } // Alte Zeitraeume gelegentlich wegraeumen if (random_int(1, 50) === 1) { $pdo->prepare('DELETE FROM checklist_ticks WHERE period < ?') ->execute([date('Y-m-d', time() - CHECKLIST_KEEP_DAYS * 86400)]); } } /** Erledigt/gesamt eines Charakters fuer eine Art (daily/weekly). */ function checklist_progress(array $character, string $kind, array $ticks): array { $done = 0; $total = 0; foreach (checklist_tasks($kind) as $task) { if (checklist_applies($task, $character['role'])) { $total++; if (isset($ticks[$task['id'] . '|' . $character['id']])) { $done++; } } } return ['done' => $done, 'total' => $total]; } /** "4 Std 12 Min" bzw. "2 Tagen 5 Std" bis zum Zeitpunkt (wie in der Vorlage). */ function checklist_countdown(int $until, ?int $now = null): string { $minutes = max(0, intdiv($until - ($now ?? time()), 60)); $days = intdiv($minutes, 1440); $hours = intdiv($minutes % 1440, 60); return $days > 0 ? $days . ' ' . ($days === 1 ? 'Tag' : 'Tagen') . ' ' . $hours . ' Std' : $hours . ' Std ' . ($minutes % 60) . ' Min'; } function checklist_class_icon(array $character): string { $url = $character['class_name'] ? class_icon_url((string) $character['class_name']) : null; return $url !== null ? '' : ''; } /** * Persoenliche Notizen der Charaktere eines Mitglieds: [Charakter-ID => Text]. Nur fuer das Mitglied selbst. * * @return array */ function checklist_notes(int $userId): array { $stmt = db()->prepare('SELECT character_id, note FROM checklist_notes WHERE user_id = ?'); $stmt->execute([$userId]); $notes = []; foreach ($stmt->fetchAll() as $row) { $notes[(int) $row['character_id']] = (string) $row['note']; } return $notes; } /** Notiz speichern; leer = loeschen (Charakter prueft der Aufrufer). */ function checklist_set_note(int $userId, int $characterId, string $note): void { if ($note === '') { db()->prepare('DELETE FROM checklist_notes WHERE character_id = ?')->execute([$characterId]); return; } db()->prepare( 'INSERT INTO checklist_notes (character_id, user_id, note) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE note = VALUES(note), updated_at = CURRENT_TIMESTAMP' )->execute([$characterId, $userId, $note]); } /** Fortschritt je Charakter (Kaertchen mit zwei Balken und der Notiz). */ function checklist_summary_html(array $characters, array $ticks, array $notes = []): string { $html = '
'; foreach ($characters as $c) { $bars = ''; $full = true; foreach (CHECKLIST_KIND_LABELS as $kind => $label) { $p = checklist_progress($c, $kind, $ticks); if ($p['total'] === 0) { continue; // z.B. Twinks haben keine taeglichen Aufgaben } $full = $full && $p['done'] === $p['total']; $percent = $p['total'] > 0 ? round($p['done'] / $p['total'] * 100) : 0; $bars .= '
' . e($label) . ' ' . $p['done'] . '/' . $p['total'] . '
' . '
' . '
'; } $html .= '
' . '
' . checklist_class_icon($c) . e((string) $c['name']) . '' . '' . e(CHECKLIST_ROLES[$c['role']]) . '
' . $bars . '

' . e($notes[(int) $c['id']] ?? '') . '

'; } return $html . '
'; } /** Tabelle einer Art: Aufgaben in Zeilen, Charaktere in Spalten. */ function checklist_table_html(string $kind, array $characters, array $ticks, array $periods): string { $done = 0; $total = 0; $rows = ''; $section = ''; foreach (checklist_tasks($kind) as $task) { if (($task['section'] ?? '') !== $section) { $section = (string) ($task['section'] ?? ''); $rows .= '
' . e($section) . '
'; } $cells = ''; $all = true; foreach ($characters as $c) { if (!checklist_applies($task, $c['role'])) { $cells .= '–'; continue; } $on = isset($ticks[$task['id'] . '|' . $c['id']]); $total++; $done += $on ? 1 : 0; $all = $all && $on; $cells .= ''; } $rows .= '
' . '' . e($task['amt']) . '' . e($task['name']) . '' . $cells . '
'; } $head = ''; foreach ($characters as $c) { $head .= '' . checklist_class_icon($c) . e((string) $c['name']) . ''; } $next = $kind === 'daily' ? $periods['next_daily'] : $periods['next_weekly']; // Rot, wenn der Reset bald kommt: taeglich unter 3 Stunden, woechentlich unter einem Tag $urgent = $next - time() < ($kind === 'daily' ? 3 * 3600 : 86400); return '
' . '

' . e(CHECKLIST_KIND_LABELS[$kind]) . '

' . '' . $done . '/' . $total . '
' . '
' . 'Reset in ' . e(checklist_countdown($next)) . '
' . '
' . '
Aufgabe' . $head . '
' . $rows . '
Alles erledigt 🎉
'; } /** Hinweis, wenn noch keine Charaktere da sind. */ function checklist_no_characters_html(): string { $link = user_has_permission('members.view') ? 'Lege auf deiner Profilseite einen Charakter an' : 'Lege auf deiner Profilseite einen Charakter an'; return '

' . $link . ' – dann erscheinen hier seine Aufgaben zum Abhaken.

'; }