// Lädt für alle Maps aus server/config/map-pools.json ein Bild herunter, verkleinert es // und speichert es als WebP unter client/public/maps/.webp. Danach zeigt "image" in // der Konfiguration auf die lokale Datei. // // Aufruf: npm run images fehlende Bilder laden // npm run images -- --force alle Bilder neu laden // // Quelle: https://github.com/ghostcap-gaming/cs2-map-images (Bilder aus dem Spiel, Rechte bei Valve) import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import sharp from 'sharp'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const poolFile = path.join(root, 'server', 'config', 'map-pools.json'); const outDir = path.join(root, 'client', 'public', 'maps'); const SOURCE = 'https://cdn.jsdelivr.net/gh/ghostcap-gaming/cs2-map-images@main/cs2/'; const WIDTH = 960; const force = process.argv.includes('--force'); const config = JSON.parse(fs.readFileSync(poolFile, 'utf8')); fs.mkdirSync(outDir, { recursive: true }); const ids = [...new Set(Object.values(config.pools).flatMap((p) => p.maps.map((m) => m.id)))]; const saved = new Set(); let failed = 0; for (const id of ids) { const target = path.join(outDir, `${id}.webp`); if (!force && fs.existsSync(target)) { console.log(`= ${id} (vorhanden)`); saved.add(id); continue; } try { const res = await fetch(`${SOURCE}${id}.png`); if (!res.ok) throw new Error(`HTTP ${res.status}`); const input = Buffer.from(await res.arrayBuffer()); const info = await sharp(input).resize({ width: WIDTH, withoutEnlargement: true }).webp({ quality: 78 }).toFile(target); console.log(`+ ${id}: ${Math.round(input.length / 1024)} KB -> ${Math.round(info.size / 1024)} KB`); saved.add(id); } catch (err) { failed++; console.warn(`! ${id}: kein Bild (${err.message}) – Map behält das Farbmuster`); } } // Konfiguration auf lokale Dateien umstellen (nur für Maps mit gespeichertem Bild). let text = fs.readFileSync(poolFile, 'utf8'); for (const id of saved) { const entry = new RegExp(`("id": "${id}"[^}]*?"image": ")[^"]*(")`, 'g'); text = text.replace(entry, `$1/maps/${id}.webp$2`); } fs.writeFileSync(poolFile, text); console.log(`Fertig: ${saved.size} Bilder in client/public/maps${failed ? `, ${failed} fehlgeschlagen` : ''}.`);