/** * MapController - Main controller for the map viewer application */ class MapController { constructor() { this.mapData = new MapData(); this.tileService = new TileService(); this.markerService = new MarkerService(); // DOM elements this.canvas = document.getElementById('map-canvas'); this.mapPicker = document.getElementById('map-picker'); this.mapPickerTabs = document.getElementById('map-picker-tabs'); this.mapPickerList = document.getElementById('map-picker-list'); this.categoryPanel = document.getElementById('category-panel'); this.mapPickerBtn = document.getElementById('map-picker-btn'); this.mapCurrentName = document.getElementById('map-current-name'); this.categoriesList = document.getElementById('categories-list'); this.zoomLevelEl = document.getElementById('zoom-level'); this.loadingOverlay = document.getElementById('loading-overlay'); this.tooltip = document.getElementById('tooltip'); // Bereich: Weltkarten (Karte) oder Dungeons & Raids (Raidplaner) - bestimmt die Startkarte und // den Reiter der Kartenauswahl (?bereich=dungeons von raidplan.php) const requestedArea = new URLSearchParams(window.location.search).get('bereich'); this.area = CONFIG.AREAS.some(a => a.id === requestedArea) ? requestedArea : CONFIG.AREAS[0].id; this.pickerArea = this.area; // Gast des Consiliums (map_frame.php setzt LUMINA_GUEST = {karte, taktik}): nur die freigegebene Taktik auf // der Karte ihrer Sitzung - keine Kartenwahl, keine Marker und Kategorien (versteckt per body.guest-mode) this.guest = window.LUMINA_GUEST || null; if (this.guest) document.body.classList.add('guest-mode'); // Initialize view this.view = new MapView(this.canvas); this.view.resize(); window.addEventListener('resize', () => { this.view.resize(); this.render(); }); } /** * Initialize the application */ async init() { this.showLoading(true); try { // Discover available maps await this.discoverMaps(); // Zuletzt gewaehlte Karte des Bereichs, sonst die Standardkarte, sonst die erste if (this.mapData.maps.length > 0) { const areaMaps = this.mapData.maps.filter(m => m.area === this.area); const candidates = areaMaps.length ? areaMaps : this.mapData.maps; let saved = null; try { saved = localStorage.getItem(this.areaInfo(this.area).storage); } catch (e) { saved = null; } const has = slug => candidates.some(m => m.slug === slug); // Ein geteilter Ort (?karte=...) hat Vorrang vor der zuletzt gewaehlten Karte - Gaeste bekommen immer // die Karte ihrer Sitzung const linked = this.locations ? this.locations.requestedMap() : null; const slug = this.guest ? this.guest.karte : linked || (has(saved) ? saved : (has(CONFIG.DEFAULT_MAP) ? CONFIG.DEFAULT_MAP : candidates[0].slug)); await this.loadMap(slug); if (this.locations) this.locations.applyLink(); } } catch (e) { console.error('Failed to initialize:', e); } finally { this.showLoading(false); } // Setup event listeners this.setupEventListeners(); } /** * Discover available maps from local maps.json file */ async discoverMaps() { try { const response = await fetch(CONFIG.MAPS_LIST); if (!response.ok) throw new Error('maps.json not found'); const data = await response.json(); const maps = data.maps || []; for (const mapEntry of maps) { this.mapData.addMap({ id: mapEntry.id, slug: mapEntry.slug, name: CONFIG.MAP_NAMES[mapEntry.slug] || mapEntry.name, zone: mapEntry.zone }); } } catch (e) { console.warn('Failed to load maps list:', e); } // Dungeon- und Raid-Karten des Raidplaners - kommen mit dem Code, nicht mit dem Datenordner try { const response = await fetch(CONFIG.DUNGEONS_LIST, { cache: 'no-cache' }); if (!response.ok) throw new Error('dungeons.json not found'); const data = await response.json(); const version = String((data.source && data.source.commit) || '').slice(0, 10); for (const group of data.groups || []) { for (const entry of group.maps || []) { this.mapData.addMap({ id: entry.slug, slug: entry.slug, name: entry.name, kind: entry.arena ? 'arena' : 'image', area: 'dungeons', group: group.name, hint: entry.hint, unavailable: entry.unavailable, bounds: entry.arena ? MapView.arenaBounds() : entry.bounds, arena: entry.arena, version }); } } } catch (e) { console.warn('Failed to load dungeon maps:', e); } // Populate map selector dropdown this.populateMapSelector(); } areaInfo(id) { return CONFIG.AREAS.find(a => a.id === id) || CONFIG.AREAS[0]; } /** Karten, die es auf unserem Patch noch nicht gibt (Weltkarten laut CONFIG, Dungeons laut dungeons.json). */ isUnavailable(map) { return !!map && (map.unavailable || CONFIG.MAP_UNAVAILABLE.includes(map.slug)); } /** * Kartenauswahl aufbauen: Reiter je Bereich (Welt / Dungeons & Raids), darin pro Gebiet bzw. * Gruppe eine Reihe Karten mit Vorschaubild. */ populateMapSelector() { // Gaeste wechseln die Karte nicht - dann auch keine Vorschaubilder laden if (this.guest) return; const maps = this.mapData.maps; this.mapPickerList.textContent = ''; if (maps.length === 0) { this.mapCurrentName.textContent = 'Keine Karten'; return; } // Weltkarten nach den Gebieten aus CONFIG.MAP_ZONES ordnen (maps.json kennt keine Gebiete). const worldMaps = maps.filter(m => m.area === 'welt'); const sections = []; for (const zone of CONFIG.MAP_ZONES) { const zoneMaps = zone.maps.map(slug => worldMaps.find(m => m.slug === slug)).filter(Boolean); if (zoneMaps.length) sections.push({ area: 'welt', name: zone.name, maps: zoneMaps }); } const assigned = new Set(CONFIG.MAP_ZONES.flatMap(zone => zone.maps)); const rest = worldMaps.filter(m => !assigned.has(m.slug)); if (rest.length) sections.push({ area: 'welt', name: 'Weitere', maps: rest }); // Dungeon-Karten in ihren Gruppen, wie in dungeons.json const groups = []; maps.filter(m => m.area === 'dungeons').forEach(map => { let group = groups.find(g => g.name === map.group); if (!group) { group = { area: 'dungeons', name: map.group, maps: [] }; groups.push(group); } group.maps.push(map); }); sections.push(...groups); // Reiter nur, wenn es beide Bereiche gibt const areas = CONFIG.AREAS.filter(a => sections.some(s => s.area === a.id)); this.mapPickerTabs.textContent = ''; this.mapPickerTabs.classList.toggle('hidden', areas.length < 2); areas.forEach(area => { const tab = document.createElement('button'); tab.type = 'button'; tab.className = 'map-picker-tab'; tab.dataset.area = area.id; tab.setAttribute('role', 'tab'); tab.textContent = area.label; tab.addEventListener('click', () => this.showPickerArea(area.id)); this.mapPickerTabs.appendChild(tab); }); for (const sectionInfo of sections) { const section = document.createElement('section'); section.className = 'map-picker-zone'; section.dataset.area = sectionInfo.area; const heading = document.createElement('h3'); heading.textContent = sectionInfo.name; const grid = document.createElement('div'); grid.className = 'map-picker-grid'; for (const map of sectionInfo.maps) { const card = document.createElement('button'); card.type = 'button'; card.className = 'map-card'; card.dataset.slug = map.slug; const img = document.createElement('img'); img.alt = ''; if (map.kind === 'arena') { img.src = MapView.arenaPreview(map.arena); } else { img.src = `${CONFIG.PREVIEWS_DIR}/${map.slug}.webp${map.version ? '?v=' + map.version : ''}`; // Die zehn Weltkarten (~300 KB) laden gleich mit, damit sie beim Oeffnen der Auswahl // sofort da sind - die vielen Dungeon-Karten erst, wenn ihr Reiter offen ist. if (map.area !== 'welt') img.loading = 'lazy'; } img.decoding = 'async'; img.onerror = () => img.remove(); const name = document.createElement('span'); name.className = 'map-card-name'; name.textContent = map.name; card.append(img, name); if (map.hint) { const hint = document.createElement('span'); hint.className = 'map-card-hint'; hint.textContent = map.hint; card.appendChild(hint); } if (this.isUnavailable(map)) { card.classList.add('unavailable'); const note = document.createElement('span'); note.className = 'map-card-note'; note.textContent = map.area === 'dungeons' ? 'Bisher nur in Korea/Taiwan' : 'Auf unserem Patch noch nicht verfügbar'; card.appendChild(note); } card.addEventListener('click', () => { this.closeMapPicker(); if (map.slug !== this.mapData.currentMap?.slug) { this.loadMap(map.slug); } }); grid.appendChild(card); } section.append(heading, grid); this.mapPickerList.appendChild(section); } this.showPickerArea(this.pickerArea); } /** Reiter der Kartenauswahl wechseln. */ showPickerArea(areaId) { if (!this.mapPickerList.querySelector(`.map-picker-zone[data-area="${areaId}"]`)) { // Den Bereich gibt es nicht (z.B. Weltkarten noch nicht hochgeladen): den ersten vorhandenen const first = this.mapPickerList.querySelector('.map-picker-zone'); areaId = first ? first.dataset.area : CONFIG.AREAS[0].id; } this.pickerArea = areaId; this.mapPickerTabs.querySelectorAll('.map-picker-tab').forEach(tab => { const active = tab.dataset.area === areaId; tab.classList.toggle('active', active); tab.setAttribute('aria-selected', String(active)); }); this.mapPickerList.querySelectorAll('.map-picker-zone').forEach(section => { section.classList.toggle('hidden', section.dataset.area !== areaId); }); this.mapPickerList.scrollTop = 0; } openMapPicker() { if (this.guest) return; // Die Auswahl oeffnet im Bereich der aktuellen Karte if (this.mapData.currentMap) this.showPickerArea(this.mapData.currentMap.area); this.mapPickerList.querySelectorAll('.map-card').forEach(card => { card.classList.toggle('active', card.dataset.slug === this.mapData.currentMap?.slug); }); this.mapPicker.classList.remove('hidden'); const active = this.mapPickerList.querySelector('.map-card.active') || this.mapPickerList.querySelector('.map-picker-zone:not(.hidden) .map-card'); if (active) active.focus(); } closeMapPicker() { this.mapPicker.classList.add('hidden'); this.mapPickerBtn.focus(); } /** * Load a specific map by slug */ async loadMap(slug) { // Gaeste bleiben auf der Karte ihrer Sitzung if (this.guest && slug !== this.guest.karte) return; this.showLoading(true); try { // Set current map in model const mapInfo = this.mapData.setCurrentMap(slug); if (!mapInfo) throw new Error(`Map ${slug} not found`); this.mapCurrentName.textContent = mapInfo.name; const unavailable = document.getElementById('map-unavailable'); unavailable.textContent = mapInfo.area === 'dungeons' ? '⚠️ Diese Karte gibt es bisher nur in Korea/Taiwan – bei uns kommt sie später.' : '⚠️ Diese Karte ist auf unserem Patch noch nicht verfügbar.'; unavailable.classList.toggle('hidden', !this.isUnavailable(mapInfo)); // Die Karte gehoert zu ihrem Bereich (auch wenn sie ueber den anderen Reiter gewaehlt wurde) this.area = mapInfo.area; document.body.classList.toggle('map-kind-tiles', mapInfo.kind === 'tiles'); try { if (!this.guest) localStorage.setItem(this.areaInfo(mapInfo.area).storage, slug); } catch (e) { // ohne Speicher eben jedes Mal die Standardkarte } if (mapInfo.kind === 'tiles') { this.view.zoomStep = 1; this.view.fitBounds = null; // Load tile manifest await this.tileService.loadManifest(slug); // Nur so weit zoomen, wie es Kacheln gibt (manche Karten enden bei Stufe 5) this.view.maxZoom = this.tileService.maxZoom(slug); // Load markers - Gaeste sehen nur die Taktik, keine Marker if (!this.guest) { try { const markerData = await this.markerService.loadMarkers(slug); this.mapData.loadMarkers(markerData); } catch (e) { console.warn(`No markers found for ${slug}`); } } } else { // Dungeon-Karte (ein Bild) oder leere Arena: feiner zoomen, beim Oeffnen auf den Dungeon this.view.zoomStep = 0.5; this.view.maxZoom = CONFIG.MAX_ZOOM; this.view.fitBounds = mapInfo.bounds || [CONFIG.WORLD_MIN_X, CONFIG.WORLD_MIN_Y, CONFIG.WORLD_MAX_X, CONFIG.WORLD_MAX_Y]; if (mapInfo.kind === 'image') await this.view.loadMapImage(mapInfo); } // Marker und ihre Kategorien gibt es nur auf den Weltkarten (und nicht fuer Gaeste) this.categoryPanel.classList.toggle('hidden', !!this.guest || mapInfo.kind !== 'tiles'); // Reset view and render this.view.resetView(); this.buildCategoryUI(); this.render(); if (this.onMapChanged) this.onMapChanged(slug); } catch (e) { console.error('Failed to load map:', e); } finally { this.showLoading(false); } } /** * Build the category filter UI */ buildCategoryUI() { const categories = this.mapData.categories; if (Object.keys(categories).length === 0) { this.categoriesList.innerHTML = '
Für diese Karte gibt es noch keine Markierungen.
'; return; } // Group by group_name const groups = {}; Object.entries(categories).forEach(([key, cat]) => { if (!groups[cat.group]) { groups[cat.group] = []; } groups[cat.group].push({ key, ...cat }); }); let html = ''; for (const [groupName, cats] of Object.entries(groups)) { html += `