LuminaWebsite/includes/checklist.php
Maaxxs 4e25365b4c Checkliste: persoenliche Notizen je Charakter und woechentliche Shop-Kaeufe
Notizfeld je Charakter auf der Checkliste (speichert beim Verlassen, eigene Tabelle checklist_notes,
nur fuer den Besitzer), Notiz auch im Charakter-Kaertchen im Dashboard. Neuer Abschnitt "Shop" unter
Woechentlich: Geiststein der Wiederbelebung, Event-Geiststein (bis 16.12.2026) und Seelenkristall.
Patchnotes, Datenauskunft und Datenschutz ergaenzt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 20:17:19 +02:00

317 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* Aion-2-Checkliste (checklist.php, Karte im Dashboard): taegliche und woechentliche Aufgaben je Charakter
* zum Abhaken. Die Charaktere kommen aus dem Profil (member_characters), die Haken stehen in checklist_ticks -
* je Charakter, Aufgabe und Zeitraum. Nach dem Reset gilt ein neuer Zeitraum, alte Haken zaehlen dann einfach
* nicht mehr und werden nach CHECKLIST_KEEP_DAYS geloescht.
*
* Ob ein Charakter als Main oder Twink zaehlt, legt member_characters.checklist_role fest
* (NULL = automatisch: Hauptcharakter = Main, alle anderen = Twink).
*/
// Reset (Uhrzeit in Europe/Berlin): taeglich um CHECKLIST_RESET_HOUR, woechentlich am CHECKLIST_WEEKLY_DAY (1 = Montag ... 7 = Sonntag)
const CHECKLIST_RESET_HOUR = 9;
const CHECKLIST_WEEKLY_DAY = 3;
const CHECKLIST_KEEP_DAYS = 21;
// Aufgaben: id (eindeutig, hoechstens 20 Zeichen - steht in der Datenbank), amt = Anzahl, m/t = gilt fuer Main/Twink.
// Eine id spaeter nicht mehr umbenennen, sonst sind die Haken des laufenden Zeitraums weg.
// Optional: section = Zwischenueberschrift in der Tabelle, until = bis dahin (Europe/Berlin) gilt die Aufgabe (z.B. Events).
// Limits "pro Server" teilen sich alle Charaktere - die stehen deshalb nur beim Main.
const CHECKLIST_TASKS = [
'daily' => [
['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<array<string, mixed>>
*/
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: ["<Aufgabe>|<Charakter-ID>" => true].
*
* @return array<string, true>
*/
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 ? '<img class="checklist-class-icon" src="' . e($url) . '" alt="" title="' . e((string) $character['class_name']) . '">' : '';
}
/**
* Persoenliche Notizen der Charaktere eines Mitglieds: [Charakter-ID => Text]. Nur fuer das Mitglied selbst.
*
* @return array<int, string>
*/
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 = '<div class="checklist-summary">';
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 .= '<div class="checklist-bar-label">' . e($label)
. ' <span data-checklist-count="' . $kind . '|' . (int) $c['id'] . '">' . $p['done'] . '/' . $p['total'] . '</span></div>'
. '<div class="checklist-bar' . ($p['done'] === $p['total'] ? ' is-full' : '') . '" data-checklist-bar="' . $kind . '|' . (int) $c['id'] . '">'
. '<i style="width:' . $percent . '%"></i></div>';
}
$html .= '<div class="checklist-char checklist-char-' . $c['role'] . ($full ? ' is-full' : '') . '" data-checklist-char="' . (int) $c['id'] . '">'
. '<div class="checklist-char-head"><strong>' . checklist_class_icon($c) . e((string) $c['name']) . '</strong>'
. '<span class="checklist-role">' . e(CHECKLIST_ROLES[$c['role']]) . '</span></div>'
. $bars
. '<p class="checklist-char-note" data-checklist-note-view="' . (int) $c['id'] . '"' . (($notes[(int) $c['id']] ?? '') === '' ? ' hidden' : '') . '>'
. e($notes[(int) $c['id']] ?? '') . '</p></div>';
}
return $html . '</div>';
}
/** 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 .= '<div class="checklist-section">' . e($section) . '</div>';
}
$cells = '';
$all = true;
foreach ($characters as $c) {
if (!checklist_applies($task, $c['role'])) {
$cells .= '<span class="checklist-dash" title="Nicht für ' . e(CHECKLIST_ROLES[$c['role']]) . 's">–</span>';
continue;
}
$on = isset($ticks[$task['id'] . '|' . $c['id']]);
$total++;
$done += $on ? 1 : 0;
$all = $all && $on;
$cells .= '<input type="checkbox" class="checklist-check"' . ($on ? ' checked' : '')
. ' data-task="' . e($task['id']) . '" data-char="' . (int) $c['id'] . '" data-kind="' . $kind . '"'
. ' aria-label="' . e($task['name'] . ' (' . $task['amt'] . ') – ' . $c['name']) . '">';
}
$rows .= '<div class="checklist-row' . ($all ? ' is-done' : '') . '"><span class="checklist-task">'
. '<span class="checklist-amt">' . e($task['amt']) . '</span>' . e($task['name']) . '</span>' . $cells . '</div>';
}
$head = '';
foreach ($characters as $c) {
$head .= '<span class="checklist-col-' . $c['role'] . '" title="' . e($c['name'] . ' (' . ($c['class_name'] ? $c['class_name'] . ', ' : '') . CHECKLIST_ROLES[$c['role']] . ')') . '">'
. checklist_class_icon($c) . e((string) $c['name']) . '</span>';
}
$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 '<div class="checklist-card" data-checklist-kind="' . $kind . '">'
. '<div class="checklist-card-head"><h3>' . e(CHECKLIST_KIND_LABELS[$kind]) . '</h3>'
. '<span class="checklist-pill" data-checklist-pill="' . $kind . '">' . $done . '/' . $total . '</span></div>'
. '<div class="checklist-reset' . ($urgent ? ' is-urgent' : '') . '" data-checklist-reset="' . $next . '" data-checklist-urgent="' . ($kind === 'daily' ? 3 * 3600 : 86400) . '">'
. 'Reset in <span>' . e(checklist_countdown($next)) . '</span></div>'
. '<div class="checklist-table" style="--checklist-cols:' . count($characters) . '">'
. '<div class="checklist-row checklist-row-head"><span>Aufgabe</span>' . $head . '</div>' . $rows
. '<div class="checklist-empty">Alles erledigt 🎉</div></div></div>';
}
/** Hinweis, wenn noch keine Charaktere da sind. */
function checklist_no_characters_html(): string
{
$link = user_has_permission('members.view')
? '<a href="/member_edit.php#charaktere">Lege auf deiner Profilseite einen Charakter an</a>'
: 'Lege auf deiner Profilseite einen Charakter an';
return '<p class="text-muted">' . $link . ' – dann erscheinen hier seine Aufgaben zum Abhaken.</p>';
}