LuminaWebsite/raids_live.php

102 lines
3.6 KiB
PHP

<?php
/**
* Live-Aktualisierung der Raid-Seite (assets/js/raids.js fragt hier regelmaessig nach).
*
* GET /raids_live.php?status=active&known=<raid_id>:<rev>,<raid_id>:<rev>,...
*
* Antwort: Reihenfolge der Raids, ihr aktueller Fingerabdruck (rev) und fertig gerenderte Karten
* fuer alle Raids, deren Fingerabdruck sich gegenueber "known" geaendert hat. Aenderungen aus dem
* Discord kommen so ebenfalls an, weil die Daten wie auf der Seite live von der Raid-API stammen.
*/
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raid_card.php';
// Wird per fetch() aufgerufen - Fehler deshalb als JSON statt Weiterleitung auf die Login-Seite.
if (!current_user()) {
http_response_code(401);
json_response(false, 'Bitte neu anmelden.');
}
// Bereich wie auf raids.php: Legion oder Consilium Elyseum - nur, wenn man ihn sehen darf
$area = (string) ($_GET['bereich'] ?? 'legion');
if (!in_array($area, ['legion', 'consilium'], true) || !raid_area_visible($area)) {
http_response_code(403);
json_response(false, 'Dir fehlt die Berechtigung, Raids zu sehen.');
}
$status = (string) ($_GET['status'] ?? 'active');
if (!in_array($status, RAID_STATUSES, true)) {
$status = 'active';
}
$known = [];
foreach (explode(',', (string) ($_GET['known'] ?? '')) as $pair) {
$parts = explode(':', trim($pair), 2);
if (count($parts) === 2 && ctype_digit($parts[0])) {
$known[$parts[0]] = $parts[1];
}
}
$currentUser = current_user();
// Rechte je Bereich wie auf raids.php
$canManageRaids = raid_area_can_manage_all($area);
$canSetRaidPoints = $area === 'legion' && user_has_permission('raids.points');
$canCreateRaids = raid_area_can_create($area);
// Die Formulare in den Karten brauchen das CSRF-Token der Sitzung - danach die Sitzung freigeben,
// damit eine langsame Raid-API andere Tabs derselben Person nicht blockiert.
csrf_token();
session_write_close();
try {
$raids = raids_in_area(fetch_raids($status), $area);
} catch (Throwable $e) {
http_response_code(502);
json_response(false, 'Raid-Daten konnten nicht geladen werden.');
}
try {
$raidSettings = raid_settings_all();
} catch (PDOException $e) {
$raidSettings = [];
}
$order = [];
$revs = [];
$changed = [];
foreach ($raids as $raid) {
$raidId = (string) (int) ($raid['raid_id'] ?? 0);
$rev = raid_card_rev($raid, $raidSettings[$raidId] ?? null);
$order[] = $raidId;
$revs[$raidId] = $rev;
if (isset($known[$raidId]) && $known[$raidId] !== $rev) {
$changed[$raidId] = $raid;
}
}
$cards = [];
if (!empty($changed)) {
$premades = [];
if ($canCreateRaids) {
try {
$premades = premades_visible_to(fetch_premades_cached(), (int) ($currentUser['discord_id'] ?? 0));
} catch (Throwable $e) {
$premades = [];
}
}
$context = [
'status' => $status,
'current_discord_id' => $currentUser['discord_id'] ?? null,
'current_user_id' => (int) $currentUser['id'],
'can_manage' => $canManageRaids,
'can_set_points' => $canSetRaidPoints,
'settings' => $raidSettings,
'premades' => $premades,
'premade_banners' => premade_banners_by_name(),
'new_since' => isset($_SESSION['raids_new_since']) ? (int) $_SESSION['raids_new_since'] : null,
];
foreach ($changed as $raidId => $raid) {
ob_start();
render_raid_card($raid, $context);
$cards[$raidId] = trim((string) ob_get_clean());
}
}
header('Cache-Control: no-store');
json_response(true, '', ['order' => $order, 'revs' => $revs, 'cards' => (object) $cards]);