Karte: tactics in steps drawn on the map, show/hide all categories
The category panel gets "Alle anzeigen" / "Alle verstecken" buttons. Tactics belong to a region and are split into steps, each its own drawing layer: pen, arrow, line, area, circle, labelled point, text and eraser, with colour, stroke width and undo. A new step can start empty or as a copy of the current one, earlier steps can be shown faded underneath, and steps can be renamed, reordered and deleted. Viewing needs map.view, editing map.tactics. Drawings are stored per shape in world coordinates (the same as the markers) through tactics_api.php in three new tables; the map page offers to create them when they are missing. The viewer's own code only gains two hooks (view change, map change) and the show/hide-all method. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
c5213b5736
commit
1278859f30
16 changed files with 2388 additions and 13 deletions
56
README.md
56
README.md
|
|
@ -19,7 +19,8 @@ und wird später ausgebaut. Fokus dieser Version ist der Mitglieder-/Admin-Berei
|
||||||
- Events-Seite: geplante (wiederkehrende) Event-Erinnerungen des Bots ansehen, anlegen,
|
- Events-Seite: geplante (wiederkehrende) Event-Erinnerungen des Bots ansehen, anlegen,
|
||||||
bearbeiten und löschen - synchron mit `/event_planen` / `/event_liste` im Discord
|
bearbeiten und löschen - synchron mit `/event_planen` / `/event_liste` im Discord
|
||||||
- Karte: interaktive Zoom-Karte aller Regionen (Verteron, Altgard, Elthen, Morheim, Poeta,
|
- Karte: interaktive Zoom-Karte aller Regionen (Verteron, Altgard, Elthen, Morheim, Poeta,
|
||||||
Ishalgen, Reshanta) mit Markern und Kategorie-Filtern
|
Ishalgen, Reshanta) mit Markern und Kategorie-Filtern, dazu Taktiken in Schritten, die
|
||||||
|
direkt auf die Karte gezeichnet werden
|
||||||
- Raids-Seite: zeigt anstehende Raid-Termine, Anforderungen, Gruppen und Anmeldungen,
|
- Raids-Seite: zeigt anstehende Raid-Termine, Anforderungen, Gruppen und Anmeldungen,
|
||||||
bezogen von einer externen API (`config.php: raids_api`), Filter Aktiv/Archiviert/Alle
|
bezogen von einer externen API (`config.php: raids_api`), Filter Aktiv/Archiviert/Alle
|
||||||
- "Mein Profil"-Seite: Anzeigename ändern, Passwort ändern, Discord-Verknüpfung verwalten
|
- "Mein Profil"-Seite: Anzeigename ändern, Passwort ändern, Discord-Verknüpfung verwalten
|
||||||
|
|
@ -261,14 +262,14 @@ Bot-Prozess neu gestartet werden.
|
||||||
Die Seite **Karte** (`map.php`) zeigt eine interaktive Karte aller Regionen: Verteron,
|
Die Seite **Karte** (`map.php`) zeigt eine interaktive Karte aller Regionen: Verteron,
|
||||||
Altgard, Elthen, Morheim, Poeta, Ishalgen, Abyss Reshanta A/B sowie Chaotic Middle/Upper
|
Altgard, Elthen, Morheim, Poeta, Ishalgen, Abyss Reshanta A/B sowie Chaotic Middle/Upper
|
||||||
Reshanta. Oben wird die Region gewählt, unten links lassen sich die Marker-Kategorien
|
Reshanta. Oben wird die Region gewählt, unten links lassen sich die Marker-Kategorien
|
||||||
(Orte, Monster, NPCs, Ressourcen, Sammelbares, Quests …) einzeln oder gruppenweise ein- und
|
(Orte, Monster, NPCs, Ressourcen, Sammelbares, Quests …) einzeln, gruppenweise oder mit
|
||||||
ausblenden, beim Überfahren eines Markers erscheint sein Name. Zoomen per Mausrad, `+`/`-`
|
„Alle anzeigen“/„Alle verstecken“ auf einmal ein- und ausblenden, beim Überfahren eines Markers erscheint sein Name. Zoomen per Mausrad, `+`/`-`
|
||||||
oder die Knöpfe oben rechts, verschieben per Ziehen, `0` setzt die Ansicht zurück.
|
oder die Knöpfe oben rechts, verschieben per Ziehen, `0` setzt die Ansicht zurück.
|
||||||
|
|
||||||
Der Viewer ist ein eigenständiges Paket (HTML/CSS/JS ohne Bibliotheken) unter `assets/map/`
|
Der Viewer ist ein eigenständiges Paket (HTML/CSS/JS ohne Bibliotheken) unter `assets/map/`
|
||||||
und wird von `map.php` per iframe eingebettet - so greift sein Stylesheet nicht auf das
|
und wird von `map.php` per iframe eingebettet - so greift sein Stylesheet nicht auf das
|
||||||
Seitenlayout über. Die Seite braucht die Berechtigung `map.view`. Datenbank und Discord-Bot
|
Seitenlayout über. Die Seite braucht die Berechtigung `map.view`. Die Karte selbst braucht
|
||||||
sind nicht beteiligt.
|
weder Datenbank noch Discord-Bot, nur die Taktiken (siehe unten) liegen in der Datenbank.
|
||||||
|
|
||||||
Kacheln, Marker und Icons liegen unter `assets/map/data/` (rund 230 MB, knapp 39.000
|
Kacheln, Marker und Icons liegen unter `assets/map/data/` (rund 230 MB, knapp 39.000
|
||||||
Dateien) und sind wegen der Größe von Git ausgenommen. Beim Deployen muss dieser Ordner mit
|
Dateien) und sind wegen der Größe von Git ausgenommen. Beim Deployen muss dieser Ordner mit
|
||||||
|
|
@ -280,10 +281,47 @@ stehen in `assets/map/README.md`.
|
||||||
Für Verteron, Altgard, Elthen, Morheim, Abyss Reshanta A sowie Chaotic Middle/Upper Reshanta
|
Für Verteron, Altgard, Elthen, Morheim, Abyss Reshanta A sowie Chaotic Middle/Upper Reshanta
|
||||||
gibt es Marker, Poeta, Ishalgen und Abyss Reshanta B zeigen nur die Karte.
|
gibt es Marker, Poeta, Ishalgen und Abyss Reshanta B zeigen nur die Karte.
|
||||||
|
|
||||||
Die frühere Taktikkarte (eigene Marker, Taktik-Ebenen, Kartenbauer) wurde dadurch ersetzt.
|
#### Taktiken
|
||||||
Ihre Tabellen `map_categories`, `map_markers`, `map_tactics` und `map_tactic_shapes` sowie
|
|
||||||
die Berechtigungen `map.manage` und `map.tactics` werden nicht mehr benutzt und können bei
|
Rechts oben auf der Karte liegt das Panel **Taktiken**. Eine Taktik gehört zu einer Region und
|
||||||
bestehenden Installationen gelöscht werden.
|
ist in **Schritte** gegliedert (Schritt 1, Schritt 2, …). Jeder Schritt ist eine eigene
|
||||||
|
Zeichenebene - so lässt sich ein Ablauf Stück für Stück zeigen: erst sammeln, dann vorrücken,
|
||||||
|
dann angreifen.
|
||||||
|
|
||||||
|
- **Ansehen** (Berechtigung `map.view`): Taktik in der Liste anklicken, dann mit der
|
||||||
|
Schritt-Leiste unten, den Pfeiltasten ←/→ oder per Klick in der Schrittliste blättern.
|
||||||
|
„Vorherige Schritte blass zeigen“ legt die Zeichnungen der früheren Schritte halb
|
||||||
|
durchsichtig darunter.
|
||||||
|
- **Anlegen und zeichnen** (Berechtigung `map.tactics`): „+ Neu“ legt eine Taktik mit
|
||||||
|
Schritt 1 an. „+ Schritt“ hängt einen leeren Schritt an, „+ Kopie dieses Schritts“ einen mit
|
||||||
|
den Zeichnungen des aktuellen - dann muss nur noch eingezeichnet werden, was sich ändert.
|
||||||
|
Schritte lassen sich umbenennen, beschreiben, verschieben (↑/↓) und löschen.
|
||||||
|
|
||||||
|
Werkzeuge: Verschieben (✋), Stift, Pfeil, Linie, Fläche (Eckpunkte klicken, Doppelklick oder
|
||||||
|
Enter schließt ab), Kreis, Punkt mit Beschriftung, Text und Radierer (Zeichnung anklicken),
|
||||||
|
dazu Farbe und Strichstärke. ↶ bzw. Strg+Z nimmt die eigenen Zeichnungen der laufenden
|
||||||
|
Sitzung im aktuellen Schritt zurück, Esc bricht ab und schaltet zurück aufs Verschieben.
|
||||||
|
Zoomen per Mausrad und Verschieben mit rechter oder mittlerer Maustaste gehen auch mitten im
|
||||||
|
Zeichnen.
|
||||||
|
|
||||||
|
Jede Zeichnung wird sofort gespeichert und ist für alle sichtbar. Die Punkte liegen in den
|
||||||
|
Weltkoordinaten der Karte (dieselben wie die Marker), Zoomstufe und Fenstergröße spielen
|
||||||
|
also keine Rolle.
|
||||||
|
|
||||||
|
Technik: `tactics_api.php` liefert und speichert die Taktiken als JSON (Anmeldung und
|
||||||
|
CSRF-Token kommen von der umgebenden Seite), die Oberfläche steckt in
|
||||||
|
`assets/map/js/controllers/TacticController.js` und `assets/map/js/views/TacticLayer.js`.
|
||||||
|
Die Tabellen `tactics`, `tactic_steps` und `tactic_shapes` stehen im Abschnitt „Taktiken auf
|
||||||
|
der Karte“ in `sql/schema.sql`. Fehlen sie, zeigt die Kartenseite oben einen Hinweis mit dem
|
||||||
|
Knopf **Tabellen jetzt anlegen** (nur mit `roles.manage`). Die Berechtigung `map.tactics`
|
||||||
|
muss bei bestehenden Installationen unter Berechtigungsgruppen angelegt und zugewiesen werden,
|
||||||
|
falls es sie noch nicht gibt.
|
||||||
|
|
||||||
|
Die frühere Taktikkarte (eigene Marker, Kartenbauer) wurde durch diese Karte ersetzt. Ihre
|
||||||
|
Tabellen `map_categories`, `map_markers`, `map_tactics` und `map_tactic_shapes` sowie die
|
||||||
|
Berechtigung `map.manage` werden nicht mehr benutzt und können bei bestehenden Installationen
|
||||||
|
gelöscht werden. Die alten Taktiken werden nicht übernommen: Sie lagen als Anteil eines
|
||||||
|
selbst erstellten Kartenbildes vor, das mit den neuen Kacheln nicht übereinstimmt.
|
||||||
|
|
||||||
### Mitgliederliste
|
### Mitgliederliste
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1913,3 +1913,15 @@ input.role-weight-input {
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
background: #1a1a2e;
|
background: #1a1a2e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.map-setup {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
padding: 0.9rem 1.25rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-setup p { margin: 0; }
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,7 @@
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const controller = new MapController();
|
const controller = new MapController();
|
||||||
|
// Vor init(), damit schon die erste Karte ihre Taktiken laedt.
|
||||||
|
new TacticController(controller);
|
||||||
controller.init();
|
controller.init();
|
||||||
});
|
});
|
||||||
|
|
@ -39,9 +39,78 @@
|
||||||
<!-- Category filter panel -->
|
<!-- Category filter panel -->
|
||||||
<aside id="category-panel" class="panel">
|
<aside id="category-panel" class="panel">
|
||||||
<h3>Categories</h3>
|
<h3>Categories</h3>
|
||||||
|
<div class="category-bulk">
|
||||||
|
<button type="button" id="btn-show-all">Alle anzeigen</button>
|
||||||
|
<button type="button" id="btn-hide-all">Alle verstecken</button>
|
||||||
|
</div>
|
||||||
<div id="categories-list"></div>
|
<div id="categories-list"></div>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
|
<!-- Taktiken: Liste, Schritte der gewaehlten Taktik -->
|
||||||
|
<aside id="tactic-panel" class="panel hidden">
|
||||||
|
<div class="tp-head">
|
||||||
|
<button type="button" class="tp-collapse" id="tp-collapse" title="Ein-/Ausklappen" aria-expanded="true">▾</button>
|
||||||
|
<h3>Taktiken</h3>
|
||||||
|
<button type="button" class="tp-btn hidden" id="tp-new">+ Neu</button>
|
||||||
|
</div>
|
||||||
|
<!-- Zeichenwerkzeuge (nur mit Berechtigung map.tactics) -->
|
||||||
|
<div id="tactic-toolbar" class="hidden">
|
||||||
|
<button type="button" class="tool active" data-tool="pan" title="Karte verschieben (Esc)">✋</button>
|
||||||
|
<button type="button" class="tool" data-tool="pen" title="Stift: frei zeichnen">✏️</button>
|
||||||
|
<button type="button" class="tool" data-tool="arrow" title="Pfeil: ziehen">➜</button>
|
||||||
|
<button type="button" class="tool" data-tool="line" title="Linie: ziehen">╱</button>
|
||||||
|
<button type="button" class="tool" data-tool="area" title="Fläche: Eckpunkte klicken, Doppelklick oder Enter schließt ab">⬠</button>
|
||||||
|
<button type="button" class="tool" data-tool="circle" title="Kreis: von der Mitte aus ziehen">◯</button>
|
||||||
|
<button type="button" class="tool" data-tool="point" title="Punkt mit Beschriftung setzen">📍</button>
|
||||||
|
<button type="button" class="tool" data-tool="text" title="Text setzen">T</button>
|
||||||
|
<button type="button" class="tool" data-tool="eraser" title="Radierer: Zeichnung anklicken">⌫</button>
|
||||||
|
<span class="tool-sep"></span>
|
||||||
|
<input type="color" id="tool-color" value="#ffd700" title="Farbe">
|
||||||
|
<select id="tool-width" title="Strichstärke">
|
||||||
|
<option value="2">dünn</option>
|
||||||
|
<option value="4" selected>mittel</option>
|
||||||
|
<option value="7">dick</option>
|
||||||
|
<option value="12">sehr dick</option>
|
||||||
|
</select>
|
||||||
|
<span class="tool-sep"></span>
|
||||||
|
<button type="button" class="tool" id="tool-undo" title="Letzte eigene Zeichnung in diesem Schritt entfernen (Strg+Z)">↶</button>
|
||||||
|
</div>
|
||||||
|
<div class="tp-body" id="tp-body">
|
||||||
|
<div id="tp-list"></div>
|
||||||
|
<p class="tp-message hidden" id="tp-message"></p>
|
||||||
|
<div class="tp-detail hidden" id="tp-detail">
|
||||||
|
<div class="tp-detail-head">
|
||||||
|
<strong id="tp-name"></strong>
|
||||||
|
<span class="tp-edit-only">
|
||||||
|
<button type="button" class="tp-icon" id="tp-edit" title="Taktik umbenennen/beschreiben">✎</button>
|
||||||
|
<button type="button" class="tp-icon tp-danger" id="tp-delete" title="Taktik löschen">🗑</button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<p class="tp-desc" id="tp-desc"></p>
|
||||||
|
<h4>Schritte</h4>
|
||||||
|
<ol class="tp-steps" id="tp-steps"></ol>
|
||||||
|
<div class="tp-step-actions tp-edit-only">
|
||||||
|
<button type="button" class="tp-btn" id="tp-step-add" title="Leeren Schritt am Ende anlegen">+ Schritt</button>
|
||||||
|
<button type="button" class="tp-btn" id="tp-step-copy" title="Neuen Schritt mit den Zeichnungen des aktuellen Schritts anlegen">+ Kopie dieses Schritts</button>
|
||||||
|
</div>
|
||||||
|
<label class="tp-check">
|
||||||
|
<input type="checkbox" id="tp-ghosts">
|
||||||
|
Vorherige Schritte blass zeigen
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<!-- Schritt-Leiste der gewaehlten Taktik -->
|
||||||
|
<div id="step-bar" class="panel hidden">
|
||||||
|
<button type="button" id="sb-prev" title="Vorheriger Schritt (←)">◀</button>
|
||||||
|
<div class="sb-text">
|
||||||
|
<div class="sb-title" id="sb-title"></div>
|
||||||
|
<div class="sb-desc" id="sb-desc"></div>
|
||||||
|
</div>
|
||||||
|
<button type="button" id="sb-next" title="Nächster Schritt (→)">▶</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Zoom level indicator -->
|
<!-- Zoom level indicator -->
|
||||||
<footer id="zoom-indicator" class="panel">
|
<footer id="zoom-indicator" class="panel">
|
||||||
Zoom: <span id="zoom-level">2</span>
|
Zoom: <span id="zoom-level">2</span>
|
||||||
|
|
@ -53,6 +122,20 @@
|
||||||
<div class="name"></div>
|
<div class="name"></div>
|
||||||
<div class="category"></div>
|
<div class="category"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Eingabedialog fuer Namen, Beschreibungen und Beschriftungen -->
|
||||||
|
<div id="tactic-dialog" class="hidden">
|
||||||
|
<form class="panel" id="td-form">
|
||||||
|
<h3 id="td-title"></h3>
|
||||||
|
<div id="td-fields"></div>
|
||||||
|
<div class="td-actions">
|
||||||
|
<button type="submit" id="td-submit">Speichern</button>
|
||||||
|
<button type="button" id="td-cancel">Abbrechen</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="toast" class="hidden"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Application scripts -->
|
<!-- Application scripts -->
|
||||||
|
|
@ -62,6 +145,9 @@
|
||||||
<script src="js/services/MarkerService.js"></script>
|
<script src="js/services/MarkerService.js"></script>
|
||||||
<script src="js/views/MapView.js"></script>
|
<script src="js/views/MapView.js"></script>
|
||||||
<script src="js/controllers/MapController.js"></script>
|
<script src="js/controllers/MapController.js"></script>
|
||||||
|
<script src="js/services/TacticService.js"></script>
|
||||||
|
<script src="js/views/TacticLayer.js"></script>
|
||||||
|
<script src="js/controllers/TacticController.js"></script>
|
||||||
<script src="app.js"></script>
|
<script src="app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
@ -139,6 +139,7 @@ class MapController {
|
||||||
this.view.resetView();
|
this.view.resetView();
|
||||||
this.buildCategoryUI();
|
this.buildCategoryUI();
|
||||||
this.render();
|
this.render();
|
||||||
|
if (this.onMapChanged) this.onMapChanged(slug);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to load map:', e);
|
console.error('Failed to load map:', e);
|
||||||
|
|
@ -271,6 +272,20 @@ class MapController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alle Kategorien ein- oder ausblenden und die Haekchen nachziehen
|
||||||
|
*/
|
||||||
|
setAllCategories(visible) {
|
||||||
|
Object.keys(this.mapData.categories).forEach(key => {
|
||||||
|
this.mapData.toggleCategory(key, visible);
|
||||||
|
});
|
||||||
|
this.categoriesList.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
|
||||||
|
checkbox.checked = visible;
|
||||||
|
checkbox.indeterminate = false;
|
||||||
|
});
|
||||||
|
this.render();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the map
|
* Render the map
|
||||||
*/
|
*/
|
||||||
|
|
@ -293,6 +308,10 @@ class MapController {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Alle Kategorien auf einmal ein- bzw. ausblenden
|
||||||
|
document.getElementById('btn-show-all').addEventListener('click', () => this.setAllCategories(true));
|
||||||
|
document.getElementById('btn-hide-all').addEventListener('click', () => this.setAllCategories(false));
|
||||||
|
|
||||||
// Zoom controls
|
// Zoom controls
|
||||||
document.getElementById('btn-zoom-in').addEventListener('click', () => {
|
document.getElementById('btn-zoom-in').addEventListener('click', () => {
|
||||||
if (this.view.zoomIn()) this.render();
|
if (this.view.zoomIn()) this.render();
|
||||||
|
|
|
||||||
790
assets/map/js/controllers/TacticController.js
Normal file
790
assets/map/js/controllers/TacticController.js
Normal file
|
|
@ -0,0 +1,790 @@
|
||||||
|
/**
|
||||||
|
* TacticController - Taktiken auf der Karte: anlegen, in Schritte gliedern, zeichnen.
|
||||||
|
*
|
||||||
|
* Eine Taktik gehoert zu einer Region und besteht aus Schritten (Schritt 1, Schritt 2 ...),
|
||||||
|
* jeder Schritt ist eine eigene Zeichenebene. Ansehen darf jeder mit map.view, anlegen und
|
||||||
|
* zeichnen nur, wer map.tactics hat - das entscheidet der Server (can_edit).
|
||||||
|
*/
|
||||||
|
|
||||||
|
class TacticController {
|
||||||
|
constructor(mapController) {
|
||||||
|
this.map = mapController;
|
||||||
|
this.view = mapController.view;
|
||||||
|
this.service = new TacticService();
|
||||||
|
this.layer = new TacticLayer(this.view);
|
||||||
|
|
||||||
|
this.slug = null;
|
||||||
|
this.tactics = [];
|
||||||
|
this.canEdit = false;
|
||||||
|
this.selectedId = null;
|
||||||
|
this.stepIndex = 0;
|
||||||
|
this.showGhosts = false;
|
||||||
|
|
||||||
|
this.tool = 'pan';
|
||||||
|
this.draft = null;
|
||||||
|
this.drawing = false;
|
||||||
|
this.undoStacks = {}; // step_id -> IDs der in dieser Sitzung gezeichneten Formen
|
||||||
|
this.loadToken = 0;
|
||||||
|
this.dialogOpen = false;
|
||||||
|
this.dialogCancel = null;
|
||||||
|
|
||||||
|
const $ = id => document.getElementById(id);
|
||||||
|
this.el = {
|
||||||
|
panel: $('tactic-panel'),
|
||||||
|
body: $('tp-body'),
|
||||||
|
collapse: $('tp-collapse'),
|
||||||
|
newBtn: $('tp-new'),
|
||||||
|
list: $('tp-list'),
|
||||||
|
message: $('tp-message'),
|
||||||
|
detail: $('tp-detail'),
|
||||||
|
name: $('tp-name'),
|
||||||
|
desc: $('tp-desc'),
|
||||||
|
edit: $('tp-edit'),
|
||||||
|
del: $('tp-delete'),
|
||||||
|
steps: $('tp-steps'),
|
||||||
|
stepAdd: $('tp-step-add'),
|
||||||
|
stepCopy: $('tp-step-copy'),
|
||||||
|
ghosts: $('tp-ghosts'),
|
||||||
|
toolbar: $('tactic-toolbar'),
|
||||||
|
color: $('tool-color'),
|
||||||
|
width: $('tool-width'),
|
||||||
|
undo: $('tool-undo'),
|
||||||
|
stepBar: $('step-bar'),
|
||||||
|
prev: $('sb-prev'),
|
||||||
|
next: $('sb-next'),
|
||||||
|
sbTitle: $('sb-title'),
|
||||||
|
sbDesc: $('sb-desc'),
|
||||||
|
dialog: $('tactic-dialog'),
|
||||||
|
form: $('td-form'),
|
||||||
|
dTitle: $('td-title'),
|
||||||
|
dFields: $('td-fields'),
|
||||||
|
dSubmit: $('td-submit'),
|
||||||
|
dCancel: $('td-cancel'),
|
||||||
|
toast: $('toast')
|
||||||
|
};
|
||||||
|
|
||||||
|
this.view.onViewChange = () => this.redraw();
|
||||||
|
this.map.onMapChanged = slug => this.loadMap(slug);
|
||||||
|
window.addEventListener('resize', () => {
|
||||||
|
this.layer.resize();
|
||||||
|
this.redraw();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.bindPanel();
|
||||||
|
this.bindToolbar();
|
||||||
|
this.bindCanvas();
|
||||||
|
this.bindKeyboard();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Daten
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
currentTactic() {
|
||||||
|
return this.tactics.find(t => t.id === this.selectedId) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentStep() {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
return tactic ? tactic.steps[this.stepIndex] || null : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadMap(slug) {
|
||||||
|
this.slug = slug;
|
||||||
|
this.selectedId = null;
|
||||||
|
this.stepIndex = 0;
|
||||||
|
this.setTool('pan');
|
||||||
|
await this.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Laedt die Taktiken der aktuellen Region neu. focusStepId: Schritt, der danach
|
||||||
|
* ausgewaehlt sein soll (z.B. ein gerade angelegter oder verschobener).
|
||||||
|
*/
|
||||||
|
async reload(focusStepId = null) {
|
||||||
|
const token = ++this.loadToken;
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await this.service.list(this.slug);
|
||||||
|
if (token !== this.loadToken) return;
|
||||||
|
this.tactics = data.tactics || [];
|
||||||
|
this.canEdit = !!data.can_edit;
|
||||||
|
} catch (e) {
|
||||||
|
if (token !== this.loadToken) return;
|
||||||
|
this.tactics = [];
|
||||||
|
this.canEdit = false;
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ausserhalb der Website (kein tactics_api.php) gibt es keine Taktiken.
|
||||||
|
this.el.panel.classList.toggle('hidden', !!(error && error.unavailable));
|
||||||
|
this.showMessage(error && !error.unavailable ? error.message : '');
|
||||||
|
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
if (!tactic) {
|
||||||
|
this.selectedId = null;
|
||||||
|
this.stepIndex = 0;
|
||||||
|
} else {
|
||||||
|
const focused = focusStepId ? tactic.steps.findIndex(s => s.id === focusStepId) : -1;
|
||||||
|
if (focused >= 0) this.stepIndex = focused;
|
||||||
|
this.stepIndex = Math.max(0, Math.min(this.stepIndex, tactic.steps.length - 1));
|
||||||
|
}
|
||||||
|
if (!this.canEdit || !tactic) this.setTool('pan');
|
||||||
|
|
||||||
|
this.renderUI();
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(action, payload) {
|
||||||
|
try {
|
||||||
|
return await this.service.send(action, payload);
|
||||||
|
} catch (e) {
|
||||||
|
this.toast(e.message, true);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Oberflaeche
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
static esc(text) {
|
||||||
|
return String(text).replace(/[&<>"']/g, c => ({
|
||||||
|
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
||||||
|
}[c]));
|
||||||
|
}
|
||||||
|
|
||||||
|
static stepLabel(step, index) {
|
||||||
|
return step.title ? step.title : `Schritt ${index + 1}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
showMessage(text) {
|
||||||
|
this.el.message.textContent = text;
|
||||||
|
this.el.message.classList.toggle('hidden', !text);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderUI() {
|
||||||
|
const esc = TacticController.esc;
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
|
||||||
|
this.el.panel.classList.toggle('can-edit', this.canEdit);
|
||||||
|
this.el.newBtn.classList.toggle('hidden', !this.canEdit);
|
||||||
|
|
||||||
|
// Liste der Taktiken
|
||||||
|
if (this.tactics.length === 0) {
|
||||||
|
this.el.list.innerHTML = this.el.message.classList.contains('hidden')
|
||||||
|
? `<p class="tp-empty">Noch keine Taktiken für diese Karte.${this.canEdit ? ' Mit „+ Neu“ legst du die erste an.' : ''}</p>`
|
||||||
|
: '';
|
||||||
|
} else {
|
||||||
|
this.el.list.innerHTML = this.tactics.map(t => `
|
||||||
|
<button type="button" class="tp-item${t.id === this.selectedId ? ' active' : ''}" data-id="${t.id}">
|
||||||
|
<span class="tp-item-name">${esc(t.name)}</span>
|
||||||
|
<span class="tp-item-meta">${t.steps.length} ${t.steps.length === 1 ? 'Schritt' : 'Schritte'}</span>
|
||||||
|
</button>`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Details der gewaehlten Taktik
|
||||||
|
this.el.detail.classList.toggle('hidden', !tactic);
|
||||||
|
if (tactic) {
|
||||||
|
this.el.name.textContent = tactic.name;
|
||||||
|
this.el.desc.textContent = tactic.description;
|
||||||
|
this.el.desc.classList.toggle('hidden', !tactic.description);
|
||||||
|
this.el.ghosts.checked = this.showGhosts;
|
||||||
|
this.renderSteps(tactic);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.el.toolbar.classList.toggle('hidden', !(this.canEdit && tactic));
|
||||||
|
this.renderStepBar();
|
||||||
|
}
|
||||||
|
|
||||||
|
renderSteps(tactic) {
|
||||||
|
const esc = TacticController.esc;
|
||||||
|
const last = tactic.steps.length - 1;
|
||||||
|
|
||||||
|
this.el.steps.innerHTML = tactic.steps.map((step, i) => `
|
||||||
|
<li class="tp-step${i === this.stepIndex ? ' active' : ''}" data-index="${i}">
|
||||||
|
<span class="tp-step-no">${i + 1}</span>
|
||||||
|
<span class="tp-step-name">${esc(TacticController.stepLabel(step, i))}</span>
|
||||||
|
<span class="tp-step-count" title="Zeichnungen">${step.shapes.length}</span>
|
||||||
|
${this.canEdit ? `
|
||||||
|
<span class="tp-step-tools">
|
||||||
|
<button type="button" class="tp-icon" data-step-action="up" title="Nach oben"${i === 0 ? ' disabled' : ''}>↑</button>
|
||||||
|
<button type="button" class="tp-icon" data-step-action="down" title="Nach unten"${i === last ? ' disabled' : ''}>↓</button>
|
||||||
|
<button type="button" class="tp-icon" data-step-action="edit" title="Umbenennen/beschreiben">✎</button>
|
||||||
|
<button type="button" class="tp-icon tp-danger" data-step-action="delete" title="Schritt löschen"${last === 0 ? ' disabled' : ''}>🗑</button>
|
||||||
|
</span>` : ''}
|
||||||
|
</li>`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
renderStepBar() {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
const step = this.currentStep();
|
||||||
|
this.el.stepBar.classList.toggle('hidden', !step);
|
||||||
|
if (!step) return;
|
||||||
|
|
||||||
|
const total = tactic.steps.length;
|
||||||
|
// Der Name der Taktik steht schon im Panel - hier nur, wo man gerade ist.
|
||||||
|
this.el.sbTitle.textContent = `Schritt ${this.stepIndex + 1}/${total}`
|
||||||
|
+ (step.title ? ` – ${step.title}` : '');
|
||||||
|
this.el.stepBar.title = tactic.name;
|
||||||
|
this.el.sbDesc.textContent = step.description;
|
||||||
|
this.el.sbDesc.classList.toggle('hidden', !step.description);
|
||||||
|
this.el.prev.disabled = this.stepIndex === 0;
|
||||||
|
this.el.next.disabled = this.stepIndex >= total - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
redraw() {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
const step = this.currentStep();
|
||||||
|
|
||||||
|
this.layer.shapes = step ? step.shapes : [];
|
||||||
|
this.layer.ghostShapes = tactic && this.showGhosts
|
||||||
|
? tactic.steps.slice(0, this.stepIndex).flatMap(s => s.shapes)
|
||||||
|
: [];
|
||||||
|
this.layer.draft = this.draft;
|
||||||
|
this.layer.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
toast(text, isError = false) {
|
||||||
|
const el = this.el.toast;
|
||||||
|
el.textContent = text;
|
||||||
|
el.classList.toggle('error', isError);
|
||||||
|
el.classList.remove('hidden');
|
||||||
|
clearTimeout(this.toastTimer);
|
||||||
|
this.toastTimer = setTimeout(() => el.classList.add('hidden'), isError ? 6000 : 3500);
|
||||||
|
}
|
||||||
|
|
||||||
|
select(id) {
|
||||||
|
this.cancelDraft();
|
||||||
|
this.selectedId = this.selectedId === id ? null : id;
|
||||||
|
this.stepIndex = 0;
|
||||||
|
if (!this.selectedId) this.setTool('pan');
|
||||||
|
this.renderUI();
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
goToStep(index) {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
if (!tactic) return;
|
||||||
|
const target = Math.max(0, Math.min(index, tactic.steps.length - 1));
|
||||||
|
if (target === this.stepIndex) return;
|
||||||
|
this.cancelDraft();
|
||||||
|
this.stepIndex = target;
|
||||||
|
this.renderUI();
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Taktiken und Schritte bearbeiten
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
async newTactic() {
|
||||||
|
const values = await this.dialog('Neue Taktik', [
|
||||||
|
{ name: 'name', label: 'Name', required: true, max: 120, placeholder: 'z. B. Festung Süd – Angriff' },
|
||||||
|
{ name: 'description', label: 'Beschreibung (optional)', type: 'textarea', max: 5000,
|
||||||
|
placeholder: 'Ziel, Aufstellung, Rollen …' }
|
||||||
|
], 'Anlegen');
|
||||||
|
if (!values) return;
|
||||||
|
|
||||||
|
const result = await this.run('tactic_create', { map: this.slug, ...values });
|
||||||
|
if (!result) return;
|
||||||
|
this.selectedId = result.tactic_id;
|
||||||
|
this.stepIndex = 0;
|
||||||
|
await this.reload();
|
||||||
|
if (this.canEdit) this.setTool('pen');
|
||||||
|
this.toast('Taktik angelegt – zeichne jetzt Schritt 1 ein.');
|
||||||
|
}
|
||||||
|
|
||||||
|
async editTactic() {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
if (!tactic) return;
|
||||||
|
const values = await this.dialog('Taktik bearbeiten', [
|
||||||
|
{ name: 'name', label: 'Name', required: true, max: 120, value: tactic.name },
|
||||||
|
{ name: 'description', label: 'Beschreibung (optional)', type: 'textarea', max: 5000, value: tactic.description }
|
||||||
|
]);
|
||||||
|
if (!values) return;
|
||||||
|
if (await this.run('tactic_update', { tactic_id: tactic.id, ...values })) {
|
||||||
|
await this.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteTactic() {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
if (!tactic || !confirm(`Taktik „${tactic.name}“ mit allen Schritten und Zeichnungen löschen?`)) return;
|
||||||
|
if (await this.run('tactic_delete', { tactic_id: tactic.id })) {
|
||||||
|
this.selectedId = null;
|
||||||
|
await this.reload();
|
||||||
|
this.toast('Taktik gelöscht.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async addStep(copyCurrent) {
|
||||||
|
const tactic = this.currentTactic();
|
||||||
|
const current = this.currentStep();
|
||||||
|
if (!tactic) return;
|
||||||
|
|
||||||
|
const number = tactic.steps.length + 1;
|
||||||
|
const values = await this.dialog(copyCurrent ? `Schritt ${number} als Kopie` : `Schritt ${number} anlegen`, [
|
||||||
|
{ name: 'title', label: 'Titel (optional)', max: 120, placeholder: `z. B. Vorrücken zum Tor` },
|
||||||
|
{ name: 'description', label: 'Beschreibung (optional)', type: 'textarea', max: 5000,
|
||||||
|
placeholder: 'Was passiert in diesem Schritt?' }
|
||||||
|
], 'Anlegen');
|
||||||
|
if (!values) return;
|
||||||
|
|
||||||
|
const payload = { tactic_id: tactic.id, ...values };
|
||||||
|
if (copyCurrent && current) payload.copy_from = current.id;
|
||||||
|
const result = await this.run('step_create', payload);
|
||||||
|
if (result) {
|
||||||
|
await this.reload(result.step_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async editStep(step, index) {
|
||||||
|
const values = await this.dialog(`Schritt ${index + 1} bearbeiten`, [
|
||||||
|
{ name: 'title', label: 'Titel (optional)', max: 120, value: step.title },
|
||||||
|
{ name: 'description', label: 'Beschreibung (optional)', type: 'textarea', max: 5000, value: step.description }
|
||||||
|
]);
|
||||||
|
if (!values) return;
|
||||||
|
if (await this.run('step_update', { step_id: step.id, ...values })) {
|
||||||
|
await this.reload(step.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async moveStep(step, direction) {
|
||||||
|
if (await this.run('step_move', { step_id: step.id, direction })) {
|
||||||
|
await this.reload(step.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteStep(step, index) {
|
||||||
|
const label = TacticController.stepLabel(step, index);
|
||||||
|
if (!confirm(`Schritt ${index + 1} („${label}“) mit ${step.shapes.length} Zeichnung(en) löschen?`)) return;
|
||||||
|
if (await this.run('step_delete', { step_id: step.id })) {
|
||||||
|
await this.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Zeichnen
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
setTool(tool) {
|
||||||
|
this.cancelDraft();
|
||||||
|
this.tool = tool;
|
||||||
|
this.layer.hoverId = null;
|
||||||
|
this.layer.canvas.classList.toggle('drawing', tool !== 'pan');
|
||||||
|
this.layer.canvas.classList.toggle('erasing', tool === 'eraser');
|
||||||
|
this.el.toolbar.querySelectorAll('.tool[data-tool]').forEach(btn => {
|
||||||
|
btn.classList.toggle('active', btn.dataset.tool === tool);
|
||||||
|
});
|
||||||
|
if (tool === 'area') {
|
||||||
|
this.toast('Eckpunkte anklicken – Doppelklick oder Enter schließt die Fläche ab.');
|
||||||
|
}
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelDraft() {
|
||||||
|
this.draft = null;
|
||||||
|
this.drawing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
worldAt(event) {
|
||||||
|
const rect = this.layer.canvas.getBoundingClientRect();
|
||||||
|
const world = this.view.screenToWorld(event.clientX - rect.left, event.clientY - rect.top);
|
||||||
|
return [world.x, world.y];
|
||||||
|
}
|
||||||
|
|
||||||
|
screenAt(event) {
|
||||||
|
const rect = this.layer.canvas.getBoundingClientRect();
|
||||||
|
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
||||||
|
}
|
||||||
|
|
||||||
|
screenDistance(a, b) {
|
||||||
|
const pa = this.layer.toScreen(a);
|
||||||
|
const pb = this.layer.toScreen(b);
|
||||||
|
return Math.hypot(pa.x - pb.x, pa.y - pb.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
newDraft(type, points) {
|
||||||
|
return {
|
||||||
|
type,
|
||||||
|
points,
|
||||||
|
color: this.el.color.value,
|
||||||
|
width: parseInt(this.el.width.value, 10) || 4,
|
||||||
|
label: ''
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
onPointerDown(event) {
|
||||||
|
// Rechte/mittlere Maustaste verschiebt die Karte auch mitten im Zeichnen.
|
||||||
|
if (event.button !== 0) {
|
||||||
|
this.view.canvas.dispatchEvent(new MouseEvent('mousedown', {
|
||||||
|
clientX: event.clientX, clientY: event.clientY, bubbles: true
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.currentStep()) return;
|
||||||
|
|
||||||
|
const point = this.worldAt(event);
|
||||||
|
switch (this.tool) {
|
||||||
|
case 'pen':
|
||||||
|
this.draft = this.newDraft('pen', [point]);
|
||||||
|
this.drawing = true;
|
||||||
|
this.layer.canvas.setPointerCapture(event.pointerId);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'line':
|
||||||
|
case 'arrow':
|
||||||
|
case 'circle':
|
||||||
|
this.draft = this.newDraft(this.tool, [point, point]);
|
||||||
|
this.drawing = true;
|
||||||
|
this.layer.canvas.setPointerCapture(event.pointerId);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'area':
|
||||||
|
// Der letzte Punkt folgt immer der Maus, ein Klick setzt ihn fest.
|
||||||
|
if (!this.draft) {
|
||||||
|
this.draft = this.newDraft('area', [point, point]);
|
||||||
|
} else {
|
||||||
|
this.draft.points[this.draft.points.length - 1] = point;
|
||||||
|
this.draft.points.push(point);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'point':
|
||||||
|
case 'text':
|
||||||
|
this.placeLabeled(this.tool, point);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'eraser': {
|
||||||
|
const screen = this.screenAt(event);
|
||||||
|
const hit = this.layer.hitTest(screen.x, screen.y);
|
||||||
|
if (hit) this.deleteShape(hit);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
onPointerMove(event) {
|
||||||
|
if (this.tool === 'eraser') {
|
||||||
|
const screen = this.screenAt(event);
|
||||||
|
const hit = this.layer.hitTest(screen.x, screen.y);
|
||||||
|
const hoverId = hit ? hit.id : null;
|
||||||
|
if (hoverId !== this.layer.hoverId) {
|
||||||
|
this.layer.hoverId = hoverId;
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.draft) return;
|
||||||
|
|
||||||
|
const point = this.worldAt(event);
|
||||||
|
const points = this.draft.points;
|
||||||
|
if (this.draft.type === 'pen') {
|
||||||
|
if (!this.drawing || this.screenDistance(points[points.length - 1], point) < 3) return;
|
||||||
|
points.push(point);
|
||||||
|
} else if (this.draft.type === 'area') {
|
||||||
|
points[points.length - 1] = point;
|
||||||
|
} else if (this.drawing) {
|
||||||
|
points[1] = point;
|
||||||
|
}
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
onPointerUp() {
|
||||||
|
if (!this.drawing || !this.draft) return;
|
||||||
|
this.drawing = false;
|
||||||
|
const draft = this.draft;
|
||||||
|
|
||||||
|
if (draft.type === 'pen') {
|
||||||
|
if (draft.points.length < 2) {
|
||||||
|
this.cancelDraft();
|
||||||
|
} else {
|
||||||
|
// Toleranz: gut ein Bildschirmpixel bei der aktuellen Zoomstufe.
|
||||||
|
draft.points = TacticLayer.simplify(draft.points, 1.2 / this.layer.scale());
|
||||||
|
this.saveShape(draft);
|
||||||
|
}
|
||||||
|
} else if (this.screenDistance(draft.points[0], draft.points[1]) < 6) {
|
||||||
|
// Nur geklickt, nicht gezogen - nichts anlegen.
|
||||||
|
this.cancelDraft();
|
||||||
|
} else {
|
||||||
|
this.saveShape(draft);
|
||||||
|
}
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
finishArea() {
|
||||||
|
if (!this.draft || this.draft.type !== 'area') return;
|
||||||
|
|
||||||
|
// Der mitlaufende Punkt faellt weg, ebenso doppelte Punkte vom Doppelklick.
|
||||||
|
const points = [];
|
||||||
|
this.draft.points.slice(0, -1).forEach(point => {
|
||||||
|
if (points.length === 0 || this.screenDistance(points[points.length - 1], point) >= 4) {
|
||||||
|
points.push(point);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (points.length < 3) {
|
||||||
|
this.toast('Eine Fläche braucht mindestens drei Eckpunkte.', true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.draft.points = points;
|
||||||
|
this.saveShape(this.draft);
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
async placeLabeled(type, point) {
|
||||||
|
const isText = type === 'text';
|
||||||
|
const values = await this.dialog(isText ? 'Text setzen' : 'Punkt setzen', [
|
||||||
|
{ name: 'label', label: isText ? 'Text' : 'Beschriftung (optional)', required: isText, max: 200,
|
||||||
|
placeholder: isText ? 'z. B. Heiler hier' : 'z. B. Sammelpunkt' }
|
||||||
|
], 'Setzen');
|
||||||
|
if (!values) return;
|
||||||
|
const shape = this.newDraft(type, [point]);
|
||||||
|
shape.label = values.label;
|
||||||
|
this.saveShape(shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveShape(shape) {
|
||||||
|
const step = this.currentStep();
|
||||||
|
if (!step) return;
|
||||||
|
|
||||||
|
// Die Form bleibt als Entwurf sichtbar, bis der Server sie bestaetigt hat.
|
||||||
|
const result = await this.run('shape_create', {
|
||||||
|
step_id: step.id,
|
||||||
|
type: shape.type,
|
||||||
|
color: shape.color,
|
||||||
|
width: shape.width,
|
||||||
|
label: shape.label,
|
||||||
|
points: shape.points.map(p => [Math.round(p[0] * 100) / 100, Math.round(p[1] * 100) / 100])
|
||||||
|
});
|
||||||
|
if (this.draft === shape) this.draft = null;
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
step.shapes.push(result.shape);
|
||||||
|
(this.undoStacks[step.id] = this.undoStacks[step.id] || []).push(result.shape.id);
|
||||||
|
this.renderUI();
|
||||||
|
}
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteShape(shape) {
|
||||||
|
const step = this.currentStep();
|
||||||
|
if (!step) return;
|
||||||
|
if (await this.run('shape_delete', { shape_id: shape.id })) {
|
||||||
|
step.shapes = step.shapes.filter(s => s.id !== shape.id);
|
||||||
|
this.layer.hoverId = null;
|
||||||
|
this.renderUI();
|
||||||
|
this.redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
undoLast() {
|
||||||
|
const step = this.currentStep();
|
||||||
|
if (!step) return;
|
||||||
|
if (this.draft) {
|
||||||
|
this.cancelDraft();
|
||||||
|
this.redraw();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stack = this.undoStacks[step.id] || [];
|
||||||
|
while (stack.length) {
|
||||||
|
const shapeId = stack.pop();
|
||||||
|
const shape = step.shapes.find(s => s.id === shapeId);
|
||||||
|
if (shape) {
|
||||||
|
this.deleteShape(shape);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.toast('In diesem Schritt gibt es nichts rückgängig zu machen.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Eingabedialog
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeigt einen kleinen Dialog mit Eingabefeldern. Liefert die (getrimmten) Werte oder
|
||||||
|
* null bei Abbruch.
|
||||||
|
*/
|
||||||
|
dialog(title, fields, submitLabel = 'Speichern') {
|
||||||
|
if (this.dialogCancel) this.dialogCancel();
|
||||||
|
|
||||||
|
return new Promise(resolve => {
|
||||||
|
const el = this.el;
|
||||||
|
el.dTitle.textContent = title;
|
||||||
|
el.dSubmit.textContent = submitLabel;
|
||||||
|
el.dFields.innerHTML = '';
|
||||||
|
|
||||||
|
fields.forEach(field => {
|
||||||
|
const label = document.createElement('label');
|
||||||
|
label.className = 'td-field';
|
||||||
|
label.textContent = field.label;
|
||||||
|
|
||||||
|
const input = document.createElement(field.type === 'textarea' ? 'textarea' : 'input');
|
||||||
|
if (field.type === 'textarea') {
|
||||||
|
input.rows = 4;
|
||||||
|
} else {
|
||||||
|
input.type = 'text';
|
||||||
|
}
|
||||||
|
input.name = field.name;
|
||||||
|
input.value = field.value || '';
|
||||||
|
if (field.placeholder) input.placeholder = field.placeholder;
|
||||||
|
if (field.max) input.maxLength = field.max;
|
||||||
|
if (field.required) input.required = true;
|
||||||
|
|
||||||
|
label.appendChild(input);
|
||||||
|
el.dFields.appendChild(label);
|
||||||
|
});
|
||||||
|
|
||||||
|
const finish = value => {
|
||||||
|
el.dialog.classList.add('hidden');
|
||||||
|
el.form.onsubmit = null;
|
||||||
|
el.dCancel.onclick = null;
|
||||||
|
this.dialogOpen = false;
|
||||||
|
this.dialogCancel = null;
|
||||||
|
resolve(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
el.form.onsubmit = event => {
|
||||||
|
event.preventDefault();
|
||||||
|
const values = {};
|
||||||
|
fields.forEach(field => {
|
||||||
|
values[field.name] = el.form.elements[field.name].value.trim();
|
||||||
|
});
|
||||||
|
if (fields.some(field => field.required && !values[field.name])) return;
|
||||||
|
finish(values);
|
||||||
|
};
|
||||||
|
el.dCancel.onclick = () => finish(null);
|
||||||
|
this.dialogCancel = () => finish(null);
|
||||||
|
|
||||||
|
this.dialogOpen = true;
|
||||||
|
el.dialog.classList.remove('hidden');
|
||||||
|
const first = el.dFields.querySelector('input, textarea');
|
||||||
|
if (first) first.focus();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Ereignisse
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
bindPanel() {
|
||||||
|
const el = this.el;
|
||||||
|
|
||||||
|
el.collapse.addEventListener('click', () => {
|
||||||
|
const collapsed = el.panel.classList.toggle('collapsed');
|
||||||
|
el.collapse.textContent = collapsed ? '▸' : '▾';
|
||||||
|
el.collapse.setAttribute('aria-expanded', String(!collapsed));
|
||||||
|
});
|
||||||
|
el.newBtn.addEventListener('click', () => this.newTactic());
|
||||||
|
el.edit.addEventListener('click', () => this.editTactic());
|
||||||
|
el.del.addEventListener('click', () => this.deleteTactic());
|
||||||
|
el.stepAdd.addEventListener('click', () => this.addStep(false));
|
||||||
|
el.stepCopy.addEventListener('click', () => this.addStep(true));
|
||||||
|
|
||||||
|
el.list.addEventListener('click', event => {
|
||||||
|
const item = event.target.closest('.tp-item');
|
||||||
|
if (item) this.select(parseInt(item.dataset.id, 10));
|
||||||
|
});
|
||||||
|
|
||||||
|
el.steps.addEventListener('click', event => {
|
||||||
|
const li = event.target.closest('.tp-step');
|
||||||
|
if (!li) return;
|
||||||
|
const index = parseInt(li.dataset.index, 10);
|
||||||
|
const step = this.currentTactic().steps[index];
|
||||||
|
const button = event.target.closest('[data-step-action]');
|
||||||
|
|
||||||
|
if (!button) {
|
||||||
|
this.goToStep(index);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (button.dataset.stepAction) {
|
||||||
|
case 'up': this.moveStep(step, -1); break;
|
||||||
|
case 'down': this.moveStep(step, 1); break;
|
||||||
|
case 'edit': this.editStep(step, index); break;
|
||||||
|
case 'delete': this.deleteStep(step, index); break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
el.ghosts.addEventListener('change', () => {
|
||||||
|
this.showGhosts = el.ghosts.checked;
|
||||||
|
this.redraw();
|
||||||
|
});
|
||||||
|
|
||||||
|
el.prev.addEventListener('click', () => this.goToStep(this.stepIndex - 1));
|
||||||
|
el.next.addEventListener('click', () => this.goToStep(this.stepIndex + 1));
|
||||||
|
|
||||||
|
// Tasten im Dialog nicht an die Tastenkuerzel der Karte (+, -, 0 ...) weiterreichen.
|
||||||
|
el.dialog.addEventListener('keydown', event => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (event.key === 'Escape' && this.dialogCancel) this.dialogCancel();
|
||||||
|
// Enter in einem einzeiligen Feld schickt ab (in der Beschreibung: neue Zeile).
|
||||||
|
if (event.key === 'Enter' && event.target.tagName === 'INPUT') {
|
||||||
|
event.preventDefault();
|
||||||
|
el.form.requestSubmit();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bindToolbar() {
|
||||||
|
this.el.toolbar.addEventListener('click', event => {
|
||||||
|
const button = event.target.closest('.tool[data-tool]');
|
||||||
|
if (button) this.setTool(button.dataset.tool);
|
||||||
|
});
|
||||||
|
this.el.undo.addEventListener('click', () => this.undoLast());
|
||||||
|
}
|
||||||
|
|
||||||
|
bindCanvas() {
|
||||||
|
const canvas = this.layer.canvas;
|
||||||
|
canvas.addEventListener('pointerdown', event => this.onPointerDown(event));
|
||||||
|
canvas.addEventListener('pointermove', event => this.onPointerMove(event));
|
||||||
|
canvas.addEventListener('pointerup', () => this.onPointerUp());
|
||||||
|
canvas.addEventListener('pointercancel', () => {
|
||||||
|
this.cancelDraft();
|
||||||
|
this.redraw();
|
||||||
|
});
|
||||||
|
canvas.addEventListener('dblclick', event => {
|
||||||
|
event.preventDefault();
|
||||||
|
this.finishArea();
|
||||||
|
});
|
||||||
|
canvas.addEventListener('contextmenu', event => event.preventDefault());
|
||||||
|
|
||||||
|
// Zoomen per Mausrad soll auch im Zeichenmodus gehen - weiter an die Karte.
|
||||||
|
canvas.addEventListener('wheel', event => {
|
||||||
|
event.preventDefault();
|
||||||
|
this.view.canvas.dispatchEvent(new WheelEvent('wheel', {
|
||||||
|
deltaY: event.deltaY, deltaX: event.deltaX, cancelable: true
|
||||||
|
}));
|
||||||
|
}, { passive: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
bindKeyboard() {
|
||||||
|
window.addEventListener('keydown', event => {
|
||||||
|
if (this.dialogOpen) return;
|
||||||
|
const tag = event.target && event.target.tagName;
|
||||||
|
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
|
||||||
|
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
if (this.draft) {
|
||||||
|
this.cancelDraft();
|
||||||
|
this.redraw();
|
||||||
|
} else if (this.tool !== 'pan') {
|
||||||
|
this.setTool('pan');
|
||||||
|
}
|
||||||
|
} else if (event.key === 'Enter') {
|
||||||
|
this.finishArea();
|
||||||
|
} else if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'z') {
|
||||||
|
event.preventDefault();
|
||||||
|
if (this.canEdit) this.undoLast();
|
||||||
|
} else if (event.key === 'ArrowLeft' && this.currentTactic()) {
|
||||||
|
this.goToStep(this.stepIndex - 1);
|
||||||
|
} else if (event.key === 'ArrowRight' && this.currentTactic()) {
|
||||||
|
this.goToStep(this.stepIndex + 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
61
assets/map/js/services/TacticService.js
Normal file
61
assets/map/js/services/TacticService.js
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
/**
|
||||||
|
* TacticService - Laden und Speichern der Taktiken ueber die Website (tactics_api.php).
|
||||||
|
*
|
||||||
|
* Der Viewer laeuft im iframe von map.php auf derselben Domain: Anmeldung (Session-Cookie)
|
||||||
|
* und CSRF-Token kommen von der umgebenden Seite. Ausserhalb der Website - etwa als
|
||||||
|
* lokale Datei - gibt es den Endpunkt nicht, dann bleiben die Taktiken ausgeblendet.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class TacticService {
|
||||||
|
constructor() {
|
||||||
|
this.endpoint = '/tactics_api.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
csrfToken() {
|
||||||
|
try {
|
||||||
|
const meta = window.parent.document.querySelector('meta[name="csrf-token"]');
|
||||||
|
return meta ? meta.content : '';
|
||||||
|
} catch (e) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async list(slug) {
|
||||||
|
const response = await fetch(`${this.endpoint}?map=${encodeURIComponent(slug)}`, {
|
||||||
|
credentials: 'same-origin',
|
||||||
|
headers: { 'Accept': 'application/json' }
|
||||||
|
});
|
||||||
|
return this.parse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async send(action, payload = {}) {
|
||||||
|
const response = await fetch(this.endpoint, {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'same-origin',
|
||||||
|
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
||||||
|
body: JSON.stringify({ ...payload, action, csrf_token: this.csrfToken() })
|
||||||
|
});
|
||||||
|
return this.parse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async parse(response) {
|
||||||
|
const text = await response.text();
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = JSON.parse(text);
|
||||||
|
} catch (e) {
|
||||||
|
const error = new Error(text && response.status !== 404
|
||||||
|
? text.replace(/<[^>]*>/g, ' ').trim().slice(0, 200)
|
||||||
|
: 'Taktiken sind hier nicht verfügbar.');
|
||||||
|
error.unavailable = response.status === 404;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.status !== 'success') {
|
||||||
|
const error = new Error(data.message || 'Unbekannter Fehler.');
|
||||||
|
error.data = data;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -94,6 +94,9 @@ class MapView {
|
||||||
ctx.fillStyle = '#0a0a1e';
|
ctx.fillStyle = '#0a0a1e';
|
||||||
ctx.fillRect(0, 0, width, height);
|
ctx.fillRect(0, 0, width, height);
|
||||||
|
|
||||||
|
// Zeichenebene der Taktiken sofort mitziehen, nicht erst nach dem Laden der Kacheln.
|
||||||
|
if (this.onViewChange) this.onViewChange();
|
||||||
|
|
||||||
if (!mapData.currentMap) return;
|
if (!mapData.currentMap) return;
|
||||||
|
|
||||||
const slug = mapData.currentMap.slug;
|
const slug = mapData.currentMap.slug;
|
||||||
|
|
|
||||||
280
assets/map/js/views/TacticLayer.js
Normal file
280
assets/map/js/views/TacticLayer.js
Normal file
|
|
@ -0,0 +1,280 @@
|
||||||
|
/**
|
||||||
|
* TacticLayer - Zeichenebene fuer Taktiken ueber der Karte.
|
||||||
|
*
|
||||||
|
* Ein eigenes Canvas ueber dem Karten-Canvas: die Zeichnungen lassen sich so bei jeder
|
||||||
|
* Mausbewegung neu zeichnen, ohne Kacheln und tausende Marker mitzuzeichnen. Alle Punkte
|
||||||
|
* liegen in Weltkoordinaten (0..4096, wie die Marker) und werden hier ueber die Kamera
|
||||||
|
* von MapView in Bildschirmpixel umgerechnet.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class TacticLayer {
|
||||||
|
constructor(mapView) {
|
||||||
|
this.view = mapView;
|
||||||
|
this.canvas = document.createElement('canvas');
|
||||||
|
this.canvas.id = 'tactic-canvas';
|
||||||
|
mapView.canvas.insertAdjacentElement('afterend', this.canvas);
|
||||||
|
this.ctx = this.canvas.getContext('2d');
|
||||||
|
|
||||||
|
// Was gezeichnet wird - setzt der TacticController.
|
||||||
|
this.shapes = []; // Zeichnungen des aktuellen Schritts
|
||||||
|
this.ghostShapes = []; // Zeichnungen der vorherigen Schritte (blass)
|
||||||
|
this.draft = null; // Form, die gerade gezeichnet wird
|
||||||
|
this.hoverId = null; // Form unter dem Radierer
|
||||||
|
|
||||||
|
this.resize();
|
||||||
|
}
|
||||||
|
|
||||||
|
resize() {
|
||||||
|
this.canvas.width = this.view.canvas.width;
|
||||||
|
this.canvas.height = this.view.canvas.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Bildschirmpixel pro Welteinheit bei der aktuellen Zoomstufe. */
|
||||||
|
scale() {
|
||||||
|
return this.view.worldToScreen(1, 0).x - this.view.worldToScreen(0, 0).x;
|
||||||
|
}
|
||||||
|
|
||||||
|
toScreen(point) {
|
||||||
|
return this.view.worldToScreen(point[0], point[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
draw() {
|
||||||
|
const ctx = this.ctx;
|
||||||
|
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
|
ctx.globalAlpha = 0.3;
|
||||||
|
this.ghostShapes.forEach(shape => this.drawShape(shape));
|
||||||
|
ctx.restore();
|
||||||
|
|
||||||
|
this.shapes.forEach(shape => this.drawShape(shape, shape.id === this.hoverId));
|
||||||
|
if (this.draft) {
|
||||||
|
this.drawShape(this.draft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawShape(shape, highlighted = false) {
|
||||||
|
const ctx = this.ctx;
|
||||||
|
const pts = shape.points.map(p => this.toScreen(p));
|
||||||
|
if (pts.length === 0) return;
|
||||||
|
|
||||||
|
const width = Math.max(1, shape.width || 4);
|
||||||
|
ctx.save();
|
||||||
|
ctx.strokeStyle = shape.color;
|
||||||
|
ctx.fillStyle = shape.color;
|
||||||
|
ctx.lineWidth = width;
|
||||||
|
ctx.lineCap = 'round';
|
||||||
|
ctx.lineJoin = 'round';
|
||||||
|
// Dunkler Schatten, damit helle Farben auch auf heller Karte lesbar bleiben.
|
||||||
|
ctx.shadowColor = 'rgba(0, 0, 0, 0.7)';
|
||||||
|
ctx.shadowBlur = 3;
|
||||||
|
|
||||||
|
if (highlighted) {
|
||||||
|
ctx.shadowColor = '#ffffff';
|
||||||
|
ctx.shadowBlur = 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (shape.type) {
|
||||||
|
case 'pen':
|
||||||
|
case 'line':
|
||||||
|
this.polyline(pts);
|
||||||
|
ctx.stroke();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'arrow':
|
||||||
|
this.drawArrow(pts[0], pts[pts.length - 1], width);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'area':
|
||||||
|
this.polyline(pts);
|
||||||
|
ctx.closePath();
|
||||||
|
ctx.save();
|
||||||
|
ctx.globalAlpha *= 0.25;
|
||||||
|
ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
ctx.stroke();
|
||||||
|
this.drawLabel(shape.label, this.centroid(pts), shape.color, 13);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'circle': {
|
||||||
|
const radius = Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y);
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(pts[0].x, pts[0].y, radius, 0, Math.PI * 2);
|
||||||
|
ctx.save();
|
||||||
|
ctx.globalAlpha *= 0.2;
|
||||||
|
ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
ctx.stroke();
|
||||||
|
this.drawLabel(shape.label, pts[0], shape.color, 13);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'point': {
|
||||||
|
const r = 6 + width / 2;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(pts[0].x, pts[0].y, r, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
ctx.shadowBlur = 0;
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.strokeStyle = '#ffffff';
|
||||||
|
ctx.stroke();
|
||||||
|
if (shape.label) {
|
||||||
|
this.drawLabel(shape.label, { x: pts[0].x + r + 4, y: pts[0].y }, '#ffffff', 13, 'left');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'text':
|
||||||
|
this.drawLabel(shape.label, pts[0], shape.color, 12 + width * 2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
polyline(pts) {
|
||||||
|
const ctx = this.ctx;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(pts[0].x, pts[0].y);
|
||||||
|
for (let i = 1; i < pts.length; i++) {
|
||||||
|
ctx.lineTo(pts[i].x, pts[i].y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawArrow(from, to, width) {
|
||||||
|
const ctx = this.ctx;
|
||||||
|
const angle = Math.atan2(to.y - from.y, to.x - from.x);
|
||||||
|
const head = Math.max(12, width * 3.5);
|
||||||
|
const length = Math.hypot(to.x - from.x, to.y - from.y);
|
||||||
|
|
||||||
|
// Die Linie endet vor der Spitze, sonst schaut sie bei dicken Strichen vorne heraus.
|
||||||
|
const shaft = Math.max(0, length - head * 0.8);
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(from.x, from.y);
|
||||||
|
ctx.lineTo(from.x + Math.cos(angle) * shaft, from.y + Math.sin(angle) * shaft);
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(to.x, to.y);
|
||||||
|
ctx.lineTo(to.x - head * Math.cos(angle - Math.PI / 7), to.y - head * Math.sin(angle - Math.PI / 7));
|
||||||
|
ctx.lineTo(to.x - head * Math.cos(angle + Math.PI / 7), to.y - head * Math.sin(angle + Math.PI / 7));
|
||||||
|
ctx.closePath();
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
drawLabel(text, at, color, size, align = 'center') {
|
||||||
|
if (!text) return;
|
||||||
|
const ctx = this.ctx;
|
||||||
|
ctx.save();
|
||||||
|
ctx.shadowBlur = 0;
|
||||||
|
ctx.font = `bold ${size}px 'Segoe UI', sans-serif`;
|
||||||
|
ctx.textAlign = align;
|
||||||
|
ctx.textBaseline = 'middle';
|
||||||
|
ctx.lineWidth = Math.max(3, size / 4);
|
||||||
|
ctx.strokeStyle = 'rgba(0, 0, 0, 0.85)';
|
||||||
|
ctx.lineJoin = 'round';
|
||||||
|
ctx.strokeText(text, at.x, at.y);
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
ctx.fillText(text, at.x, at.y);
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
centroid(pts) {
|
||||||
|
const sum = pts.reduce((acc, p) => ({ x: acc.x + p.x, y: acc.y + p.y }), { x: 0, y: 0 });
|
||||||
|
return { x: sum.x / pts.length, y: sum.y / pts.length };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Die oberste Form des aktuellen Schritts an einer Bildschirmposition (fuer den
|
||||||
|
* Radierer), oder null.
|
||||||
|
*/
|
||||||
|
hitTest(screenX, screenY) {
|
||||||
|
for (let i = this.shapes.length - 1; i >= 0; i--) {
|
||||||
|
if (this.hitsShape(this.shapes[i], screenX, screenY)) {
|
||||||
|
return this.shapes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
hitsShape(shape, x, y) {
|
||||||
|
const pts = shape.points.map(p => this.toScreen(p));
|
||||||
|
const tolerance = 8 + (shape.width || 4) / 2;
|
||||||
|
|
||||||
|
switch (shape.type) {
|
||||||
|
case 'pen':
|
||||||
|
case 'line':
|
||||||
|
case 'arrow':
|
||||||
|
return this.distanceToPolyline(pts, x, y) <= tolerance;
|
||||||
|
case 'area':
|
||||||
|
return this.insidePolygon(pts, x, y) || this.distanceToPolyline([...pts, pts[0]], x, y) <= tolerance;
|
||||||
|
case 'circle':
|
||||||
|
return Math.hypot(x - pts[0].x, y - pts[0].y) <= Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y) + tolerance;
|
||||||
|
case 'point':
|
||||||
|
return Math.hypot(x - pts[0].x, y - pts[0].y) <= 6 + (shape.width || 4) / 2 + tolerance;
|
||||||
|
case 'text': {
|
||||||
|
const size = 12 + (shape.width || 4) * 2;
|
||||||
|
this.ctx.font = `bold ${size}px 'Segoe UI', sans-serif`;
|
||||||
|
const half = this.ctx.measureText(shape.label || '').width / 2;
|
||||||
|
return Math.abs(x - pts[0].x) <= half + 4 && Math.abs(y - pts[0].y) <= size / 2 + 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
distanceToPolyline(pts, x, y) {
|
||||||
|
let best = Infinity;
|
||||||
|
for (let i = 0; i < pts.length - 1; i++) {
|
||||||
|
best = Math.min(best, TacticLayer.distanceToSegment(pts[i], pts[i + 1], x, y));
|
||||||
|
}
|
||||||
|
return pts.length === 1 ? Math.hypot(x - pts[0].x, y - pts[0].y) : best;
|
||||||
|
}
|
||||||
|
|
||||||
|
static distanceToSegment(a, b, x, y) {
|
||||||
|
const dx = b.x - a.x;
|
||||||
|
const dy = b.y - a.y;
|
||||||
|
const lengthSq = dx * dx + dy * dy;
|
||||||
|
const t = lengthSq === 0 ? 0 : Math.max(0, Math.min(1, ((x - a.x) * dx + (y - a.y) * dy) / lengthSq));
|
||||||
|
return Math.hypot(x - (a.x + t * dx), y - (a.y + t * dy));
|
||||||
|
}
|
||||||
|
|
||||||
|
insidePolygon(pts, x, y) {
|
||||||
|
let inside = false;
|
||||||
|
for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) {
|
||||||
|
if ((pts[i].y > y) !== (pts[j].y > y)
|
||||||
|
&& x < (pts[j].x - pts[i].x) * (y - pts[i].y) / (pts[j].y - pts[i].y) + pts[i].x) {
|
||||||
|
inside = !inside;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inside;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vereinfacht eine Freihandlinie (Ramer-Douglas-Peucker), damit nicht jeder
|
||||||
|
* Mausschritt als eigener Punkt gespeichert wird. tolerance in Welteinheiten.
|
||||||
|
*/
|
||||||
|
static simplify(points, tolerance) {
|
||||||
|
if (points.length <= 2) return points;
|
||||||
|
|
||||||
|
const toXY = p => ({ x: p[0], y: p[1] });
|
||||||
|
let maxDist = 0;
|
||||||
|
let index = 0;
|
||||||
|
const first = toXY(points[0]);
|
||||||
|
const last = toXY(points[points.length - 1]);
|
||||||
|
for (let i = 1; i < points.length - 1; i++) {
|
||||||
|
const p = toXY(points[i]);
|
||||||
|
const dist = TacticLayer.distanceToSegment(first, last, p.x, p.y);
|
||||||
|
if (dist > maxDist) {
|
||||||
|
maxDist = dist;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxDist <= tolerance) {
|
||||||
|
return [points[0], points[points.length - 1]];
|
||||||
|
}
|
||||||
|
const left = TacticLayer.simplify(points.slice(0, index + 1), tolerance);
|
||||||
|
const right = TacticLayer.simplify(points.slice(index), tolerance);
|
||||||
|
return left.slice(0, -1).concat(right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -260,4 +260,478 @@ button:hover {
|
||||||
#category-panel::-webkit-scrollbar-thumb {
|
#category-panel::-webkit-scrollbar-thumb {
|
||||||
background: rgba(100, 100, 150, 0.5);
|
background: rgba(100, 100, 150, 0.5);
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------ */
|
||||||
|
/* Alle anzeigen / Alle verstecken */
|
||||||
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
.category-bulk {
|
||||||
|
display: flex;
|
||||||
|
gap: 6px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.category-bulk button {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 5px 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------ */
|
||||||
|
/* Taktiken */
|
||||||
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
/* Zeichenebene ueber der Karte - faengt die Maus nur ab, wenn ein Werkzeug aktiv ist. */
|
||||||
|
#tactic-canvas {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-canvas.drawing {
|
||||||
|
pointer-events: auto;
|
||||||
|
cursor: crosshair;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-canvas.erasing {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-panel {
|
||||||
|
position: absolute;
|
||||||
|
top: 16px;
|
||||||
|
right: 76px;
|
||||||
|
width: 290px;
|
||||||
|
max-height: calc(100% - 90px);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
padding: 10px 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-panel.collapsed .tp-body {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-head {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-head h3 {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-body {
|
||||||
|
margin-top: 8px;
|
||||||
|
overflow-y: auto;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-collapse {
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
padding: 0 4px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-collapse:hover {
|
||||||
|
background: none;
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-btn {
|
||||||
|
font-size: 12px;
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-icon {
|
||||||
|
background: none;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
padding: 1px 5px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-icon:hover:not(:disabled) {
|
||||||
|
background: rgba(255, 255, 255, 0.08);
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-icon:disabled {
|
||||||
|
opacity: 0.3;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-danger:hover:not(:disabled) {
|
||||||
|
color: #ff6b6b;
|
||||||
|
border-color: rgba(255, 107, 107, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-edit-only {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-panel.can-edit .tp-edit-only {
|
||||||
|
display: flex !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-empty,
|
||||||
|
.tp-message {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-message {
|
||||||
|
color: #ff9b9b;
|
||||||
|
margin-top: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: baseline;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 8px;
|
||||||
|
width: 100%;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
padding: 6px 8px;
|
||||||
|
font-size: 13px;
|
||||||
|
text-align: left;
|
||||||
|
background: rgba(40, 40, 70, 0.6);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tp-item.active {
|
||||||
|
border-color: var(--accent-gold);
|
||||||
|
background: rgba(255, 215, 0, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-item-name {
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-item-meta {
|
||||||
|
flex-shrink: 0;
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-detail {
|
||||||
|
margin-top: 10px;
|
||||||
|
padding-top: 10px;
|
||||||
|
border-top: 1px solid rgba(255, 215, 0, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-detail-head {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-detail-head strong {
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--accent-gold);
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-desc {
|
||||||
|
margin-top: 6px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
white-space: pre-wrap;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-detail h4 {
|
||||||
|
margin: 10px 0 4px;
|
||||||
|
font-size: 11px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-steps {
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 4px 6px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step:hover {
|
||||||
|
background: rgba(255, 255, 255, 0.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step.active {
|
||||||
|
border-color: var(--accent-gold);
|
||||||
|
background: rgba(255, 215, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step-no {
|
||||||
|
flex-shrink: 0;
|
||||||
|
width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
line-height: 18px;
|
||||||
|
border-radius: 50%;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 10px;
|
||||||
|
font-weight: bold;
|
||||||
|
background: rgba(255, 215, 0, 0.2);
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step-name {
|
||||||
|
flex: 1;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step-count {
|
||||||
|
font-size: 10px;
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step-tools {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step:hover .tp-step-tools,
|
||||||
|
.tp-step.active .tp-step-tools {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-step-actions {
|
||||||
|
gap: 6px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-check {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
margin-top: 10px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tp-check input {
|
||||||
|
accent-color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Werkzeugleiste im Taktik-Panel - bleibt auch beim Einklappen sichtbar */
|
||||||
|
#tactic-toolbar {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
margin-top: 8px;
|
||||||
|
padding-top: 8px;
|
||||||
|
border-top: 1px solid rgba(255, 215, 0, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-toolbar.hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-toolbar .tool {
|
||||||
|
min-width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 5px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-toolbar .tool.active {
|
||||||
|
border-color: var(--accent-gold);
|
||||||
|
background: rgba(255, 215, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tool-sep {
|
||||||
|
width: 1px;
|
||||||
|
height: 22px;
|
||||||
|
margin: 0 2px;
|
||||||
|
background: var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
#tool-color {
|
||||||
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 2px;
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
background: rgba(50, 50, 80, 0.8);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tool-width {
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 6px;
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
background: rgba(50, 50, 80, 0.8);
|
||||||
|
color: var(--text-primary);
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Schritt-Leiste unten mittig zwischen Kategorien-Panel (links, 300px) und
|
||||||
|
Zoom-Anzeige (rechts) - left/right plus margin auto zentriert sie in diesem Streifen. */
|
||||||
|
#step-bar {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 16px;
|
||||||
|
left: 332px;
|
||||||
|
right: 100px;
|
||||||
|
margin: 0 auto;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 8px 10px;
|
||||||
|
width: fit-content;
|
||||||
|
min-width: 240px;
|
||||||
|
max-width: 560px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#step-bar button:disabled {
|
||||||
|
opacity: 0.3;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sb-text {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sb-title {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
.sb-desc {
|
||||||
|
margin-top: 3px;
|
||||||
|
max-height: 60px;
|
||||||
|
overflow-y: auto;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Eingabedialog */
|
||||||
|
#tactic-dialog {
|
||||||
|
position: absolute;
|
||||||
|
inset: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
background: rgba(0, 0, 0, 0.45);
|
||||||
|
z-index: 2000;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-dialog form {
|
||||||
|
width: min(420px, calc(100% - 32px));
|
||||||
|
padding: 18px 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tactic-dialog h3 {
|
||||||
|
margin-bottom: 12px;
|
||||||
|
font-size: 16px;
|
||||||
|
color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
.td-field {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.td-field input,
|
||||||
|
.td-field textarea {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 4px;
|
||||||
|
padding: 7px 9px;
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
background: rgba(30, 30, 60, 0.9);
|
||||||
|
color: var(--text-primary);
|
||||||
|
font: inherit;
|
||||||
|
font-size: 13px;
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
.td-field input:focus,
|
||||||
|
.td-field textarea:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: var(--accent-gold);
|
||||||
|
}
|
||||||
|
|
||||||
|
.td-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
margin-top: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#td-submit {
|
||||||
|
border-color: var(--accent-gold);
|
||||||
|
background: rgba(255, 215, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Kurze Rueckmeldungen */
|
||||||
|
#toast {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 96px;
|
||||||
|
left: 332px;
|
||||||
|
right: 100px;
|
||||||
|
margin: 0 auto;
|
||||||
|
width: fit-content;
|
||||||
|
max-width: 480px;
|
||||||
|
padding: 8px 14px;
|
||||||
|
border: 1px solid rgba(255, 215, 0, 0.5);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: rgba(10, 10, 30, 0.95);
|
||||||
|
color: var(--text-primary);
|
||||||
|
font-size: 13px;
|
||||||
|
text-align: center;
|
||||||
|
z-index: 1500;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#toast.error {
|
||||||
|
border-color: #ff6b6b;
|
||||||
|
color: #ffb3b3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bei schmaler Karte stoesst der mittige Titel sonst an das Taktik-Panel. */
|
||||||
|
@media (max-width: 1100px) {
|
||||||
|
#title-bar {
|
||||||
|
left: 16px;
|
||||||
|
transform: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#title-bar h1 {
|
||||||
|
font-size: 18px;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-selector-container select {
|
||||||
|
min-width: 180px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,8 @@ const CORE_PERMISSIONS = [
|
||||||
['key' => 'premades.manage', 'description' => 'Alle Premade-Gruppen verwalten (nicht nur die eigene)'],
|
['key' => 'premades.manage', 'description' => 'Alle Premade-Gruppen verwalten (nicht nur die eigene)'],
|
||||||
['key' => 'events.view', 'description' => 'Geplante Event-Erinnerungen einsehen'],
|
['key' => 'events.view', 'description' => 'Geplante Event-Erinnerungen einsehen'],
|
||||||
['key' => 'events.manage', 'description' => 'Event-Erinnerungen planen, bearbeiten und löschen'],
|
['key' => 'events.manage', 'description' => 'Event-Erinnerungen planen, bearbeiten und löschen'],
|
||||||
['key' => 'map.view', 'description' => 'Interaktive Karte ansehen'],
|
['key' => 'map.view', 'description' => 'Interaktive Karte und Taktiken ansehen'],
|
||||||
|
['key' => 'map.tactics', 'description' => 'Taktiken auf der Karte anlegen, zeichnen und bearbeiten'],
|
||||||
['key' => 'members.view', 'description' => 'Mitgliederliste einsehen'],
|
['key' => 'members.view', 'description' => 'Mitgliederliste einsehen'],
|
||||||
['key' => 'reports.view', 'description' => 'Eingegangene Reports einsehen'],
|
['key' => 'reports.view', 'description' => 'Eingegangene Reports einsehen'],
|
||||||
['key' => 'reports.manage', 'description' => 'Reports löschen/archivieren und die Punkte-Gewichtung verwalten'],
|
['key' => 'reports.manage', 'description' => 'Reports löschen/archivieren und die Punkte-Gewichtung verwalten'],
|
||||||
|
|
|
||||||
237
includes/tactics.php
Normal file
237
includes/tactics.php
Normal file
|
|
@ -0,0 +1,237 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Datenschicht der Taktiken auf der Karte. Die Karte selbst ist ein statischer Viewer
|
||||||
|
* (assets/map/), nur die Taktiken liegen in der Website-Datenbank - der Discord-Bot ist
|
||||||
|
* daran nicht beteiligt.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Die Tabellen in Anlegereihenfolge (Fremdschluessel!).
|
||||||
|
const TACTIC_TABLES = ['tactics', 'tactic_steps', 'tactic_shapes'];
|
||||||
|
|
||||||
|
const TACTIC_SHAPE_TYPES = ['pen', 'line', 'arrow', 'area', 'circle', 'point', 'text'];
|
||||||
|
|
||||||
|
// Die Weltkoordinaten der Karte laufen von 0 bis 4096. Etwas Luft rundherum, damit ein
|
||||||
|
// Pfeil auch knapp ueber den Kartenrand hinaus zeigen darf.
|
||||||
|
const TACTIC_COORD_MIN = -512.0;
|
||||||
|
const TACTIC_COORD_MAX = 4608.0;
|
||||||
|
|
||||||
|
// Freihandlinien werden im Browser vereinfacht - mehr Punkte kommen normal nicht zusammen.
|
||||||
|
const TACTIC_MAX_POINTS = 4000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Welche der Taktik-Tabellen fehlen noch? Bestehende Installationen kennen sie nicht -
|
||||||
|
* ohne diese Pruefung wuerde die erste Abfrage mit einer PDOException aussteigen.
|
||||||
|
*
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
function tactics_missing_tables(): array
|
||||||
|
{
|
||||||
|
$placeholders = implode(',', array_fill(0, count(TACTIC_TABLES), '?'));
|
||||||
|
$stmt = db()->prepare(
|
||||||
|
"SELECT table_name FROM information_schema.tables
|
||||||
|
WHERE table_schema = DATABASE() AND table_name IN ($placeholders)"
|
||||||
|
);
|
||||||
|
$stmt->execute(TACTIC_TABLES);
|
||||||
|
|
||||||
|
$existing = array_map('strtolower', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||||
|
|
||||||
|
return array_values(array_filter(
|
||||||
|
TACTIC_TABLES,
|
||||||
|
static fn (string $table): bool => !in_array(strtolower($table), $existing, true)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Legt die fehlenden Tabellen an. Die Befehle kommen aus sql/schema.sql, damit es genau
|
||||||
|
* eine Quelle fuer die Struktur gibt. Alle sind "IF NOT EXISTS" - ein zweiter Aufruf
|
||||||
|
* richtet keinen Schaden an.
|
||||||
|
*
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function tactics_create_tables(): void
|
||||||
|
{
|
||||||
|
$path = __DIR__ . '/../sql/schema.sql';
|
||||||
|
$schema = is_file($path) ? (string) file_get_contents($path) : '';
|
||||||
|
|
||||||
|
$statements = [];
|
||||||
|
foreach (TACTIC_TABLES as $table) {
|
||||||
|
$pattern = '/CREATE TABLE IF NOT EXISTS\s+' . preg_quote($table, '/') . '\s*\(.*?\)\s*ENGINE[^;]*;/s';
|
||||||
|
if (preg_match($pattern, $schema, $match) === 1) {
|
||||||
|
$statements[] = $match[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($statements) !== count(TACTIC_TABLES)) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
'sql/schema.sql wurde nicht gefunden oder enthält nicht alle Taktik-Tabellen - '
|
||||||
|
. 'bitte den Abschnitt "Taktiken auf der Karte" aus sql/schema.sql von Hand ausführen.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($statements as $sql) {
|
||||||
|
db()->exec($sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Die Regionen der Karte, wie sie der Viewer kennt (assets/map/data/maps.json).
|
||||||
|
* Fehlt die Datei - etwa weil der Datenordner noch nicht hochgeladen ist - wird nur
|
||||||
|
* das Format des Kartennamens geprueft.
|
||||||
|
*/
|
||||||
|
function tactics_valid_map(string $slug): bool
|
||||||
|
{
|
||||||
|
if (preg_match('/^[a-z0-9-]{1,60}$/', $slug) !== 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static $slugs = null;
|
||||||
|
if ($slugs === null) {
|
||||||
|
$slugs = [];
|
||||||
|
$path = __DIR__ . '/../assets/map/data/maps.json';
|
||||||
|
$data = is_file($path) ? json_decode((string) file_get_contents($path), true) : null;
|
||||||
|
foreach ((is_array($data) ? ($data['maps'] ?? []) : []) as $map) {
|
||||||
|
if (isset($map['slug'])) {
|
||||||
|
$slugs[] = (string) $map['slug'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return empty($slugs) || in_array($slug, $slugs, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alle Taktiken einer Region samt Schritten und Zeichnungen.
|
||||||
|
*
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
function tactics_for_map(string $slug): array
|
||||||
|
{
|
||||||
|
$stmt = db()->prepare(
|
||||||
|
'SELECT t.id, t.name, t.description, t.created_at, t.updated_at,
|
||||||
|
COALESCE(NULLIF(u.display_name, \'\'), u.username) AS author
|
||||||
|
FROM tactics t
|
||||||
|
LEFT JOIN users u ON u.id = t.created_by
|
||||||
|
WHERE t.map_slug = ?
|
||||||
|
ORDER BY t.name, t.id'
|
||||||
|
);
|
||||||
|
$stmt->execute([$slug]);
|
||||||
|
|
||||||
|
$tactics = [];
|
||||||
|
foreach ($stmt->fetchAll() as $row) {
|
||||||
|
$tactics[(int) $row['id']] = [
|
||||||
|
'id' => (int) $row['id'],
|
||||||
|
'name' => (string) $row['name'],
|
||||||
|
'description' => (string) ($row['description'] ?? ''),
|
||||||
|
'author' => (string) ($row['author'] ?? ''),
|
||||||
|
'updated_at' => (string) ($row['updated_at'] ?? $row['created_at']),
|
||||||
|
'steps' => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if (empty($tactics)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$ids = array_keys($tactics);
|
||||||
|
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||||||
|
|
||||||
|
$stepStmt = db()->prepare(
|
||||||
|
"SELECT id, tactic_id, title, description FROM tactic_steps
|
||||||
|
WHERE tactic_id IN ($placeholders) ORDER BY sort_order, id"
|
||||||
|
);
|
||||||
|
$stepStmt->execute($ids);
|
||||||
|
|
||||||
|
$steps = [];
|
||||||
|
foreach ($stepStmt->fetchAll() as $row) {
|
||||||
|
$steps[(int) $row['id']] = [
|
||||||
|
'id' => (int) $row['id'],
|
||||||
|
'tactic_id' => (int) $row['tactic_id'],
|
||||||
|
'title' => (string) $row['title'],
|
||||||
|
'description' => (string) ($row['description'] ?? ''),
|
||||||
|
'shapes' => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($steps)) {
|
||||||
|
$stepIds = array_keys($steps);
|
||||||
|
$stepPlaceholders = implode(',', array_fill(0, count($stepIds), '?'));
|
||||||
|
$shapeStmt = db()->prepare(
|
||||||
|
"SELECT id, step_id, shape_type, label, color, width, points FROM tactic_shapes
|
||||||
|
WHERE step_id IN ($stepPlaceholders) ORDER BY id"
|
||||||
|
);
|
||||||
|
$shapeStmt->execute($stepIds);
|
||||||
|
|
||||||
|
foreach ($shapeStmt->fetchAll() as $row) {
|
||||||
|
$steps[(int) $row['step_id']]['shapes'][] = tactic_shape_row($row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($steps as $step) {
|
||||||
|
$tacticId = $step['tactic_id'];
|
||||||
|
unset($step['tactic_id']);
|
||||||
|
$tactics[$tacticId]['steps'][] = $step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values($tactics);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
function tactic_shape_row(array $row): array
|
||||||
|
{
|
||||||
|
$points = json_decode((string) $row['points'], true);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => (int) $row['id'],
|
||||||
|
'type' => (string) $row['shape_type'],
|
||||||
|
'label' => (string) $row['label'],
|
||||||
|
'color' => (string) $row['color'],
|
||||||
|
'width' => (int) $row['width'],
|
||||||
|
'points' => is_array($points) ? $points : [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prueft die Stuetzpunkte einer Zeichnung und gibt sie gerundet zurueck, oder null, wenn
|
||||||
|
* sie nicht zur Form passen.
|
||||||
|
*
|
||||||
|
* @return array<int, array{0: float, 1: float}>|null
|
||||||
|
*/
|
||||||
|
function tactic_clean_points($points, string $type): ?array
|
||||||
|
{
|
||||||
|
if (!is_array($points)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$clean = [];
|
||||||
|
foreach ($points as $point) {
|
||||||
|
if (!is_array($point) || count($point) !== 2) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
[$x, $y] = array_values($point);
|
||||||
|
if ((!is_int($x) && !is_float($x)) || (!is_int($y) && !is_float($y))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ($x < TACTIC_COORD_MIN || $x > TACTIC_COORD_MAX || $y < TACTIC_COORD_MIN || $y > TACTIC_COORD_MAX) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$clean[] = [round((float) $x, 2), round((float) $y, 2)];
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = count($clean);
|
||||||
|
$valid = match ($type) {
|
||||||
|
'point', 'text' => $count === 1,
|
||||||
|
'line', 'arrow', 'circle' => $count === 2,
|
||||||
|
'pen' => $count >= 2 && $count <= TACTIC_MAX_POINTS,
|
||||||
|
'area' => $count >= 3 && $count <= TACTIC_MAX_POINTS,
|
||||||
|
default => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
return $valid ? $clean : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Aktualisiert den Zeitstempel einer Taktik, damit die Liste zeigt, wann zuletzt gezeichnet wurde. */
|
||||||
|
function tactic_touch(int $tacticId): void
|
||||||
|
{
|
||||||
|
db()->prepare('UPDATE tactics SET updated_at = NOW() WHERE id = ?')->execute([$tacticId]);
|
||||||
|
}
|
||||||
30
map.php
30
map.php
|
|
@ -1,13 +1,39 @@
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/includes/bootstrap.php';
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/tactics.php';
|
||||||
require_permission('map.view');
|
require_permission('map.view');
|
||||||
|
|
||||||
|
// Bestehende Installationen kennen die Taktik-Tabellen noch nicht. Die Karte selbst
|
||||||
|
// funktioniert trotzdem, nur die Taktiken brauchen die Datenbank.
|
||||||
|
try {
|
||||||
|
$missingTables = tactics_missing_tables();
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$missingTables = TACTIC_TABLES;
|
||||||
|
}
|
||||||
|
|
||||||
// Die Karte ist ein eigenstaendiger Viewer (assets/map/) mit eigenem Stylesheet, das
|
// Die Karte ist ein eigenstaendiger Viewer (assets/map/) mit eigenem Stylesheet, das
|
||||||
// global auf body/button/* greift. Im iframe bleibt er vom Seitenlayout getrennt und
|
// global auf body/button/* greift. Im iframe bleibt er vom Seitenlayout getrennt.
|
||||||
// laeuft unveraendert so, wie er geliefert wurde.
|
// Die Taktiken laedt und speichert er selbst ueber tactics_api.php.
|
||||||
$pageTitle = 'Karte';
|
$pageTitle = 'Karte';
|
||||||
$containerWide = true;
|
$containerWide = true;
|
||||||
include __DIR__ . '/includes/header.php';
|
include __DIR__ . '/includes/header.php';
|
||||||
?>
|
?>
|
||||||
|
<?php if (!empty($missingTables)): ?>
|
||||||
|
<div class="panel map-setup">
|
||||||
|
<p>
|
||||||
|
<strong>Taktiken einrichten:</strong>
|
||||||
|
<span class="text-muted">Der Datenbank fehlen noch die Tabellen für Taktiken
|
||||||
|
(<?= e(implode(', ', $missingTables)) ?>). Die Karte funktioniert auch ohne sie.</span>
|
||||||
|
</p>
|
||||||
|
<?php if (user_has_permission('roles.manage')): ?>
|
||||||
|
<form method="post" action="/tactics_install_action.php">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<button type="submit">Tabellen jetzt anlegen</button>
|
||||||
|
</form>
|
||||||
|
<?php else: ?>
|
||||||
|
<p class="text-muted">Dafür fehlt dir die Berechtigung <code>roles.manage</code> - bitte einen Administrator.</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
<iframe class="map-frame" src="/assets/map/index.html" title="Interaktive Karte"></iframe>
|
<iframe class="map-frame" src="/assets/map/index.html" title="Interaktive Karte"></iframe>
|
||||||
<?php include __DIR__ . '/includes/footer.php'; ?>
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|
||||||
|
|
|
||||||
|
|
@ -55,3 +55,50 @@ CREATE TABLE IF NOT EXISTS user_roles (
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
|
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- --------------------------------------------------------------------------
|
||||||
|
-- Taktiken auf der Karte
|
||||||
|
-- --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- Eine Taktik gehoert zu einer Region der Karte (map_slug aus assets/map/data/maps.json)
|
||||||
|
-- und besteht aus nacheinander ablaufenden Schritten.
|
||||||
|
CREATE TABLE IF NOT EXISTS tactics (
|
||||||
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
map_slug VARCHAR(60) NOT NULL,
|
||||||
|
name VARCHAR(120) NOT NULL,
|
||||||
|
description TEXT NULL,
|
||||||
|
created_by INT UNSIGNED NULL,
|
||||||
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at DATETIME NULL,
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,
|
||||||
|
INDEX idx_tactics_map (map_slug)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- Ein Schritt ist eine eigene Zeichenebene ("Schritt 1: Sammeln", "Schritt 2: Angriff").
|
||||||
|
CREATE TABLE IF NOT EXISTS tactic_steps (
|
||||||
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
tactic_id INT UNSIGNED NOT NULL,
|
||||||
|
title VARCHAR(120) NOT NULL DEFAULT '',
|
||||||
|
description TEXT NULL,
|
||||||
|
sort_order INT NOT NULL DEFAULT 0,
|
||||||
|
FOREIGN KEY (tactic_id) REFERENCES tactics(id) ON DELETE CASCADE,
|
||||||
|
INDEX idx_tactic_steps_tactic (tactic_id)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- points enthaelt die Stuetzpunkte als JSON-Array [[x,y], ...] in Weltkoordinaten der
|
||||||
|
-- Karte (0..4096, dieselben wie die Marker). TEXT statt JSON, damit auch aeltere
|
||||||
|
-- MySQL-/MariaDB-Versionen ohne JSON-Typ funktionieren.
|
||||||
|
CREATE TABLE IF NOT EXISTS tactic_shapes (
|
||||||
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
step_id INT UNSIGNED NOT NULL,
|
||||||
|
shape_type ENUM('pen', 'line', 'arrow', 'area', 'circle', 'point', 'text') NOT NULL,
|
||||||
|
label VARCHAR(200) NOT NULL DEFAULT '',
|
||||||
|
color VARCHAR(7) NOT NULL DEFAULT '#ffd700',
|
||||||
|
width TINYINT UNSIGNED NOT NULL DEFAULT 4,
|
||||||
|
points MEDIUMTEXT NOT NULL,
|
||||||
|
created_by INT UNSIGNED NULL,
|
||||||
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (step_id) REFERENCES tactic_steps(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,
|
||||||
|
INDEX idx_tactic_shapes_step (step_id)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
|
||||||
272
tactics_api.php
Normal file
272
tactics_api.php
Normal file
|
|
@ -0,0 +1,272 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/tactics.php';
|
||||||
|
|
||||||
|
// Wird vom Karten-Viewer im iframe per fetch() aufgerufen - deshalb Fehler immer als JSON
|
||||||
|
// statt Weiterleitung auf die Login-Seite bzw. HTML-Fehlerseite.
|
||||||
|
if (!current_user()) {
|
||||||
|
http_response_code(401);
|
||||||
|
json_response(false, 'Bitte neu anmelden.');
|
||||||
|
}
|
||||||
|
if (!user_has_permission('map.view')) {
|
||||||
|
http_response_code(403);
|
||||||
|
json_response(false, 'Dir fehlt die Berechtigung, die Karte zu sehen.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$canEdit = user_has_permission('map.tactics');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$missing = tactics_missing_tables();
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
json_response(false, 'Datenbankfehler: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
if (!empty($missing)) {
|
||||||
|
json_response(false, 'Die Tabellen für Taktiken fehlen noch - sie lassen sich auf der Kartenseite anlegen.', [
|
||||||
|
'missing_tables' => $missing,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Lesen ----
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||||
|
$slug = (string) ($_GET['map'] ?? '');
|
||||||
|
if (!tactics_valid_map($slug)) {
|
||||||
|
json_response(false, 'Unbekannte Karte.');
|
||||||
|
}
|
||||||
|
json_response(true, '', [
|
||||||
|
'can_edit' => $canEdit,
|
||||||
|
'tactics' => tactics_for_map($slug),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Schreiben ----
|
||||||
|
$input = read_json_body();
|
||||||
|
verify_csrf_token($input['csrf_token'] ?? '');
|
||||||
|
|
||||||
|
if (!$canEdit) {
|
||||||
|
json_response(false, 'Dir fehlt die Berechtigung, Taktiken zu bearbeiten.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$userId = (int) current_user()['id'];
|
||||||
|
$action = (string) ($input['action'] ?? '');
|
||||||
|
|
||||||
|
/** Liest einen Text aus dem Request, gekuerzt und auf die Hoechstlaenge geprueft. */
|
||||||
|
function tactic_text(array $input, string $key, int $max, string $label, bool $required): string
|
||||||
|
{
|
||||||
|
$value = trim((string) ($input[$key] ?? ''));
|
||||||
|
if ($required && $value === '') {
|
||||||
|
json_response(false, "Bitte $label angeben.");
|
||||||
|
}
|
||||||
|
if (mb_strlen($value) > $max) {
|
||||||
|
json_response(false, ucfirst($label) . " darf höchstens $max Zeichen lang sein.");
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Sucht einen Schritt und liefert die Taktik, zu der er gehoert. */
|
||||||
|
function tactic_step_owner(int $stepId): array
|
||||||
|
{
|
||||||
|
$stmt = db()->prepare('SELECT id, tactic_id, sort_order FROM tactic_steps WHERE id = ?');
|
||||||
|
$stmt->execute([$stepId]);
|
||||||
|
$step = $stmt->fetch();
|
||||||
|
if (!$step) {
|
||||||
|
json_response(false, 'Diesen Schritt gibt es nicht mehr - bitte die Seite neu laden.');
|
||||||
|
}
|
||||||
|
return $step;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tactic_exists(int $tacticId): void
|
||||||
|
{
|
||||||
|
$stmt = db()->prepare('SELECT 1 FROM tactics WHERE id = ?');
|
||||||
|
$stmt->execute([$tacticId]);
|
||||||
|
if (!$stmt->fetchColumn()) {
|
||||||
|
json_response(false, 'Diese Taktik gibt es nicht mehr - bitte die Seite neu laden.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Legt einen Schritt am Ende der Taktik an und gibt seine ID zurueck. */
|
||||||
|
function tactic_append_step(int $tacticId, string $title, string $description = ''): int
|
||||||
|
{
|
||||||
|
$stmt = db()->prepare('SELECT COALESCE(MAX(sort_order), 0) + 1 FROM tactic_steps WHERE tactic_id = ?');
|
||||||
|
$stmt->execute([$tacticId]);
|
||||||
|
$order = (int) $stmt->fetchColumn();
|
||||||
|
|
||||||
|
db()->prepare('INSERT INTO tactic_steps (tactic_id, title, description, sort_order) VALUES (?, ?, ?, ?)')
|
||||||
|
->execute([$tacticId, $title, $description, $order]);
|
||||||
|
|
||||||
|
return (int) db()->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch ($action) {
|
||||||
|
// ---- Taktiken ----
|
||||||
|
case 'tactic_create':
|
||||||
|
$slug = (string) ($input['map'] ?? '');
|
||||||
|
if (!tactics_valid_map($slug)) {
|
||||||
|
json_response(false, 'Unbekannte Karte.');
|
||||||
|
}
|
||||||
|
$name = tactic_text($input, 'name', 120, 'einen Namen', true);
|
||||||
|
$description = tactic_text($input, 'description', 5000, 'die Beschreibung', false);
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
$pdo->beginTransaction();
|
||||||
|
$pdo->prepare('INSERT INTO tactics (map_slug, name, description, created_by) VALUES (?, ?, ?, ?)')
|
||||||
|
->execute([$slug, $name, $description, $userId]);
|
||||||
|
$tacticId = (int) $pdo->lastInsertId();
|
||||||
|
// Ohne Titel - angezeigt wird dann "Schritt 1".
|
||||||
|
$stepId = tactic_append_step($tacticId, '');
|
||||||
|
$pdo->commit();
|
||||||
|
|
||||||
|
json_response(true, 'Taktik angelegt.', ['tactic_id' => $tacticId, 'step_id' => $stepId]);
|
||||||
|
|
||||||
|
case 'tactic_update':
|
||||||
|
$tacticId = (int) ($input['tactic_id'] ?? 0);
|
||||||
|
tactic_exists($tacticId);
|
||||||
|
$name = tactic_text($input, 'name', 120, 'einen Namen', true);
|
||||||
|
$description = tactic_text($input, 'description', 5000, 'die Beschreibung', false);
|
||||||
|
|
||||||
|
db()->prepare('UPDATE tactics SET name = ?, description = ?, updated_at = NOW() WHERE id = ?')
|
||||||
|
->execute([$name, $description, $tacticId]);
|
||||||
|
json_response(true, 'Taktik gespeichert.');
|
||||||
|
|
||||||
|
case 'tactic_delete':
|
||||||
|
$tacticId = (int) ($input['tactic_id'] ?? 0);
|
||||||
|
db()->prepare('DELETE FROM tactics WHERE id = ?')->execute([$tacticId]);
|
||||||
|
json_response(true, 'Taktik gelöscht.');
|
||||||
|
|
||||||
|
// ---- Schritte ----
|
||||||
|
case 'step_create':
|
||||||
|
$tacticId = (int) ($input['tactic_id'] ?? 0);
|
||||||
|
tactic_exists($tacticId);
|
||||||
|
$title = tactic_text($input, 'title', 120, 'einen Titel', false);
|
||||||
|
$description = tactic_text($input, 'description', 5000, 'die Beschreibung', false);
|
||||||
|
$copyFrom = (int) ($input['copy_from'] ?? 0);
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
$pdo->beginTransaction();
|
||||||
|
$stepId = tactic_append_step($tacticId, $title, $description);
|
||||||
|
|
||||||
|
// "Als Kopie": der neue Schritt startet mit den Zeichnungen des alten, damit
|
||||||
|
// man nur noch einzeichnen muss, was sich von einem Schritt zum naechsten aendert.
|
||||||
|
if ($copyFrom > 0) {
|
||||||
|
$source = tactic_step_owner($copyFrom);
|
||||||
|
if ((int) $source['tactic_id'] !== $tacticId) {
|
||||||
|
$pdo->rollBack();
|
||||||
|
json_response(false, 'Der Schritt gehört zu einer anderen Taktik.');
|
||||||
|
}
|
||||||
|
$pdo->prepare(
|
||||||
|
'INSERT INTO tactic_shapes (step_id, shape_type, label, color, width, points, created_by)
|
||||||
|
SELECT ?, shape_type, label, color, width, points, ? FROM tactic_shapes WHERE step_id = ? ORDER BY id'
|
||||||
|
)->execute([$stepId, $userId, $copyFrom]);
|
||||||
|
}
|
||||||
|
tactic_touch($tacticId);
|
||||||
|
$pdo->commit();
|
||||||
|
|
||||||
|
json_response(true, 'Schritt angelegt.', ['step_id' => $stepId]);
|
||||||
|
|
||||||
|
case 'step_update':
|
||||||
|
$step = tactic_step_owner((int) ($input['step_id'] ?? 0));
|
||||||
|
$title = tactic_text($input, 'title', 120, 'einen Titel', false);
|
||||||
|
$description = tactic_text($input, 'description', 5000, 'die Beschreibung', false);
|
||||||
|
|
||||||
|
db()->prepare('UPDATE tactic_steps SET title = ?, description = ? WHERE id = ?')
|
||||||
|
->execute([$title, $description, (int) $step['id']]);
|
||||||
|
tactic_touch((int) $step['tactic_id']);
|
||||||
|
json_response(true, 'Schritt gespeichert.');
|
||||||
|
|
||||||
|
case 'step_delete':
|
||||||
|
$step = tactic_step_owner((int) ($input['step_id'] ?? 0));
|
||||||
|
|
||||||
|
$count = db()->prepare('SELECT COUNT(*) FROM tactic_steps WHERE tactic_id = ?');
|
||||||
|
$count->execute([(int) $step['tactic_id']]);
|
||||||
|
if ((int) $count->fetchColumn() <= 1) {
|
||||||
|
json_response(false, 'Eine Taktik braucht mindestens einen Schritt.');
|
||||||
|
}
|
||||||
|
|
||||||
|
db()->prepare('DELETE FROM tactic_steps WHERE id = ?')->execute([(int) $step['id']]);
|
||||||
|
tactic_touch((int) $step['tactic_id']);
|
||||||
|
json_response(true, 'Schritt gelöscht.');
|
||||||
|
|
||||||
|
case 'step_move':
|
||||||
|
$step = tactic_step_owner((int) ($input['step_id'] ?? 0));
|
||||||
|
$direction = (int) ($input['direction'] ?? 0) < 0 ? -1 : 1;
|
||||||
|
|
||||||
|
// Reihenfolge neu durchnummerieren und mit dem Nachbarn tauschen - so stoeren
|
||||||
|
// doppelte oder lueckenhafte sort_order-Werte nicht.
|
||||||
|
$stmt = db()->prepare('SELECT id FROM tactic_steps WHERE tactic_id = ? ORDER BY sort_order, id');
|
||||||
|
$stmt->execute([(int) $step['tactic_id']]);
|
||||||
|
$ids = array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||||
|
|
||||||
|
$index = array_search((int) $step['id'], $ids, true);
|
||||||
|
$target = $index + $direction;
|
||||||
|
if ($index === false || $target < 0 || $target >= count($ids)) {
|
||||||
|
json_response(true, '');
|
||||||
|
}
|
||||||
|
[$ids[$index], $ids[$target]] = [$ids[$target], $ids[$index]];
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
$pdo->beginTransaction();
|
||||||
|
$update = $pdo->prepare('UPDATE tactic_steps SET sort_order = ? WHERE id = ?');
|
||||||
|
foreach ($ids as $position => $id) {
|
||||||
|
$update->execute([$position + 1, $id]);
|
||||||
|
}
|
||||||
|
tactic_touch((int) $step['tactic_id']);
|
||||||
|
$pdo->commit();
|
||||||
|
json_response(true, 'Schritt verschoben.');
|
||||||
|
|
||||||
|
// ---- Zeichnungen ----
|
||||||
|
case 'shape_create':
|
||||||
|
$step = tactic_step_owner((int) ($input['step_id'] ?? 0));
|
||||||
|
$type = (string) ($input['type'] ?? '');
|
||||||
|
if (!in_array($type, TACTIC_SHAPE_TYPES, true)) {
|
||||||
|
json_response(false, 'Unbekannte Form.');
|
||||||
|
}
|
||||||
|
$points = tactic_clean_points($input['points'] ?? null, $type);
|
||||||
|
if ($points === null) {
|
||||||
|
json_response(false, 'Die Zeichnung ist ungültig oder liegt außerhalb der Karte.');
|
||||||
|
}
|
||||||
|
$color = (string) ($input['color'] ?? '');
|
||||||
|
if (preg_match('/^#[0-9a-fA-F]{6}$/', $color) !== 1) {
|
||||||
|
$color = '#ffd700';
|
||||||
|
}
|
||||||
|
$width = max(1, min(30, (int) ($input['width'] ?? 4)));
|
||||||
|
$label = tactic_text($input, 'label', 200, 'die Beschriftung', $type === 'text');
|
||||||
|
|
||||||
|
db()->prepare(
|
||||||
|
'INSERT INTO tactic_shapes (step_id, shape_type, label, color, width, points, created_by)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)'
|
||||||
|
)->execute([(int) $step['id'], $type, $label, strtolower($color), $width, json_encode($points), $userId]);
|
||||||
|
$shapeId = (int) db()->lastInsertId();
|
||||||
|
tactic_touch((int) $step['tactic_id']);
|
||||||
|
|
||||||
|
json_response(true, '', ['shape' => [
|
||||||
|
'id' => $shapeId,
|
||||||
|
'type' => $type,
|
||||||
|
'label' => $label,
|
||||||
|
'color' => strtolower($color),
|
||||||
|
'width' => $width,
|
||||||
|
'points' => $points,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
case 'shape_delete':
|
||||||
|
$shapeId = (int) ($input['shape_id'] ?? 0);
|
||||||
|
$stmt = db()->prepare(
|
||||||
|
'SELECT s.tactic_id FROM tactic_shapes sh JOIN tactic_steps s ON s.id = sh.step_id WHERE sh.id = ?'
|
||||||
|
);
|
||||||
|
$stmt->execute([$shapeId]);
|
||||||
|
$tacticId = (int) $stmt->fetchColumn();
|
||||||
|
|
||||||
|
db()->prepare('DELETE FROM tactic_shapes WHERE id = ?')->execute([$shapeId]);
|
||||||
|
if ($tacticId > 0) {
|
||||||
|
tactic_touch($tacticId);
|
||||||
|
}
|
||||||
|
json_response(true, '');
|
||||||
|
|
||||||
|
default:
|
||||||
|
json_response(false, 'Unbekannte Aktion.');
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
if (db()->inTransaction()) {
|
||||||
|
db()->rollBack();
|
||||||
|
}
|
||||||
|
json_response(false, 'Speichern fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
27
tactics_install_action.php
Normal file
27
tactics_install_action.php
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/tactics.php';
|
||||||
|
|
||||||
|
// Die Struktur der Datenbank zu aendern ist eine Administratoren-Aufgabe - deshalb nicht an
|
||||||
|
// map.tactics haengen, sondern an dieselbe Berechtigung wie die Rechteverwaltung.
|
||||||
|
require_permission('roles.manage');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/map.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
try {
|
||||||
|
tactics_create_tables();
|
||||||
|
|
||||||
|
$stillMissing = tactics_missing_tables();
|
||||||
|
if (!empty($stillMissing)) {
|
||||||
|
set_flash('error', 'Diese Tabellen konnten nicht angelegt werden: ' . implode(', ', $stillMissing));
|
||||||
|
} else {
|
||||||
|
set_flash('success', 'Die Tabellen für Taktiken wurden angelegt.');
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Anlegen fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect('/map.php');
|
||||||
Loading…
Reference in a new issue