feat(net): F3 network overlay - ping graph and where lag comes from (you, opponent, server)

This commit is contained in:
Benjamin Oezcan
2026-11-19 14:30:40 +01:00
parent a8d6158384
commit 695cf77afc
8 changed files with 251 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
/* Robot Clash - client side of a server-run live fight. No simulation here: the server steps RC.Combat;
this sends the local input and shows the server's snapshots (already mapped to "robot 0 = me").
const nf = RC.NetFight({ net, seat })
const nf = RC.NetFight({ net, seat, stats }) stats = RC.NetStats() (optional, F3 overlay)
nf.frame(s, input, dt) send input (on change, max 30/s, at least every 0.25 s), ease the local state toward
the newest snapshot, return the events of snapshots not seen yet
nf.oppAway true while the opponent is disconnected (server tells us)
@@ -17,14 +17,18 @@
RC.NetFight = (opt) => {
const sock = opt.net.socket, seat = opt.seat;
let latest = null, seenTick = -1, seq = 0, sendT = 0, keepT = 0, prev = null, lastSnap = Date.now();
const off = sock.on('snap', m => { latest = RC.Proto.toLocal(seat, m); lastSnap = Date.now(); });
const stats = opt.stats || null; // RC.NetStats for the F3 overlay (optional)
const off = sock.on('snap', m => {
latest = RC.Proto.toLocal(seat, m); lastSnap = Date.now();
if (stats) stats.snap(latest, m._bytes, performance.now());
});
const changed = i => !prev || i.throttle !== prev.throttle || i.turn !== prev.turn || i.fire !== prev.fire || i.fire2 !== prev.fire2 || i.fire3 !== prev.fire3;
function sendInput(inp, dt) {
sendT -= dt; keepT -= dt;
if (sendT > 0 || (!changed(inp) && keepT > 0)) return;
const i = RC.Proto.input(inp);
if (sock.send(Object.assign({ t: 'input', seq: ++seq }, i))) { prev = i; sendT = SEND_MIN; keepT = SEND_KEEP; }
if (sock.send(Object.assign({ t: 'input', seq: ++seq }, i))) { prev = i; sendT = SEND_MIN; keepT = SEND_KEEP; if (stats) stats.inputSent(seq, performance.now()); }
}
function applyRobot(r, d, k) {