74 lines
4.0 KiB
JavaScript
74 lines
4.0 KiB
JavaScript
/* Robot Clash - network overlay (F3 or ?net): ping graph of the last 10 s and a plain-language reading of
|
||
where lag comes from (your line, the opponent's line, the server). Measurements: netstats.js.
|
||
|
||
const ov = RC.NetOverlay({ stats, net, t, mount }) mount = #stage; pings the server once a second
|
||
ov.toggle() / ov.visible */
|
||
(function (root) {
|
||
'use strict';
|
||
const RC = (root.RC = root.RC || {});
|
||
const SECS = 10, MAX_MS = 300, W = 300, H = 84;
|
||
const COL = { ping: '#ff7a1a', opp: '#4aa8ff', gap: '#6dff9e', input: '#e8ebf2' };
|
||
const LEVEL = { ok: '#6dff9e', warn: '#ffc940', bad: '#ff5a4a' };
|
||
|
||
RC.NetOverlay = function (opt) {
|
||
const { stats, net, t } = opt;
|
||
const el = document.createElement('div');
|
||
el.id = 'netov';
|
||
el.className = 'hide';
|
||
el.innerHTML = `<div class="nvh"></div><canvas width="${W}" height="${H}"></canvas><div class="nvleg"></div><div class="nvrows"></div><div class="nvv"></div>`;
|
||
opt.mount.appendChild(el);
|
||
const cv = el.querySelector('canvas'), cx = cv.getContext('2d');
|
||
let visible = false, drawT = null;
|
||
|
||
// my ping: one tiny message per second while connected (also when hidden, so the history is there)
|
||
setInterval(() => { if (net.enabled && net.socket.connected) net.socket.send({ t: 'ping', t0: performance.now() }); }, 1000);
|
||
net.socket.on('pong', m => { if (typeof m.t0 === 'number') { const now = performance.now(); stats.pong(now - m.t0, now); } });
|
||
|
||
function graph(now) {
|
||
cx.clearRect(0, 0, W, H);
|
||
cx.fillStyle = 'rgba(0,0,0,.35)'; cx.fillRect(0, 0, W, H);
|
||
cx.strokeStyle = 'rgba(255,255,255,.12)'; cx.lineWidth = 1;
|
||
for (const ms of [50, 100, 200]) { const y = H - ms / MAX_MS * H; cx.beginPath(); cx.moveTo(0, y); cx.lineTo(W, y); cx.stroke(); }
|
||
cx.fillStyle = 'rgba(255,255,255,.35)'; cx.font = '9px monospace';
|
||
for (const ms of [50, 100, 200]) cx.fillText(ms + 'ms', 2, H - ms / MAX_MS * H - 2);
|
||
const pts = stats.series(now, SECS);
|
||
for (const k of ['gap', 'input', 'opp', 'ping']) {
|
||
cx.strokeStyle = COL[k]; cx.lineWidth = k === 'ping' ? 2 : 1.2; cx.beginPath();
|
||
let first = true;
|
||
for (const p of pts) {
|
||
if (p[k] === undefined) continue;
|
||
const x = W - (now - p.t) / (SECS * 1000) * W, y = H - Math.min(MAX_MS, p[k]) / MAX_MS * H;
|
||
if (first) { cx.moveTo(x, y); first = false; } else cx.lineTo(x, y);
|
||
}
|
||
cx.stroke();
|
||
}
|
||
}
|
||
|
||
function draw() {
|
||
const now = performance.now(), s = stats.summary(now);
|
||
el.querySelector('.nvh').textContent = t('net_title');
|
||
el.querySelector('.nvleg').innerHTML = ['ping', 'opp', 'gap', 'input'].map(k => `<span><i style="background:${COL[k]}"></i>${t('net_l_' + k)}</span>`).join('');
|
||
const row = (label, val) => `<span>${label}</span><b>${val}</b>`;
|
||
el.querySelector('.nvrows').innerHTML =
|
||
row(t('net_ping'), s.ping ? `${s.ping} ms <small>(max ${s.pingMax})</small>` : '–')
|
||
+ (s.fight ? row(t('net_ping_srv'), `${s.serverPing} ms`)
|
||
+ row(t('net_ping_opp'), `${s.oppPing} ms`)
|
||
+ row(t('net_snaps'), `${s.gap} ms ±${s.jitter}${s.late ? ` · ${s.late} ${t('net_late')}` : ''}`)
|
||
+ row(t('net_input'), s.input ? `${s.input} ms` : '–')
|
||
+ row(t('net_server'), `${s.tickMs} ms · ${s.kbps} kbit/s`) : '');
|
||
el.querySelector('.nvv').innerHTML = RC.NetStats.verdict(s)
|
||
.map(v => `<div style="color:${LEVEL[v.level]}">${v.level === 'ok' ? '✔' : v.level === 'warn' ? '⚠' : '✖'} ${t(v.key, { n: v.n })}</div>`).join('');
|
||
graph(now);
|
||
}
|
||
|
||
function toggle(on) {
|
||
visible = on === undefined ? !visible : !!on;
|
||
el.classList.toggle('hide', !visible);
|
||
clearInterval(drawT);
|
||
if (visible) { draw(); drawT = setInterval(draw, 250); }
|
||
}
|
||
addEventListener('keydown', e => { if (e.key === 'F3') { e.preventDefault(); e.stopPropagation(); toggle(); } }, true);
|
||
return { toggle, get visible() { return visible; } };
|
||
};
|
||
})(typeof window !== 'undefined' ? window : globalThis);
|