LuminaWebsite/map.php
Maaxxs 444969f8dc Karte: don't fail with HTTP 500 when the map tables are missing
An installation that predates the map update has none of the four map
tables, so the first query threw an uncaught PDOException and the page
answered 500 with an empty body - no hint what was wrong.

map.php now checks for the tables up front and shows a setup page instead:
it names the missing tables and, for accounts with roles.manage, offers a
button that creates them. The statements are extracted from
sql/schema.sql at runtime, so the schema file stays the single source of
truth and cannot drift from what gets created. They are all CREATE TABLE IF
NOT EXISTS, so running it twice is harmless, and doing it by hand in
phpMyAdmin remains equivalent.

Reproduced against a database built from the pre-update schema: map.php
returned 500, now returns 200 with the setup page, the button creates all
four tables, and the map renders afterwards.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 14:39:55 +02:00

399 lines
22 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
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/map.php';
require_permission('map.view');
$canManageMap = user_has_permission('map.manage');
$canEditTactics = user_has_permission('map.tactics');
// Bestehende Installationen kennen die Kartentabellen noch nicht. Ohne diese Pruefung
// wuerde die erste Abfrage mit einer PDOException aussteigen - also lieber eine
// verstaendliche Seite mit dem noetigen Schritt zeigen.
$missingTables = map_missing_tables();
if (!empty($missingTables)) {
$pageTitle = 'Karte';
include __DIR__ . '/includes/header.php';
?>
<div class="panel">
<h1>Karte einrichten</h1>
<p class="text-muted">
Der Datenbank fehlen noch die Tabellen für die Karte
(<?= e(implode(', ', $missingTables)) ?>). Das ist bei jeder Installation so,
die es schon vor dem Karten-Update gab - einmalig anlegen, dann ist die Seite
einsatzbereit. Vorhandene Daten sind davon nicht betroffen.
</p>
<?php if (user_has_permission('roles.manage')): ?>
<form method="post" action="/map_install_action.php">
<?= csrf_field() ?>
<button type="submit">Tabellen jetzt anlegen</button>
</form>
<p class="text-muted map-hint">
Führt genau die <code>CREATE TABLE IF NOT EXISTS</code>-Befehle aus dem
Abschnitt „Taktikkarte" in <code>sql/schema.sql</code> aus.
</p>
<?php else: ?>
<p class="text-muted">Dafür fehlt dir die Berechtigung <code>roles.manage</code> - bitte einen Administrator.</p>
<?php endif; ?>
<h2>Oder von Hand</h2>
<p class="text-muted">
Den Abschnitt „Taktikkarte“ am Ende von <code>sql/schema.sql</code> in phpMyAdmin
ausführen - er enthält genau diese vier <code>CREATE TABLE</code>-Befehle.
</p>
</div>
<?php
include __DIR__ . '/includes/footer.php';
exit;
}
$maps = map_available();
$mapKey = (string) ($_GET['map'] ?? '');
if ($mapKey === '' || !isset($maps[$mapKey])) {
$mapKey = (string) array_key_first($maps);
}
$manifest = $maps[$mapKey] ?? null;
$categoryTree = $manifest ? map_category_tree($mapKey) : [];
$markers = $manifest ? map_markers($mapKey) : [];
$tactics = $manifest ? map_tactics($mapKey) : [];
// Flache Kategorienliste fuer die Auswahlfelder im Marker-Formular.
$flatCategories = [];
foreach ($categoryTree as $group) {
foreach ($group['children'] as $child) {
$flatCategories[] = [
'id' => (int) $child['id'],
'name' => (string) $child['name'],
'group' => (string) $group['name'],
'icon' => (string) $child['icon'],
'color' => (string) $child['color'],
];
}
}
$totalMarkers = count($markers);
$pageTitle = 'Karte';
$containerWide = true;
include __DIR__ . '/includes/header.php';
?>
<?php if (!$manifest): ?>
<div class="panel">
<h1>Taktikkarte</h1>
<p class="text-muted">
Es ist noch keine Karte hinterlegt. Die Karte wird als Kachel-Pyramide ausgeliefert
(mehrere Zoomstufen), damit sie auch nah herangezoomt scharf bleibt.
</p>
<h2>Karte einrichten</h2>
<ol class="map-setup-steps">
<li>Die Ingame-Karte in Bahnen abfotografieren - im Spiel maximal hineinzoomen, mit
ca. 30% Überlappung. Je mehr Pixel dabei zusammenkommen, desto weiter lässt sich
später scharf hineinzoomen.</li>
<li>Die Screenshots lokal zu einem Bild zusammensetzen:<br>
<code>python tools/stitch_map_screenshots.py shots/ -o verteron.png --crop=0,60,0,120</code></li>
<li>Daraus die Kacheln erzeugen (braucht die PHP-Erweiterung GD):<br>
<code>php tools/make_map_tiles.php verteron verteron.png --name="Verteron"</code></li>
<li>Den erzeugten Ordner <code>assets/uploads/maps/verteron/</code> per FTP hochladen.</li>
</ol>
<p class="text-muted">
Danach erscheint die Karte hier automatisch - mehrere Regionen nebeneinander funktionieren
genauso, jeder Unterordner mit einer <code>tiles.json</code> wird zu einer eigenen Karte.
</p>
</div>
<?php else: ?>
<div class="map-page"
data-map-key="<?= e($mapKey) ?>"
data-can-manage="<?= $canManageMap ? '1' : '0' ?>"
data-can-tactics="<?= $canEditTactics ? '1' : '0' ?>">
<div class="map-sidebar">
<div class="map-sidebar-head">
<?php if (count($maps) > 1): ?>
<select class="map-select" id="map-select">
<?php foreach ($maps as $key => $available): ?>
<option value="<?= e($key) ?>" <?= $key === $mapKey ? 'selected' : '' ?>><?= e((string) $available['name']) ?></option>
<?php endforeach; ?>
</select>
<?php else: ?>
<h1><?= e((string) $manifest['name']) ?></h1>
<?php endif; ?>
<input type="text" id="map-search" placeholder="Suchen …" autocomplete="off">
<div class="map-bulk-actions">
<button type="button" class="button-secondary" id="map-show-all">Alle einblenden</button>
<button type="button" class="button-secondary" id="map-hide-all">Alle ausblenden</button>
</div>
</div>
<div class="map-tabs">
<button type="button" class="map-tab active" data-panel="markers">Marker <span class="map-tab-count"><?= $totalMarkers ?></span></button>
<button type="button" class="map-tab" data-panel="tactics">Taktiken <span class="map-tab-count"><?= count($tactics) ?></span></button>
</div>
<div class="map-panel" data-panel="markers">
<?php if (empty($categoryTree)): ?>
<div class="map-empty">
<p class="text-muted">Noch keine Kategorien angelegt.</p>
<?php if ($canManageMap): ?>
<button type="button" class="button-success" id="map-seed-categories">Standard-Kategorien anlegen</button>
<p class="text-muted map-hint">Legt eine Gliederung nach Orten, Ressourcen, NPCs, Quests und Monstern an, die ihr danach frei anpassen könnt.</p>
<?php endif; ?>
</div>
<?php else: ?>
<div class="map-category-tree" id="map-category-tree">
<?php foreach ($categoryTree as $group): ?>
<div class="map-group" data-group-id="<?= (int) $group['id'] ?>">
<div class="map-group-head">
<button type="button" class="map-group-toggle" aria-expanded="true">
<?= icon('chevron-down') ?>
</button>
<label class="map-group-label">
<input type="checkbox" class="map-group-checkbox" checked>
<span class="map-group-icon"><?= e((string) $group['icon']) ?></span>
<span class="map-group-name"><?= e((string) $group['name']) ?></span>
</label>
<span class="map-count"><?= (int) $group['total_count'] ?></span>
<?php if ($canManageMap && (int) $group['id'] > 0): ?>
<button type="button" class="icon-button map-category-edit"
data-category-id="<?= (int) $group['id'] ?>"
data-parent-id="0"
data-name="<?= e((string) $group['name']) ?>"
data-icon="<?= e((string) $group['icon']) ?>"
data-color="<?= e((string) $group['color']) ?>"
title="Gruppe bearbeiten"><?= icon('edit') ?></button>
<?php endif; ?>
</div>
<div class="map-group-children">
<?php foreach ($group['children'] as $child): ?>
<div class="map-category" data-category-id="<?= (int) $child['id'] ?>" data-name="<?= e((string) $child['name']) ?>">
<label class="map-category-label">
<input type="checkbox" class="map-category-checkbox" value="<?= (int) $child['id'] ?>" checked>
<span class="map-category-icon" style="background: <?= e((string) $child['color']) ?>;"><?= e((string) $child['icon']) ?></span>
<span class="map-category-name"><?= e((string) $child['name']) ?></span>
</label>
<span class="map-count"><?= (int) $child['marker_count'] ?></span>
<?php if ($canManageMap): ?>
<button type="button" class="icon-button map-category-edit"
data-category-id="<?= (int) $child['id'] ?>"
data-parent-id="<?= (int) $group['id'] ?>"
data-name="<?= e((string) $child['name']) ?>"
data-icon="<?= e((string) $child['icon']) ?>"
data-color="<?= e((string) $child['color']) ?>"
title="Kategorie bearbeiten"><?= icon('edit') ?></button>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if ($canManageMap): ?>
<button type="button" class="button-secondary map-add-category" id="map-add-category">+ Kategorie</button>
<?php endif; ?>
<?php endif; ?>
</div>
<div class="map-panel" data-panel="tactics" hidden>
<?php if (empty($tactics)): ?>
<p class="text-muted" id="map-tactics-empty">Noch keine Taktiken angelegt.</p>
<?php endif; ?>
<div class="map-tactic-list" id="map-tactic-list">
<?php foreach ($tactics as $tactic): ?>
<div class="map-tactic" data-tactic-id="<?= (int) $tactic['id'] ?>">
<label class="map-tactic-label">
<input type="checkbox" class="map-tactic-checkbox" value="<?= (int) $tactic['id'] ?>">
<span class="map-tactic-name"><?= e($tactic['name']) ?></span>
</label>
<span class="map-count"><?= count($tactic['shapes']) ?></span>
<?php if ($canEditTactics): ?>
<button type="button" class="icon-button map-tactic-edit" title="Taktik bearbeiten"><?= icon('edit') ?></button>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php if ($canEditTactics): ?>
<button type="button" class="button-secondary map-add-tactic" id="map-add-tactic">+ Neue Taktik</button>
<?php endif; ?>
</div>
</div>
<div class="map-stage">
<div class="map-toolbar">
<?php if ($canManageMap): ?>
<button type="button" class="map-tool" id="map-tool-marker" title="Marker setzen: Werkzeug aktivieren und auf die Karte klicken">
<?= icon('map-pin') ?><span>Marker setzen</span>
</button>
<?php endif; ?>
<?php if ($canEditTactics): ?>
<div class="map-tool-group" id="map-tactic-tools" hidden>
<span class="map-tool-group-label" id="map-tactic-active-name"></span>
<button type="button" class="map-tool" data-shape="point" title="Punkt/Sammelpunkt setzen"><?= icon('map-pin') ?><span>Punkt</span></button>
<button type="button" class="map-tool" data-shape="route" title="Route zeichnen: klicken für Punkte, Doppelklick beendet"><?= icon('route') ?><span>Route</span></button>
<button type="button" class="map-tool" data-shape="area" title="Fläche zeichnen: klicken für Eckpunkte, Doppelklick beendet"><?= icon('area') ?><span>Fläche</span></button>
<button type="button" class="map-tool" data-shape="text" title="Textnotiz setzen"><?= icon('edit') ?><span>Notiz</span></button>
<input type="color" id="map-shape-color" value="#c9a227" title="Farbe für neue Objekte">
<button type="button" class="map-tool map-tool-finish" id="map-shape-finish" hidden
title="Route/Fläche abschließen (geht auch per Doppelklick oder Enter)">
<?= icon('check') ?><span>Abschließen</span>
</button>
<button type="button" class="map-tool map-tool-done" id="map-tactic-close" title="Taktik-Bearbeitung beenden"><?= icon('x') ?><span>Fertig</span></button>
</div>
<?php endif; ?>
<div class="map-toolbar-spacer"></div>
<span class="map-coords" id="map-coords" title="Position des Mauszeigers"></span>
<button type="button" class="map-tool" id="map-reset-view" title="Ansicht zurücksetzen"><?= icon('restore') ?><span>Ansicht</span></button>
</div>
<div id="map-canvas" class="map-canvas"></div>
<p class="map-status text-muted" id="map-status"></p>
</div>
</div>
<!-- Marker anlegen/bearbeiten -->
<div class="modal-overlay" id="map-marker-modal" hidden>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2 id="map-marker-modal-title">Marker anlegen</h2>
<form id="map-marker-form">
<input type="hidden" name="marker_id" value="">
<input type="hidden" name="x" value="">
<input type="hidden" name="y" value="">
<div>
<label>Name</label>
<input type="text" name="name" maxlength="120" placeholder="z. B. Orihalum-Ader">
</div>
<div>
<label>Kategorie</label>
<select name="category_id">
<?php
$lastGroup = null;
foreach ($flatCategories as $category):
if ($category['group'] !== $lastGroup):
if ($lastGroup !== null): ?></optgroup><?php endif; ?>
<optgroup label="<?= e($category['group']) ?>">
<?php $lastGroup = $category['group'];
endif; ?>
<option value="<?= (int) $category['id'] ?>"><?= e($category['icon'] . ' ' . $category['name']) ?></option>
<?php endforeach;
if ($lastGroup !== null): ?></optgroup><?php endif; ?>
</select>
</div>
<div>
<label>Beschreibung <span class="text-muted">(optional)</span></label>
<textarea name="description" rows="3" placeholder="z. B. Respawn ca. 5 Min., 3er-Gruppe empfohlen"></textarea>
</div>
<div class="actions">
<button type="submit">Speichern</button>
<button type="button" class="button-secondary" id="map-marker-cancel">Abbrechen</button>
<button type="button" class="icon-button icon-button-danger" id="map-marker-delete" title="Marker löschen" hidden><?= icon('trash') ?></button>
<span class="map-feedback text-muted"></span>
</div>
</form>
</div>
</div>
<!-- Kategorie anlegen/bearbeiten -->
<div class="modal-overlay" id="map-category-modal" hidden>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2 id="map-category-modal-title">Kategorie anlegen</h2>
<form id="map-category-form">
<input type="hidden" name="category_id" value="">
<div>
<label>Gruppe</label>
<select name="parent_id">
<option value="0">– Eigene Gruppe (Überschrift) –</option>
<?php foreach ($categoryTree as $group): ?>
<?php if ((int) $group['id'] > 0): ?>
<option value="<?= (int) $group['id'] ?>"><?= e((string) $group['name']) ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<div>
<label>Name</label>
<input type="text" name="name" maxlength="80" placeholder="z. B. Orihalum-Erz">
</div>
<div class="map-category-form-row">
<div>
<label>Symbol</label>
<input type="text" name="icon" maxlength="4" placeholder="⛏">
</div>
<div>
<label>Farbe</label>
<input type="color" name="color" value="#c9a227">
</div>
</div>
<div class="actions">
<button type="submit">Speichern</button>
<button type="button" class="button-secondary" id="map-category-cancel">Abbrechen</button>
<button type="button" class="icon-button icon-button-danger" id="map-category-delete" title="Kategorie mitsamt ihren Markern löschen" hidden><?= icon('trash') ?></button>
<span class="map-feedback text-muted"></span>
</div>
</form>
</div>
</div>
<!-- Beschriftung fuer ein neu gezeichnetes Taktik-Objekt -->
<div class="modal-overlay" id="map-shape-modal" hidden>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2 id="map-shape-modal-title">Objekt beschriften</h2>
<form id="map-shape-form">
<div>
<label>Beschriftung <span class="text-muted" id="map-shape-label-hint">(optional)</span></label>
<input type="text" name="label" maxlength="120" placeholder="z. B. Sammelpunkt">
</div>
<div>
<label>Farbe</label>
<input type="color" name="color" value="#c9a227">
</div>
<div class="actions">
<button type="submit">Übernehmen</button>
<button type="button" class="button-secondary" id="map-shape-cancel">Abbrechen</button>
<span class="map-feedback text-muted"></span>
</div>
</form>
</div>
</div>
<!-- Taktik anlegen/bearbeiten -->
<div class="modal-overlay" id="map-tactic-modal" hidden>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2 id="map-tactic-modal-title">Taktik anlegen</h2>
<form id="map-tactic-form">
<input type="hidden" name="tactic_id" value="">
<div>
<label>Name</label>
<input type="text" name="name" maxlength="120" placeholder="z. B. Festung Verteron - Angriff Süd">
</div>
<div>
<label>Beschreibung <span class="text-muted">(optional)</span></label>
<textarea name="description" rows="3" placeholder="Ablauf, Rollenverteilung, Timings …"></textarea>
</div>
<div class="actions">
<button type="submit">Speichern &amp; bearbeiten</button>
<button type="button" class="button-secondary" id="map-tactic-cancel">Abbrechen</button>
<button type="button" class="icon-button icon-button-danger" id="map-tactic-delete" title="Taktik löschen" hidden><?= icon('trash') ?></button>
<span class="map-feedback text-muted"></span>
</div>
</form>
</div>
</div>
<script id="map-data" type="application/json"><?= json_encode([
'manifest' => [
'width' => (int) $manifest['width'],
'height' => (int) $manifest['height'],
'tile_size' => (int) ($manifest['tile_size'] ?? 256),
'min_zoom' => (int) ($manifest['min_zoom'] ?? 0),
'max_zoom' => (int) ($manifest['max_zoom'] ?? 0),
'url_template' => (string) $manifest['url_template'],
'name' => (string) $manifest['name'],
],
'categories' => $flatCategories,
'markers' => $markers,
'tactics' => $tactics,
], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) ?></script>
<script src="/assets/vendor/leaflet/leaflet.js"></script>
<script src="/assets/js/map.js"></script>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>