Files
Cs2-Mapveto/scripts/fetch-map-images.mjs
T
mregosar 7be0d85ab5 Map-Bilder und Seitenwahl mit Emblemen
Map-Bilder aus ghostcap-gaming/cs2-map-images lokal als WebP gespeichert
(npm run images lädt und verkleinert sie). Bilder in Veto-Kacheln,
Pool-Vorschau, Serie und Seitenwahl; CT/T-Embleme aus client/public/sides
mit Ersatzsymbol, falls die Dateien fehlen.
2026-10-25 17:23:55 +01:00

57 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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/<map>.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` : ''}.`);