diff --git a/src/presence.js b/src/presence.js new file mode 100644 index 0000000..af6d33e --- /dev/null +++ b/src/presence.js @@ -0,0 +1,20 @@ +// Seed #6: presence. Other players show up as ghost cats (max 40). Positions and 4 fixed emotes only, never free text. +export function connect(w) { + let ws = null, tick = null, backoff = 1000; + const open = () => { + try { ws = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/arcade/ws`); } catch { return retry(); } + ws.onopen = () => { backoff = 1000; tick = setInterval(send, 200); }; + ws.onmessage = (m) => { + let d; try { d = JSON.parse(m.data); } catch { return; } + if (d.t === "snap" && Array.isArray(d.p)) w.ghosts = d.p.slice(0, 40).map(([id, room, x, y, f, a, e]) => ({ id, room, x, y, f, a, e })); + }; + ws.onclose = () => { clearInterval(tick); w.ghosts = []; retry(); }; + ws.onerror = () => ws.close(); + }; + const retry = () => { setTimeout(open, backoff); backoff = Math.min(30000, backoff * 2); }; + const send = () => { + const p = w.player; if (!p || ws.readyState !== 1) return; + ws.send(JSON.stringify({ t: "pos", room: w.room, x: Math.round(p.x), y: Math.round(p.y), f: p.f, a: p.walk > 0 ? 1 : 0, e: p.emote })); + }; + open(); +}