Files
Robot-Clash/build-web.mjs
Michi 7a69651b6d Online-Lobby, Arenen, Lackiererei, ComfyGen-Grafik, Verschleierung
Grosse Erweiterung ueber die erste spielbare Version hinaus:

- Online (Firebase ueber REST, kein SDK): Konto per E-Mail, globale Lobby
  mit Laender-Raeumen, Herausfordern, Live-PvP (Host rechnet, Gast
  interpoliert, KI-Uebernahme bei Lag). Gast kann anonym beitreten.
  net.js / lobby.js / netfight.js, Regeln in docs/firebase.
- 3 Arenen in verschiedenen Groessen (RC.ARENAS, half 8/12/16), Kamera +
  Licht + Boden skalieren; Liga skaliert die Arena mit, Setting + online
  aus Seed. Harte Rams/Wandtreffer schleudern Roboter auf den Rucken.
- Intro (3D-Kamerafahrt), Titel, Einstellungen (Kamera, Schwierigkeit,
  Steuerung, Grafik, Arena), Lackiererei (painter.js, selbst bemalen).
- Shop mit ComfyGen-Sprites (aus den 3D-Teilen erzeugt), Liga-Gegner sehen
  aus wie ihre Portraits (looks.js), kraeftigere Waffen-Modelle.
- Oekonomie: Startgeld 800, Schadensbonus, Schaukaempfe, Online-Preisgeld.
- Verschleierung: build.ps1 -Obfuscate (terser), tablet-taugliche Touch-UI.
- Tests: tests/sim.test.js, balance.js, online.test.js. Dev-Tools in tools/.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-13 15:45:35 +01:00

75 lines
3.7 KiB
JavaScript

// Builds release/robotclash.html: the whole game in ONE file. Local <script src> and
// <link rel="stylesheet"> are inlined, every file under game/assets is embedded as a data: URI
// (window.KIT_ASSETS, read by Kit.asset). Works by double-click (file://) and on any host
// (itch.io, webspace). From _Tools\Library\web\template.
// node build-web.mjs
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = path.dirname(fileURLToPath(import.meta.url));
const game = path.join(root, 'game');
const MIME = {
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif',
'.svg': 'image/svg+xml', '.mp3': 'audio/mpeg', '.ogg': 'audio/ogg', '.wav': 'audio/wav',
'.ttf': 'font/ttf', '.otf': 'font/otf', '.woff2': 'font/woff2', '.json': 'application/json',
};
function walk(dir) {
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir, { withFileTypes: true }).flatMap(e =>
e.isDirectory() ? walk(path.join(dir, e.name)) : [path.join(dir, e.name)]);
}
const assets = {};
let bytes = 0;
for (const file of walk(path.join(game, 'assets'))) {
const mime = MIME[path.extname(file).toLowerCase()];
if (!mime) continue;
const rel = path.relative(game, file).split(path.sep).join('/');
const buf = fs.readFileSync(file);
bytes += buf.length;
assets[rel] = `data:${mime};base64,${buf.toString('base64')}`;
}
let html = fs.readFileSync(path.join(game, 'index.html'), 'utf8');
const local = p => !/^(https?:)?\/\//.test(p) && !p.startsWith('data:');
// "</script" inside an inlined file would end the tag early
const safe = s => s.replace(/<\/script/gi, '<\\/script');
html = html.replace(/<link\s+rel="stylesheet"\s+href="([^"]+)"\s*\/?>/g, (m, p) =>
local(p) ? `<style>\n${fs.readFileSync(path.join(game, p), 'utf8')}\n</style>` : m);
// optional: run each own script through terser (mangle locals, compress) -> harder to read.
// Enabled with RC_OBFUSCATE=1 (build.ps1 -Obfuscate). vendor/three.min.js is already minified -> skip.
const obf = process.env.RC_OBFUSCATE === '1';
let terser = null;
if (obf) {
try { terser = await import('terser'); }
catch { console.error('RC_OBFUSCATE=1 but terser is missing - run: npm install'); process.exit(1); }
}
const minify = async (code, name) => {
if (!terser || /vendor\/three/.test(name)) return code;
const r = await terser.minify(code, { compress: true, mangle: true, format: { comments: false } });
if (r.error) { console.error(`terser failed on ${name}:`, r.error); process.exit(1); }
return r.code;
};
const scriptJobs = [];
html = html.replace(/<script\s+src="([^"]+)"\s*><\/script>/g, (m, p) => {
if (!local(p)) return m;
const token = `\u0000SCRIPT${scriptJobs.length}\u0000`;
scriptJobs.push(minify(fs.readFileSync(path.join(game, p), 'utf8'), p).then(code => [token, `<script>\n${safe(code)}\n</script>`]));
return token;
});
for (const [token, tag] of await Promise.all(scriptJobs)) html = html.replace(token, () => tag);
// static references in CSS / HTML attributes
html = html.replace(/url\((['"]?)(assets\/[^'")]+)\1\)/g, (m, q, p) => (assets[p] ? `url('${assets[p]}')` : m));
html = html.replace(/(src|href)="(assets\/[^"]+)"/g, (m, a, p) => (assets[p] ? `${a}="${assets[p]}"` : m));
// everything the scripts load at runtime: must come before the first other <script>
html = html.replace('<script>', () => `<script>window.KIT_ASSETS = ${JSON.stringify(assets)};</script>\n<script>`);
const out = path.join(root, 'release', 'robotclash.html');
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, html);
const mb = n => (n / 1024 / 1024).toFixed(1);
console.log(`Fertig: ${out} (${mb(fs.statSync(out).size)} MB, ${Object.keys(assets).length} Assets, ${mb(bytes)} MB roh)`);