LuminaWebsite/map.php
Maaxxs 52fea9f92d Karte: build a map from an image right on the page
Pick an image, press "Karte bauen", done - no FTP, no command line tool.
The panel sits on the setup page while no map exists and under the map as
"Karte neu bauen" afterwards.

The tiles are cut in the browser and only the finished 256px JPEGs are
uploaded. That is the point: on ordinary shared hosting a 50MB map image
runs into upload_max_filesize, cutting it server side runs into memory_limit
and max_execution_time, and GD is not installed everywhere. This way the
server needs none of it - it validates small JPEGs and stores them.

Uploads go into a side folder <key>__build that only replaces the live map
once every tile has arrived, so an abort - by button, by error or by closing
the browser - leaves the existing map untouched.

The server trusts nothing from the client: the key must match a strict
pattern, tile coordinates must lie inside 2^z on their level, and each file
is checked with getimagesize for an actual JPEG within a size limit before it
is stored.

Verified against tools/make_map_tiles.php on the same source image: same zoom
levels, same per-level tile counts (1/4/12/40), same tile names, same
manifest.

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

475 lines
26 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_build.php';
require_permission('map.view');
$canManageMap = user_has_permission('map.manage');
$canEditTactics = user_has_permission('map.tactics');
/**
* Panel zum Bauen einer Karte aus einem Bild. Das Schneiden passiert im Browser
* (assets/js/map_build.js) - hochgeladen werden nur die fertigen Kacheln, damit weder
* upload_max_filesize noch memory_limit oder max_execution_time im Weg stehen.
*/
function render_map_build_panel(?string $existingKey = null): void
{
?>
<div class="map-build" id="map-build">
<h2><?= $existingKey === null ? 'Karte aus einem Bild bauen' : 'Karte neu bauen' ?></h2>
<p class="text-muted">
Bild auswählen, Knopf drücken - der Rest passiert von allein: die Zoomstufen
werden im Browser geschnitten und einzeln hochgeladen. Kein FTP, kein
Kommandozeilen-Werkzeug nötig.
<?php if ($existingKey !== null): ?>
Die bisherige Karte bleibt unverändert, bis die neue vollständig oben ist.
<?php endif; ?>
</p>
<label class="map-build-drop" id="map-build-drop" for="map-build-file">
<input type="file" id="map-build-file" accept="image/png,image/jpeg,image/webp" hidden>
<span class="map-build-drop-text">Bild hierher ziehen oder klicken zum Auswählen</span>
<span class="text-muted">PNG, JPEG oder WebP</span>
</label>
<p class="map-build-info text-muted" id="map-build-info"></p>
<div class="map-build-fields">
<div>
<label>Kartenschlüssel</label>
<input type="text" id="map-build-key" maxlength="49" placeholder="verteron"
value="<?= e((string) $existingKey) ?>" <?= $existingKey !== null ? 'readonly' : '' ?>>
<p class="text-muted map-build-hint">Ordnername, nur a-z, 0-9, - und _</p>
</div>
<div>
<label>Anzeigename</label>
<input type="text" id="map-build-name" maxlength="60" placeholder="Verteron">
</div>
<div>
<label>JPEG-Qualität</label>
<input type="text" id="map-build-quality" value="88">
<p class="text-muted map-build-hint">40-100, höher = schärfer und größer</p>
</div>
</div>
<div class="map-build-progress">
<div class="map-build-track"><div class="map-build-bar" id="map-build-bar"></div></div>
<span class="text-muted" id="map-build-progress"></span>
</div>
<div class="actions">
<button type="button" id="map-build-start" disabled>Karte bauen</button>
<button type="button" class="button-secondary" id="map-build-abort" hidden>Abbrechen</button>
</div>
</div>
<?php
}
// 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>Alternativ: über die Kommandozeile</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</code><br>
<span class="text-muted">Feste UI-Ränder (Chat, Minimap) bei Bedarf mit
<code>--crop=links,oben,rechts,unten</code> abschneiden - die Werte an den
eigenen Screenshots abmessen, nicht raten.</span></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 if ($canManageMap): ?>
<div class="panel">
<?php render_map_build_panel(); ?>
</div>
<script src="/assets/js/map_build.js"></script>
<?php endif; ?>
<?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>
<?php if ($canManageMap): ?>
<details class="panel map-build-panel">
<summary>Karte neu bauen</summary>
<?php render_map_build_panel($mapKey); ?>
</details>
<script src="/assets/js/map_build.js"></script>
<?php endif; ?>
<script src="/assets/js/map.js"></script>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>