diff --git a/game/game.js b/game/game.js index 2a0ad72..a841f52 100644 --- a/game/game.js +++ b/game/game.js @@ -122,6 +122,10 @@ const FLAGS = ['DE', 'AT', 'CH', 'GB', 'US', 'FR', 'ES', 'IT', 'NL', 'PL', 'TR', 'RU', 'BR', 'JP', 'KR', 'XX']; const flagHtml = c => ``; const lobby = net.enabled ? RC.Lobby({ net }) : null; + // F3 (or ?net): network overlay - ping graph + where lag comes from (netstats.js / netoverlay.js) + const netStats = RC.NetStats(); + const netOverlay = net.enabled ? RC.NetOverlay({ stats: netStats, net, t, mount: $('stage') }) : null; + if (netOverlay && DEV.has('net')) netOverlay.toggle(true); const fx = Kit.fx(); const input = Kit.input({ @@ -764,7 +768,7 @@ const count = online ? Math.max(0, +online.countdown || 0) : 3.5; F = { s, opp, training, exhibition: !!exhibition, online, tour: !!tour, brain: online ? null : RC.AI.create({ style: opp.style, skill, seed: seed + 5 }), count, beat: Math.ceil(count) + 1, acc: [0, 0], accT: 0, banner: null, ended: false, oppAway: false }; - if (online) F.nf = RC.NetFight({ net, seat: online.seat }); // the server runs the fight, we show it + if (online) { netStats.reset(); F.nf = RC.NetFight({ net, seat: online.seat, stats: netStats }); } // the server runs the fight, we show it // dev: ?auto = AI drives the player too, ?ff=30 = fast-forward 30 s (screenshots, balancing) if (DEV.has('auto')) F.auto = RC.AI.create({ style: 'brawler', skill: 0.7, seed: seed + 11 }); if (DEV.has('ff')) { diff --git a/game/index.html b/game/index.html index 1911457..a6672f7 100644 --- a/game/index.html +++ b/game/index.html @@ -34,6 +34,17 @@ /* fixed design size, scaled to the window by Kit.fit */ #app { position: relative; width: 1280px; display: flex; flex-direction: column; } /* HUD lives INSIDE the stage (full-screen game look), above the overlay so menus keep it reachable */ + /* network overlay (F3) */ + #netov { position: absolute; left: 8px; bottom: 12px; z-index: 9; width: 316px; padding: 8px; border-radius: 6px; + background: rgba(10,12,16,.86); border: 1px solid var(--line); box-shadow: var(--hl); font: 12px/1.35 Consolas, monospace; + color: var(--text); pointer-events: none; } + #netov .nvh { font-weight: bold; color: var(--accent); margin-bottom: 4px; } + #netov canvas { display: block; width: 300px; height: 84px; } + #netov .nvleg { display: flex; gap: 8px; flex-wrap: wrap; margin: 3px 0 5px; font-size: 11px; color: var(--dim, #8d95a8); } + #netov .nvleg i { display: inline-block; width: 10px; height: 3px; margin-right: 3px; vertical-align: middle; } + #netov .nvrows { display: grid; grid-template-columns: auto 1fr; gap: 1px 10px; } + #netov .nvrows b { text-align: right; font-weight: normal; } + #netov .nvv { margin-top: 5px; border-top: 1px solid var(--line); padding-top: 4px; } #hud { position: absolute; left: 0; right: 0; top: 0; z-index: 7; display: flex; gap: 8px; align-items: center; height: 44px; padding: 0 12px; pointer-events: none; background: linear-gradient(rgba(6,8,12,.9) 35%, rgba(6,8,12,0)); } @@ -830,6 +841,8 @@ + + diff --git a/game/lang.js b/game/lang.js index dcc7f2d..7d421db 100644 --- a/game/lang.js +++ b/game/lang.js @@ -243,6 +243,15 @@ window.LANG = { e_MISSING_PASSWORD: 'Please enter a password.', e_OPERATION_NOT_ALLOWED: 'Guest login is off - use e-mail.', e_OFFLINE: 'Online is not set up yet.', e_ERROR: 'Something went wrong. Try again.', f_login: 'Name or e-mail', f_email_opt: 'E-mail (optional)', + net_title: 'Network (F3 hides)', net_ping: 'Your ping', net_ping_srv: 'Server measures you', net_ping_opp: 'Opponent ping', + net_snaps: 'Snapshots', net_late: 'late', net_input: 'Key to server', net_server: 'Server step', + net_l_ping: 'your ping', net_l_opp: 'opponent', net_l_gap: 'snapshot gap', net_l_input: 'key delay', + nv_ok: 'Connection looks fine', nv_nofight: 'Not in a fight - only your ping is measured', + nv_me_slow: 'Your ping is high ({n} ms) - your connection or the route to the server', + nv_me_jitter: 'Your connection is unsteady (snapshots arrive irregularly, ±{n} ms)', + nv_opp_slow: 'Opponent has a high ping ({n} ms) - their connection', + nv_server: 'Server is slow ({n} ms per step)', nv_queue: 'Keys take {n} ms - data piles up on the way', + nv_lost: 'No snapshots for {n} s', e_INVALID_LOGIN: 'Wrong name or password.', e_NAME_EXISTS: 'That name is already taken.', e_BAD_NAME: 'The name needs at least 3 letters or digits.', e_RATE_LIMIT: 'Too many tries - please wait a minute.', e_INVALID_ROBOT: 'Your robot is not ready to fight - check weight and power in the workshop.', e_OPPONENT_INVALID: 'The other robot is not ready to fight.', e_BUSY: 'That player is busy right now.', e_NOT_AVAILABLE: 'That player is not available.', @@ -523,6 +532,15 @@ window.LANG = { e_MISSING_PASSWORD: 'Bitte ein Passwort eingeben.', e_OPERATION_NOT_ALLOWED: 'Gast-Login ist aus - nimm E-Mail.', e_OFFLINE: 'Online ist noch nicht eingerichtet.', e_ERROR: 'Etwas ist schiefgelaufen. Nochmal versuchen.', f_login: 'Name oder E-Mail', f_email_opt: 'E-Mail (optional)', + net_title: 'Netzwerk (F3 blendet aus)', net_ping: 'Dein Ping', net_ping_srv: 'Server misst dich', net_ping_opp: 'Gegner-Ping', + net_snaps: 'Snapshots', net_late: 'zu spät', net_input: 'Taste bis Server', net_server: 'Server-Schritt', + net_l_ping: 'dein Ping', net_l_opp: 'Gegner', net_l_gap: 'Snapshot-Abstand', net_l_input: 'Tasten-Verzögerung', + nv_ok: 'Verbindung sieht gut aus', nv_nofight: 'Kein Kampf - nur dein Ping wird gemessen', + nv_me_slow: 'Dein Ping ist hoch ({n} ms) - deine Leitung oder der Weg zum Server', + nv_me_jitter: 'Deine Verbindung schwankt (Snapshots kommen unregelmäßig, ±{n} ms)', + nv_opp_slow: 'Der Gegner hat hohen Ping ({n} ms) - seine Leitung', + nv_server: 'Server ist langsam ({n} ms pro Schritt)', nv_queue: 'Tasten brauchen {n} ms - unterwegs staut es sich', + nv_lost: 'Seit {n} s keine Snapshots', e_INVALID_LOGIN: 'Name oder Passwort falsch.', e_NAME_EXISTS: 'Dieser Name ist schon vergeben.', e_BAD_NAME: 'Der Name braucht mindestens 3 Buchstaben oder Ziffern.', e_RATE_LIMIT: 'Zu viele Versuche - bitte eine Minute warten.', e_INVALID_ROBOT: 'Dein Roboter ist nicht kampfbereit - prüfe Gewicht und Strom in der Werkstatt.', e_OPPONENT_INVALID: 'Der andere Roboter ist nicht kampfbereit.', e_BUSY: 'Dieser Spieler ist gerade beschäftigt.', e_NOT_AVAILABLE: 'Dieser Spieler ist nicht verfügbar.', diff --git a/game/net.js b/game/net.js index 169947f..f676542 100644 --- a/game/net.js +++ b/game/net.js @@ -129,6 +129,7 @@ s.onopen = () => s.send(JSON.stringify({ t: 'hello', v: RC.Proto.VERSION, token: t })); s.onmessage = ev => { let msg; try { msg = JSON.parse(ev.data); } catch (e) { return; } + msg._bytes = ev.data.length; // for the network overlay (bandwidth) if (msg.t === 'welcome') { isOpen = true; backoff = 1000; emit('open', msg); } if (msg.t === 'error' && (msg.code === 'OUTDATED' || msg.code === 'REPLACED')) { want = false; emit(msg.code === 'OUTDATED' ? 'outdated' : 'replaced', msg); } if (msg.t === 'error' && msg.code === 'UNAUTH' && session) session.exp = 0; // refresh before the retry diff --git a/game/netfight.js b/game/netfight.js index 28ccda3..2460aa0 100644 --- a/game/netfight.js +++ b/game/netfight.js @@ -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) { diff --git a/game/netoverlay.js b/game/netoverlay.js new file mode 100644 index 0000000..130a961 --- /dev/null +++ b/game/netoverlay.js @@ -0,0 +1,73 @@ +/* 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 = `
`; + 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 => `${t('net_l_' + k)}`).join(''); + const row = (label, val) => `${label}${val}`; + el.querySelector('.nvrows').innerHTML = + row(t('net_ping'), s.ping ? `${s.ping} ms (max ${s.pingMax})` : '–') + + (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 => `