feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
/* Robot Clash server: REST (accounts, online save + shop actions, campaign cloud save, ranking, live settings),
|
|
|
|
|
WebSocket /ws (lobby + server-run live fights) and the static game from ../game - all on one origin. */
|
2026-11-18 15:18:38 +01:00
|
|
|
'use strict';
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const http = require('http');
|
2026-11-18 15:18:38 +01:00
|
|
|
const path = require('path');
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const express = require('express');
|
2026-11-18 15:18:38 +01:00
|
|
|
const auth = require('./auth');
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const economy = require('./economy');
|
|
|
|
|
const { Matches, KV, CampaignSaves } = require('./db');
|
|
|
|
|
const { rateLimit } = require('./ratelimit');
|
|
|
|
|
const { createRunner } = require('./match/runner');
|
|
|
|
|
const { createLobby } = require('./lobby');
|
|
|
|
|
const hub = require('./ws/hub');
|
2026-11-19 01:40:31 +01:00
|
|
|
const pkg = require('./package.json');
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
|
2026-11-19 01:40:31 +01:00
|
|
|
const STARTED = Date.now();
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const runner = createRunner();
|
|
|
|
|
const lobby = createLobby({ runner, economy, matches: Matches });
|
2026-11-19 01:40:31 +01:00
|
|
|
|
2026-11-18 15:18:38 +01:00
|
|
|
const app = express();
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
app.set('trust proxy', 'loopback'); // behind Caddy: real client IP for the rate limiter
|
|
|
|
|
app.use(express.json({ limit: '1mb' }));
|
|
|
|
|
app.use(auth.middleware);
|
|
|
|
|
|
|
|
|
|
const run = fn => (req, res) => {
|
|
|
|
|
try { res.json(fn(req)); } catch (e) { if (!e.status) console.error(e); res.status(e.status || 500).json({ error: e.code || 'ERROR' }); }
|
|
|
|
|
};
|
|
|
|
|
const need = (req, admin) => {
|
|
|
|
|
if (!req.user) { const e = new Error('UNAUTH'); e.status = 401; e.code = 'UNAUTH'; throw e; }
|
|
|
|
|
if (admin && !req.user.admin) { const e = new Error('DENIED'); e.status = 403; e.code = 'DENIED'; throw e; }
|
|
|
|
|
return req.user;
|
|
|
|
|
};
|
|
|
|
|
const body = req => (req.body && typeof req.body === 'object' && !Array.isArray(req.body) ? req.body : {});
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- health ------------------------------------------------------------------------------------------
|
2026-11-19 01:40:31 +01:00
|
|
|
app.get('/healthz', (_req, res) => res.json({
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
ok: true, version: pkg.version, commit: process.env.GIT_COMMIT || null,
|
|
|
|
|
uptime: Math.round((Date.now() - STARTED) / 1000), players: lobby.players.size, matches: runner.rooms.size,
|
2026-11-19 01:40:31 +01:00
|
|
|
}));
|
|
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- accounts ----------------------------------------------------------------------------------------
|
|
|
|
|
const authLimit = rateLimit({ name: 'auth', windowMs: 60000, max: process.env.NODE_ENV === 'test' ? 1000 : 20 });
|
|
|
|
|
app.post('/api/auth/register', authLimit, run(req => auth.register(body(req).name, body(req).pass, body(req).email)));
|
|
|
|
|
app.post('/api/auth/login', authLimit, run(req => auth.login(body(req).login || body(req).email, body(req).pass)));
|
|
|
|
|
app.post('/api/auth/guest', authLimit, run(req => auth.guest(body(req).name)));
|
|
|
|
|
app.post('/api/auth/refresh', run(req => auth.refresh(body(req).refreshToken)));
|
|
|
|
|
app.post('/api/auth/logout', run(req => { auth.logout(body(req).refreshToken); return { ok: true }; }));
|
|
|
|
|
app.get('/api/me', (req, res) => res.json(req.user ? { uid: req.uid, name: req.user.name, guest: req.user.guest, admin: req.user.admin } : { uid: null, admin: false }));
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- online save: read, shop/workshop actions (server-authoritative), cosmetics ------------------------
|
|
|
|
|
app.get('/api/online', run(req => economy.load(need(req).uid)));
|
|
|
|
|
app.post('/api/online/action', run(req => {
|
|
|
|
|
const u = need(req), r = economy.action(u, body(req));
|
|
|
|
|
if (r.ok) lobby.refresh(u.uid);
|
|
|
|
|
return r;
|
|
|
|
|
}));
|
|
|
|
|
app.put('/api/online/save', run(req => { const u = need(req), s = economy.putCosmetics(u.uid, body(req)); lobby.refresh(u.uid); return s; }));
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- campaign cloud save (registered accounts; offline game, client-trusted) --------------------------
|
|
|
|
|
app.get('/api/campaign', run(req => { const u = need(req); return CampaignSaves.get(u.uid); }));
|
|
|
|
|
app.put('/api/campaign', run(req => { const u = need(req); if (u.guest) return { ok: false }; CampaignSaves.put(u.uid, body(req)); return { ok: true }; }));
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- ranking + live settings (public read) -----------------------------------------------------------
|
|
|
|
|
app.get('/api/ranks', run(req => economy.ranks(+req.query.n || 20)));
|
|
|
|
|
app.get('/api/live', run(() => KV.get('live')));
|
|
|
|
|
app.put('/api/admin/live', run(req => { need(req, true); KV.put('live', body(req)); return { ok: true }; }));
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
// --- static game (same origin: no CORS, no mixed content) --------------------------------------------
|
2026-11-19 00:55:39 +01:00
|
|
|
app.use(express.static(path.join(__dirname, '..', 'game'), {
|
|
|
|
|
etag: true,
|
|
|
|
|
setHeaders(res, p) {
|
|
|
|
|
if (/\.(png|jpe?g|webp|gif|svg|woff2?|ttf|otf|mp3|ogg|wav|m4a)$/i.test(p)) res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
|
|
|
|
else res.setHeader('Cache-Control', 'no-cache'); // code/html: revalidate so deploys show up
|
|
|
|
|
},
|
|
|
|
|
}));
|
2026-11-18 15:18:38 +01:00
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const server = http.createServer(app);
|
|
|
|
|
const wss = hub.attach(server, { lobby });
|
|
|
|
|
|
|
|
|
|
// started directly (node app.js) -> listen; tests call listen(0) themselves
|
2026-11-19 01:40:31 +01:00
|
|
|
function listen(port = process.env.PORT || 8080) {
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
return new Promise(resolve => server.listen(port, () => resolve(server)));
|
2026-11-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
|
if (require.main === module) listen().then(srv => console.log('Robot Clash server on :' + srv.address().port));
|
|
|
|
|
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
module.exports = { app, server, wss, listen, runner, lobby };
|