feat(net): F3 network overlay - ping graph and where lag comes from (you, opponent, server)
This commit is contained in:
78
game/netstats.js
Normal file
78
game/netstats.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Robot Clash - network measurements for the F3 overlay. No DOM (Node-testable).
|
||||
|
||||
const st = RC.NetStats()
|
||||
st.pong(rttMs, now) my ping, measured by the client (ping/pong over the WebSocket)
|
||||
st.snap(localSnap, bytes, now) a snapshot arrived (already RC.Proto.toLocal'ed: rtt = [me, opponent])
|
||||
st.inputSent(seq, now) input seq left the client
|
||||
st.summary(now) -> { ping, pingMax, serverPing, oppPing, gap, jitter, late, input, tickMs, kbps, snapAge, fight }
|
||||
st.series(now, seconds) -> samples for the graph [{ t, ping, opp, gap, input }]
|
||||
RC.NetStats.verdict(summary) -> [{ key, level: 'ok'|'warn'|'bad', n }] (lang keys nv_*)
|
||||
|
||||
How to read it: ping = you <-> server. oppPing = the opponent <-> server (the server measures it).
|
||||
gap/jitter = how evenly the 20 snapshots/s reach YOU (unsteady = your line). input = key press until the
|
||||
server confirms it (~ping + up to one server step; much more = queueing somewhere). tickMs = server load. */
|
||||
(function (root) {
|
||||
'use strict';
|
||||
const RC = (root.RC = root.RC || {});
|
||||
const KEEP = 20000; // ms of history
|
||||
|
||||
RC.NetStats = function () {
|
||||
const pings = [], snaps = [], inputs = new Map(), delays = [];
|
||||
let lastSnapAt = 0;
|
||||
const trim = (arr, now) => { while (arr.length && now - arr[0].t > KEEP) arr.shift(); };
|
||||
const avg = a => (a.length ? a.reduce((s, x) => s + x, 0) / a.length : 0);
|
||||
const recent = (arr, now, ms) => arr.filter(x => now - x.t <= ms);
|
||||
|
||||
return {
|
||||
pong(rtt, now) { pings.push({ t: now, v: rtt }); trim(pings, now); },
|
||||
inputSent(seq, now) { inputs.set(seq, now); if (inputs.size > 400) inputs.delete(inputs.keys().next().value); },
|
||||
snap(s, bytes, now) {
|
||||
const gap = lastSnapAt ? now - lastSnapAt : 0;
|
||||
lastSnapAt = now;
|
||||
snaps.push({ t: now, gap, bytes: bytes || 0, opp: s.rtt ? s.rtt[1] : 0, srv: s.rtt ? s.rtt[0] : 0, tickMs: s.tickMs || 0 });
|
||||
trim(snaps, now);
|
||||
// server confirmed every input up to s.ack: the oldest unconfirmed send time gives the delay
|
||||
if (s.ack) for (const [seq, at] of inputs) { if (seq > s.ack) break; if (seq === s.ack) delays.push({ t: now, v: now - at }); inputs.delete(seq); }
|
||||
trim(delays, now);
|
||||
},
|
||||
summary(now) {
|
||||
const p = recent(pings, now, 5000).map(x => x.v), sn = recent(snaps, now, 5000), d = recent(delays, now, 5000).map(x => x.v);
|
||||
const gaps = sn.map(x => x.gap).filter(g => g > 0), g = avg(gaps);
|
||||
const jitter = gaps.length ? Math.sqrt(avg(gaps.map(x => (x - g) ** 2))) : 0;
|
||||
const last = sn[sn.length - 1];
|
||||
return {
|
||||
ping: Math.round(avg(p)), pingMax: Math.round(p.length ? Math.max(...p) : 0),
|
||||
serverPing: last ? last.srv : 0, oppPing: last ? last.opp : 0,
|
||||
gap: Math.round(g), jitter: Math.round(jitter), late: gaps.filter(x => x > 110).length,
|
||||
input: Math.round(avg(d)), tickMs: last ? last.tickMs : 0,
|
||||
kbps: Math.round(sn.reduce((s, x) => s + x.bytes, 0) * 8 / 5 / 1000),
|
||||
snapAge: lastSnapAt ? now - lastSnapAt : Infinity, fight: !!(lastSnapAt && now - lastSnapAt < 3000),
|
||||
};
|
||||
},
|
||||
series(now, seconds = 10) {
|
||||
const from = now - seconds * 1000, out = [];
|
||||
for (const x of snaps) if (x.t >= from) out.push({ t: x.t, gap: x.gap, opp: x.opp });
|
||||
for (const x of pings) if (x.t >= from) out.push({ t: x.t, ping: x.v });
|
||||
for (const x of delays) if (x.t >= from) out.push({ t: x.t, input: x.v });
|
||||
return out.sort((a, b) => a.t - b.t);
|
||||
},
|
||||
reset() { snaps.length = 0; delays.length = 0; inputs.clear(); lastSnapAt = 0; },
|
||||
};
|
||||
};
|
||||
|
||||
// plain-language reading of a summary; thresholds tuned for a 20 Hz action game
|
||||
RC.NetStats.verdict = function (s) {
|
||||
const out = [];
|
||||
if (s.ping > 150) out.push({ key: 'nv_me_slow', level: s.ping > 250 ? 'bad' : 'warn', n: s.ping });
|
||||
if (s.fight) {
|
||||
if (s.jitter > 30 || s.late > 3) out.push({ key: 'nv_me_jitter', level: s.jitter > 60 || s.late > 10 ? 'bad' : 'warn', n: s.jitter });
|
||||
if (s.oppPing > 150) out.push({ key: 'nv_opp_slow', level: s.oppPing > 250 ? 'bad' : 'warn', n: s.oppPing });
|
||||
if (s.tickMs > 4) out.push({ key: 'nv_server', level: s.tickMs > 10 ? 'bad' : 'warn', n: s.tickMs });
|
||||
if (s.input > s.ping + 150 && s.input > 200) out.push({ key: 'nv_queue', level: 'warn', n: s.input });
|
||||
} else if (s.snapAge !== Infinity && s.snapAge > 3000 && s.snapAge < 30000) out.push({ key: 'nv_lost', level: 'bad', n: Math.round(s.snapAge / 1000) });
|
||||
if (!out.length) out.push({ key: s.fight ? 'nv_ok' : 'nv_nofight', level: 'ok', n: 0 });
|
||||
return out;
|
||||
};
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
|
||||
})(typeof window !== 'undefined' ? window : globalThis);
|
||||
Reference in New Issue
Block a user