Skip to content

gitflarePublicPartly agent-ready40

sketch-rush

Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.

Add the Apache-2.0 licencedbe1e99
JavaScript68 lines3.1 KB
const args = process.argv.slice(2);const base = args[0] && !args[0].startsWith("--") ? args[0] : "http://localhost:8787";const opt = (n, d) => { const i = args.indexOf("--" + n); return i >= 0 ? args[i + 1] : d; };const N = Number(opt("bots", 4));const SECONDS = Number(opt("seconds", 10));const code = opt("room", "BOTRMA");const wsUrl = base.replace(/^http/, "ws") + "/ws/" + code;const snapBytes = [], rtts = [];let kmsgs = 0, events = 0, snaps = 0, disconnects = 0, done = false; const pct = (a, p) => (a.length ? [...a].sort((x, y) => x - y)[Math.min(a.length - 1, Math.floor(a.length * p))] : 0);const mean = (a) => (a.length ? a.reduce((x, y) => x + y, 0) / a.length : 0); function bot(i) {  const ws = new WebSocket(wsUrl);  let tickN = 0, seq = 0, me = "", drawing = false, lastAck = 0;  const timers = [];  ws.onopen = () => {    ws.send(JSON.stringify(["j", "bot" + i, null, null]));    timers.push(setInterval(() => ws.send(JSON.stringify(["p", performance.now()])), 1000));    timers.push(setInterval(() => {      const r = Math.random();      if (drawing && r < 0.8) {        if (++tickN % 2) return; // batches every ~66 ms        const pts = [];        for (let k = 0; k < 4; k++) pts.push(Math.floor(Math.random() * 1001), Math.floor(Math.random() * 751));        ws.send(JSON.stringify(["d", ++seq, seq, Math.floor(Math.random() * 8), 1, ...pts]));      } else if (r > 0.97) ws.send(JSON.stringify(["g", ++seq, "guess" + Math.floor(Math.random() * 9)]));    }, 1000 / 30));  };  ws.onmessage = (ev) => {    const raw = String(ev.data);    const m = JSON.parse(raw);    if (m[0] === "w") me = m[1];    else if (m[0] === "s") {      snaps++;      snapBytes.push(raw.length);      if (performance.now() - lastAck >= 100) { lastAck = performance.now(); ws.send(JSON.stringify(["a", m[1]])); }      const d = m[4];      if (d.ph !== undefined || d.dr !== undefined) {        if (d.ph !== undefined) drawing = d.ph === "waiting" || (d.ph === "drawing" && (d.dr ?? "") === me);        if (d.dr !== undefined && d.dr !== me) drawing = d.ph === "waiting";      }      if (d.ch && d.ch.length) ws.send(JSON.stringify(["w", ++seq, 0]));    } else if (m[0] === "k") kmsgs++;    else if (m[0] === "v") events += m.length - 2;    else if (m[0] === "o") rtts.push(performance.now() - m[1]);  };  ws.onclose = () => { if (!done) disconnects++; timers.forEach(clearInterval); };  return ws;} const sockets = Array.from({ length: N }, (_, i) => bot(i));setTimeout(() => {  done = true;  sockets.forEach((s) => { try { s.close(); } catch { /* closed */ } });  console.log(JSON.stringify({    room: code, bots: N, seconds: SECONDS,    snapshotHz: +(snaps / Math.max(1, N) / SECONDS).toFixed(2),    snapshotBytes: { mean: Math.round(mean(snapBytes)), p95: pct(snapBytes, 0.95) },    rttMs: { mean: +mean(rtts).toFixed(1), p95: +pct(rtts, 0.95).toFixed(1) },    strokeMsgs: kmsgs, eventsSeen: events,    bytesPerTickPerBot: Math.round(snapBytes.reduce((x, y) => x + y, 0) / Math.max(1, N) / (SECONDS * 30)),    disconnects,  }));  process.exit(0);}, SECONDS * 1000);