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).
This commit is contained in:
Benjamin Oezcan
2026-11-19 14:25:46 +01:00
parent 3dc770e34a
commit 593d69714b
6 changed files with 46 additions and 9 deletions

View File

@@ -25,6 +25,7 @@ class Room {
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];
@@ -80,7 +81,10 @@ class Room {
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;
@@ -95,7 +99,10 @@ class Room {
const snap = RC.Proto.packSnap(s, this.tickN, this.acks);
s.events = []; this.evBuf.length = 0;
const away = [!!this.away[0], !!this.away[1]];
for (const seat of [0, 1]) this.send(seat, Object.assign({ t: 'snap', away }, snap));
// 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) {