LuminaWebsite/assets/map/js/services/TileService.js
Maaxxs c5213b5736 Karte: replace the tactical map with the interactive map viewer
The Karte page now embeds a standalone viewer (assets/map/) covering all ten
regions - Verteron, Altgard, Elthen, Morheim, Poeta, Ishalgen and the
Reshanta maps - with markers and category filters from interactivemap.app.
map.php loads it in an iframe because its stylesheet restyles body, button
and * globally and would otherwise break the site layout.

The tile, marker and icon data (~230 MB, ~39,000 files) is kept out of Git
and uploaded by FTP; the README explains where it comes from.

Removed with it: own markers and categories, tactic layers, the in-browser
map builder, the tile/stitch/viewer tools, the bundled Leaflet, the four map
tables in schema.sql and the map.manage / map.tactics permissions. Existing
databases keep their old map tables untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 06:54:17 +02:00

120 lines
No EOL
3.9 KiB
JavaScript

/**
* TileService - Handles tile loading, caching, and manifest management
*/
class TileService {
constructor() {
this.tileCache = {};
this.loadingTiles = new Set();
this.manifests = {}; // slug -> Set of tile keys
}
/**
* Load tile manifest for a map
*/
async loadManifest(slug) {
if (this.manifests[slug]) return;
try {
const response = await fetch(`${CONFIG.TILES_DIR}/${slug}/tile_manifest.json`);
if (!response.ok) throw new Error('Manifest not found');
const data = await response.json();
this.manifests[slug] = new Set(data.tiles || []);
} catch (e) {
console.warn(`Failed to load manifest for ${slug}:`, e);
this.manifests[slug] = null; // No manifest, try all tiles
}
}
/**
* Check if a tile exists in the manifest
*/
hasTile(slug, zoom, x, y) {
const key = `${zoom}_${x}_${y}`;
const manifest = this.manifests[slug];
// If no manifest or null, assume tile might exist
return !manifest || manifest.has(key);
}
/**
* Load a single tile image
*/
async loadTile(slug, zoom, x, y) {
const key = `${slug}_${zoom}_${x}_${y}`;
if (this.tileCache[key]) {
return this.tileCache[key];
}
// Check manifest before attempting to load
if (!this.hasTile(slug, zoom, x, y)) {
return null;
}
// If already loading, wait briefly and check again
if (this.loadingTiles.has(key)) {
await new Promise(r => setTimeout(r, 10));
return this.tileCache[key] || null;
}
this.loadingTiles.add(key);
try {
const img = new Image();
img.src = `${CONFIG.TILES_DIR}/${slug}/${zoom}_${x}_${y}.jpg`;
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = () => reject(new Error('Load failed'));
setTimeout(() => reject(new Error('Timeout')), 5000);
});
this.tileCache[key] = img;
} catch (e) {
// Silently fail for missing tiles
} finally {
this.loadingTiles.delete(key);
}
return this.tileCache[key];
}
/**
* Preload tiles at adjacent zoom levels in background
*/
async preloadAdjacentZoomLevels(slug, currentZoom, viewBounds) {
const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X;
for (let targetZoom of [currentZoom - 1, currentZoom + 1]) {
if (targetZoom < CONFIG.MIN_ZOOM || targetZoom > CONFIG.MAX_ZOOM) continue;
const tilesPerSide = Math.pow(2, targetZoom);
const tileWorldSize = worldWidth / tilesPerSide;
// Calculate which tiles overlap with viewport
const startTX = Math.floor((viewBounds.minX - CONFIG.WORLD_MIN_X) / tileWorldSize);
const endTX = Math.ceil((viewBounds.maxX - CONFIG.WORLD_MIN_X) / tileWorldSize);
const startTY = Math.floor((viewBounds.minY - CONFIG.WORLD_MIN_Y) / tileWorldSize);
const endTY = Math.ceil((viewBounds.maxY - CONFIG.WORLD_MIN_Y) / tileWorldSize);
// Load tiles in background (fire and forget)
for (let tx = startTX; tx <= endTX; tx++) {
for (let ty = startTY; ty <= endTY; ty++) {
if (tx >= 0 && tx < tilesPerSide && ty >= 0 && ty < tilesPerSide) {
this.loadTile(slug, targetZoom, tx, ty);
}
}
}
}
}
/**
* Clear cache for a specific map (when switching maps)
*/
clearCache() {
this.tileCache = {};
this.loadingTiles.clear();
}
}