Files
Robot-Clash/server/match/room.js
Benjamin Oezcan 593d69714b feat(net): server measures every player's ping and its own step cost, sent with each snapshot
Also fixes the ping message schema (it checked the field t, which is the message type).
2026-11-19 14:25:46 +01:00

124 lines
5.5 KiB
JavaScript

/* One live fight, run by the server. Pure: no sockets, no timers - the caller feeds inputs and calls tick()
at 60 Hz (see runner.js). The fight itself is the same RC.Combat the browser uses offline.
const room = new Room({ id, seed, players: [{ uid, name, build, paint }, ...], send(seat, msg), onEnd(room) })
room.input(seat, raw) latest input of that seat (validated + clamped)
room.disconnect(seat) robot idles; after GRACE ticks the AI drives it
room.reconnect(seat) player takes over again
room.tick() one 1/60 s step (countdown first), snapshots every SNAP_EVERY ticks
room.ended / room.result { winner (seat or -1), reason } once over */
'use strict';
const RC = require('../../game/robot.js');
require('../../game/combat.js');
require('../../game/ai.js');
require('../../game/protocol.js');
const DT = 1 / 60;
const COUNTDOWN = Math.round(3.5 * 60); // ticks before the fight starts (matches the client countdown)
const SNAP_EVERY = 3; // 20 snapshots per second
const GRACE = 10 * 60; // ticks a disconnected player keeps his robot before the AI takes over
const HARD_CAP = 60 * 60 * 4; // safety net: no room lives longer than 4 minutes of fight
class Room {
constructor(opt) {
this.id = opt.id;
this.seed = opt.seed || 1;
this.players = opt.players; // [{ uid, name, build, paint, flag }]
this.send = opt.send || (() => {});
this.rttOf = opt.rttOf || (() => 0); // seat -> ping in ms (measured by the WebSocket hub)
this.onEnd = opt.onEnd || (() => {});
const ids = RC.ARENAS.map(a => a.id);
this.arena = opt.arena || ids[this.seed % ids.length];
this.s = RC.Combat.create({
robots: this.players.map(p => RC.stats(p.build)), seed: this.seed,
names: this.players.map(p => p.name), arena: this.arena,
});
this.inputs = [RC.Proto.input(null), RC.Proto.input(null)];
this.acks = [0, 0];
this.away = [0, 0]; // ticks since disconnect (0 = connected)
this.ai = [null, null];
this.tickN = 0;
this.ended = false;
this.result = null;
}
input(seat, raw) {
if (this.ended || (seat !== 0 && seat !== 1)) return;
this.inputs[seat] = RC.Proto.input(raw);
if (raw && Number.isInteger(raw.seq)) this.acks[seat] = raw.seq;
}
disconnect(seat) { if (!this.away[seat]) this.away[seat] = 1; }
// player gives up: his robot is knocked out at once
forfeit(seat) {
const s = this.s, r = s.robots[seat];
if (this.ended || !r || s.result) return;
r.hp = 0; r.dead = true;
s.result = { winner: 1 - seat, reason: 'ko' }; s.endIn = 0;
if (this.tickN <= COUNTDOWN) this.tickN = COUNTDOWN; // end now even during the countdown
}
reconnect(seat) { this.away[seat] = 0; this.ai[seat] = null; }
connected(seat) { return !this.away[seat]; }
// what a (re)joining player needs to build the fight locally
startMsg(seat) {
return {
t: 'match.start', matchId: this.id, seat, seed: this.seed, arena: this.arena,
countdown: Math.max(0, (COUNTDOWN - this.tickN) * DT),
robots: this.players.map(p => ({ uid: p.uid, name: p.name, build: p.build, paint: p.paint, flag: p.flag })),
};
}
inputFor(seat) {
if (!this.away[seat]) return this.inputs[seat];
if (this.away[seat] < GRACE) return RC.Proto.input(null); // grace period: robot stands still
if (!this.ai[seat]) this.ai[seat] = RC.AI.create({ style: 'brawler', skill: 0.5, seed: this.seed + seat });
return this.ai[seat].think(this.s, this.s.robots[seat], this.s.robots[1 - seat], DT);
}
tick() {
if (this.ended) return;
this.tickN++;
for (const k of [0, 1]) if (this.away[k]) this.away[k]++;
if (this.tickN <= COUNTDOWN) return;
const s = this.s;
const t0 = process.hrtime.bigint();
RC.Combat.step(s, DT, [this.inputFor(0), this.inputFor(1)]);
const ms = Number(process.hrtime.bigint() - t0) / 1e6;
this.tickMs = this.tickMs ? this.tickMs * 0.9 + ms * 0.1 : ms; // smoothed cost of one step (server load)
if (!this.evBuf) this.evBuf = [];
if (s.events.length) this.evBuf.push(...s.events);
s.events.length = 0;
const over = s.result && s.endIn <= 0;
if (over || this.tickN % SNAP_EVERY === 0) this.publish();
if (over || this.tickN > COUNTDOWN + HARD_CAP) this.finish(s.result || { winner: -1, reason: 'time' });
}
publish() {
const s = this.s;
s.events = this.evBuf.slice(-48); // events since the last snapshot
const snap = RC.Proto.packSnap(s, this.tickN, this.acks);
s.events = []; this.evBuf.length = 0;
const away = [!!this.away[0], !!this.away[1]];
// network diagnostics for the F3 overlay: each player's ping to the server (measured here) + server step cost
const rtt = [0, 1].map(k => Math.round(this.rttOf(k) || 0));
const tickMs = Math.round((this.tickMs || 0) * 100) / 100;
for (const seat of [0, 1]) this.send(seat, Object.assign({ t: 'snap', away, rtt, tickMs, sent: Date.now() }, snap));
}
finish(result) {
if (this.ended) return;
this.ended = true;
this.result = { winner: result.winner, reason: result.reason };
this.onEnd(this);
}
// per-seat fight summary for rewards / the result screen
summary(seat) {
const r = this.s.robots[seat];
return { hp: r.hp, maxHp: r.maxHp, dealt: r.dealt, taken: r.taken, value: r.st.value || 0,
lost: Object.keys(r.lost || {}).filter(k => r.lost[k]) };
}
}
module.exports = { Room, DT, COUNTDOWN, SNAP_EVERY, GRACE };