LuminaWebsite/map_action.php
Maaxxs 6b5661dd53 Karte: tactical map tab with own markers and tactic layers
New "Karte" tab: a tiled zoom/pan map of the game world with a category
sidebar (groups, per-category marker counts, show all / hide all, search)
and marker popups, plus tactic layers drawn on top of it.

Markers are placed by clicking the map (map.manage) and live in freely
editable two-level categories, each with its own emoji and colour. A tactic
(map.tactics) is a separate drawing layer that can be toggled on and off:
rally points, dashed routes, areas and text notes in any colour, finished by
double click, an explicit button or Enter, removed by clicking them.

Coordinates are stored as a fraction of the map's width/height rather than
in pixels, so re-tiling the map at a different resolution never moves
anything.

The map data lives entirely in the website database - the Discord bot is not
involved. tools/make_map_tiles.php slices a high-resolution image into the
zoom pyramid locally; the resulting folder is uploaded by FTP and picked up
automatically, one region per folder. Leaflet 1.9.4 is vendored under
assets/vendor/ so there is still no CDN and no build step.

New permissions map.view / map.manage / map.tactics and four new tables -
existing installs have to add both once (see README).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 13:25:12 +02:00

294 lines
13 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/map.php';
require_permission('map.view');
$input = read_json_body();
verify_csrf_token($input['csrf_token'] ?? '');
$action = (string) ($input['action'] ?? '');
$user = current_user();
$userId = (int) $user['id'];
$canManage = user_has_permission('map.manage');
$canTactics = user_has_permission('map.tactics');
$mapKey = trim((string) ($input['map_key'] ?? ''));
if ($mapKey === '' || !array_key_exists($mapKey, map_available())) {
json_response(false, 'Unbekannte Karte.');
}
/** Bricht mit einer Fehlermeldung ab, wenn die noetige Berechtigung fehlt. */
function map_require(bool $allowed, string $message): void
{
if (!$allowed) {
json_response(false, $message);
}
}
try {
switch ($action) {
// ---- Marker ----
case 'marker_save':
map_require($canManage, 'Du darfst keine Marker verwalten.');
$markerId = (int) ($input['marker_id'] ?? 0);
$categoryId = (int) ($input['category_id'] ?? 0);
$name = trim((string) ($input['name'] ?? ''));
$description = trim((string) ($input['description'] ?? ''));
$x = $input['x'] ?? null;
$y = $input['y'] ?? null;
if ($name === '') {
json_response(false, 'Bitte einen Namen für den Marker angeben.');
}
if (mb_strlen($name) > 120) {
json_response(false, 'Der Name darf höchstens 120 Zeichen lang sein.');
}
if (!map_valid_coordinate($x) || !map_valid_coordinate($y)) {
json_response(false, 'Die Position liegt außerhalb der Karte.');
}
// Nur Kategorien der zweiten Ebene koennen Marker tragen - eine Gruppe
// ("Ressourcen") ist nur eine Ueberschrift in der Sidebar.
$catStmt = db()->prepare('SELECT parent_id FROM map_categories WHERE id = ?');
$catStmt->execute([$categoryId]);
$category = $catStmt->fetch();
if (!$category) {
json_response(false, 'Bitte eine Kategorie auswählen.');
}
if ($category['parent_id'] === null) {
json_response(false, 'Bitte eine Unterkategorie auswählen, keine Gruppe.');
}
if ($markerId > 0) {
$stmt = db()->prepare(
'UPDATE map_markers SET category_id = ?, name = ?, description = ?, pos_x = ?, pos_y = ?, updated_at = NOW()
WHERE id = ? AND map_key = ?'
);
$stmt->execute([$categoryId, $name, $description, (float) $x, (float) $y, $markerId, $mapKey]);
json_response(true, 'Marker gespeichert.', ['marker_id' => $markerId]);
}
$stmt = db()->prepare(
'INSERT INTO map_markers (map_key, category_id, name, description, pos_x, pos_y, created_by)
VALUES (?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([$mapKey, $categoryId, $name, $description, (float) $x, (float) $y, $userId]);
json_response(true, 'Marker angelegt.', ['marker_id' => (int) db()->lastInsertId()]);
// no break - json_response() beendet den Request
case 'marker_move':
map_require($canManage, 'Du darfst keine Marker verwalten.');
$markerId = (int) ($input['marker_id'] ?? 0);
$x = $input['x'] ?? null;
$y = $input['y'] ?? null;
if ($markerId <= 0 || !map_valid_coordinate($x) || !map_valid_coordinate($y)) {
json_response(false, 'Ungültige Position.');
}
$stmt = db()->prepare('UPDATE map_markers SET pos_x = ?, pos_y = ?, updated_at = NOW() WHERE id = ? AND map_key = ?');
$stmt->execute([(float) $x, (float) $y, $markerId, $mapKey]);
json_response(true, 'Marker verschoben.');
case 'marker_delete':
map_require($canManage, 'Du darfst keine Marker verwalten.');
$markerId = (int) ($input['marker_id'] ?? 0);
if ($markerId <= 0) {
json_response(false, 'Ungültiger Marker.');
}
$stmt = db()->prepare('DELETE FROM map_markers WHERE id = ? AND map_key = ?');
$stmt->execute([$markerId, $mapKey]);
json_response(true, 'Marker gelöscht.');
// ---- Kategorien ----
case 'category_save':
map_require($canManage, 'Du darfst keine Kategorien verwalten.');
$categoryId = (int) ($input['category_id'] ?? 0);
$parentId = (int) ($input['parent_id'] ?? 0);
$name = trim((string) ($input['name'] ?? ''));
$icon = trim((string) ($input['icon'] ?? ''));
$color = trim((string) ($input['color'] ?? '#c9a227'));
if ($name === '' || mb_strlen($name) > 80) {
json_response(false, 'Bitte einen Namen mit höchstens 80 Zeichen angeben.');
}
if (mb_strlen($icon) > 4) {
json_response(false, 'Das Symbol darf höchstens 4 Zeichen lang sein (z.B. ein Emoji).');
}
if (!map_valid_color($color)) {
json_response(false, 'Ungültige Farbe.');
}
// Nur zwei Ebenen: eine Gruppe darf keine Gruppe als Elternteil haben.
if ($parentId > 0) {
$parentStmt = db()->prepare('SELECT parent_id FROM map_categories WHERE id = ?');
$parentStmt->execute([$parentId]);
$parent = $parentStmt->fetch();
if (!$parent || $parent['parent_id'] !== null) {
json_response(false, 'Ungültige Gruppe ausgewählt.');
}
}
if ($categoryId > 0) {
$stmt = db()->prepare('UPDATE map_categories SET parent_id = ?, name = ?, icon = ?, color = ? WHERE id = ?');
$stmt->execute([$parentId > 0 ? $parentId : null, $name, $icon, $color, $categoryId]);
json_response(true, 'Kategorie gespeichert.');
}
$sortStmt = db()->prepare('SELECT COALESCE(MAX(sort_order), 0) + 10 FROM map_categories WHERE parent_id <=> ?');
$sortStmt->execute([$parentId > 0 ? $parentId : null]);
$sortOrder = (int) $sortStmt->fetchColumn();
$stmt = db()->prepare('INSERT INTO map_categories (parent_id, name, icon, color, sort_order) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([$parentId > 0 ? $parentId : null, $name, $icon, $color, $sortOrder]);
json_response(true, 'Kategorie angelegt.', ['category_id' => (int) db()->lastInsertId()]);
case 'category_delete':
map_require($canManage, 'Du darfst keine Kategorien verwalten.');
$categoryId = (int) ($input['category_id'] ?? 0);
if ($categoryId <= 0) {
json_response(false, 'Ungültige Kategorie.');
}
// Loescht per ON DELETE CASCADE auch Unterkategorien und deren Marker.
$stmt = db()->prepare('DELETE FROM map_categories WHERE id = ?');
$stmt->execute([$categoryId]);
json_response(true, 'Kategorie gelöscht.');
case 'category_seed':
map_require($canManage, 'Du darfst keine Kategorien verwalten.');
$existing = (int) db()->query('SELECT COUNT(*) FROM map_categories')->fetchColumn();
if ($existing > 0) {
json_response(false, 'Es gibt bereits Kategorien - die Startbestückung ist nur für eine leere Liste gedacht.');
}
$created = map_seed_default_categories();
json_response(true, $created . ' Kategorien wurden angelegt.');
// ---- Taktiken ----
case 'tactic_save':
map_require($canTactics, 'Du darfst keine Taktiken verwalten.');
$tacticId = (int) ($input['tactic_id'] ?? 0);
$name = trim((string) ($input['name'] ?? ''));
$description = trim((string) ($input['description'] ?? ''));
if ($name === '' || mb_strlen($name) > 120) {
json_response(false, 'Bitte einen Namen mit höchstens 120 Zeichen angeben.');
}
if ($tacticId > 0) {
$stmt = db()->prepare('UPDATE map_tactics SET name = ?, description = ?, updated_at = NOW() WHERE id = ? AND map_key = ?');
$stmt->execute([$name, $description, $tacticId, $mapKey]);
json_response(true, 'Taktik gespeichert.', ['tactic_id' => $tacticId]);
}
$stmt = db()->prepare('INSERT INTO map_tactics (map_key, name, description, created_by) VALUES (?, ?, ?, ?)');
$stmt->execute([$mapKey, $name, $description, $userId]);
json_response(true, 'Taktik angelegt.', ['tactic_id' => (int) db()->lastInsertId()]);
case 'tactic_delete':
map_require($canTactics, 'Du darfst keine Taktiken verwalten.');
$tacticId = (int) ($input['tactic_id'] ?? 0);
if ($tacticId <= 0) {
json_response(false, 'Ungültige Taktik.');
}
$stmt = db()->prepare('DELETE FROM map_tactics WHERE id = ? AND map_key = ?');
$stmt->execute([$tacticId, $mapKey]);
json_response(true, 'Taktik gelöscht.');
case 'shape_save':
map_require($canTactics, 'Du darfst keine Taktiken verwalten.');
$tacticId = (int) ($input['tactic_id'] ?? 0);
$shapeId = (int) ($input['shape_id'] ?? 0);
$type = (string) ($input['type'] ?? '');
$label = trim((string) ($input['label'] ?? ''));
$color = trim((string) ($input['color'] ?? '#c9a227'));
$points = $input['points'] ?? null;
if (!in_array($type, ['point', 'route', 'area', 'text'], true)) {
json_response(false, 'Unbekannter Objekttyp.');
}
if (!map_valid_color($color)) {
json_response(false, 'Ungültige Farbe.');
}
if (mb_strlen($label) > 120) {
json_response(false, 'Die Beschriftung darf höchstens 120 Zeichen lang sein.');
}
if (!is_array($points) || empty($points)) {
json_response(false, 'Dem Objekt fehlen Punkte.');
}
// Routen/Flaechen brauchen mehrere Stuetzpunkte, Punkte/Texte genau einen.
$minPoints = in_array($type, ['route', 'area'], true) ? 2 : 1;
if (count($points) < $minPoints) {
json_response(false, 'Bitte mindestens ' . $minPoints . ' Punkte setzen.');
}
$clean = [];
foreach ($points as $point) {
if (!is_array($point) || count($point) !== 2
|| !map_valid_coordinate($point[0]) || !map_valid_coordinate($point[1])) {
json_response(false, 'Ein Punkt liegt außerhalb der Karte.');
}
$clean[] = [round((float) $point[0], 6), round((float) $point[1], 6)];
}
$ownerStmt = db()->prepare('SELECT id FROM map_tactics WHERE id = ? AND map_key = ?');
$ownerStmt->execute([$tacticId, $mapKey]);
if (!$ownerStmt->fetch()) {
json_response(false, 'Die Taktik wurde nicht gefunden.');
}
$encoded = json_encode($clean, JSON_UNESCAPED_UNICODE);
if ($shapeId > 0) {
$stmt = db()->prepare(
'UPDATE map_tactic_shapes SET shape_type = ?, label = ?, color = ?, points = ?
WHERE id = ? AND tactic_id = ?'
);
$stmt->execute([$type, $label, $color, $encoded, $shapeId, $tacticId]);
} else {
$sortStmt = db()->prepare('SELECT COALESCE(MAX(sort_order), 0) + 10 FROM map_tactic_shapes WHERE tactic_id = ?');
$sortStmt->execute([$tacticId]);
$sortOrder = (int) $sortStmt->fetchColumn();
$stmt = db()->prepare(
'INSERT INTO map_tactic_shapes (tactic_id, shape_type, label, color, points, sort_order)
VALUES (?, ?, ?, ?, ?, ?)'
);
$stmt->execute([$tacticId, $type, $label, $color, $encoded, $sortOrder]);
$shapeId = (int) db()->lastInsertId();
}
db()->prepare('UPDATE map_tactics SET updated_at = NOW() WHERE id = ?')->execute([$tacticId]);
json_response(true, 'Objekt gespeichert.', ['shape_id' => $shapeId]);
case 'shape_delete':
map_require($canTactics, 'Du darfst keine Taktiken verwalten.');
$shapeId = (int) ($input['shape_id'] ?? 0);
if ($shapeId <= 0) {
json_response(false, 'Ungültiges Objekt.');
}
// Ueber den Join sicherstellen, dass das Objekt wirklich zu dieser Karte gehoert.
$stmt = db()->prepare(
'DELETE s FROM map_tactic_shapes s
JOIN map_tactics t ON t.id = s.tactic_id
WHERE s.id = ? AND t.map_key = ?'
);
$stmt->execute([$shapeId, $mapKey]);
json_response(true, 'Objekt gelöscht.');
default:
json_response(false, 'Unbekannte Aktion.');
}
} catch (Throwable $e) {
json_response(false, 'Aktion fehlgeschlagen: ' . $e->getMessage());
}