Skip to content

gitflarePublicPartly agent-ready40

sketch-rush

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

main
Add the Apache-2.0 licencedbe1e99
JavaScript36 lines1.9 KB
// Pure UI helpers (no DOM) so they can be tested with node:test.export function sortPlayers(ps) {  return Object.keys(ps || {}).map((id) => ({ id, name: ps[id][0], score: ps[id][1] }))    .sort((a, b) => b.score - a.score || (a.name < b.name ? -1 : 1));}export function maskChars(mask) { return Array.from(String(mask || "")).filter((c) => c !== " " || true); }export function revealedCount(mask) { return (String(mask).match(/[^_ ]/g) || []).length; }export function timerFraction(tm, tt) { return !tt ? 0 : Math.max(0, Math.min(1, tm / tt)); }export function podium(list) {  return (list || []).slice().sort((a, b) => b[2] - a[2]).slice(0, 3).map((r, i) => ({ id: r[0], name: r[1], score: r[2], place: i + 1 }));}export function inviteUrl(origin, code) { return origin + "/r/" + code; }export function chatLine(ev, nameOf) {  switch (ev[1]) {    case "chat": return { text: `${senderName(ev[2], nameOf)}: ${ev[3]}`, cls: ev[4] ? "dim" : "", name: senderName(ev[2], nameOf), body: ev[3], locked: !!ev[4] };    case "correct": return { text: `${senderName(ev[2], nameOf)} guessed it! +${ev[3] ?? 0}`, cls: "ok" };    case "close": return { text: "So close!", cls: "close" };    case "reveal": return { text: `The word was "${ev[2]}"`, cls: "ok" };    case "turn": return { text: `${nameOf(ev[2])} is drawing (round ${ev[3]})`, cls: "sys" };    default: return null;  }}export function senderName(v, nameOf) { return typeof v === "string" ? v : (nameOf ? nameOf(v) : "Someone"); }export function avatarColor(name) {  let h = 0; for (const c of String(name)) h = (h * 31 + c.charCodeAt(0)) >>> 0;  return `hsl(${h % 360} 60% 42%)`;}export function turnPoints(before, after) {  const out = [];  for (const id of Object.keys(after || {})) {    const d = after[id][1] - ((before && before[id] && before[id][1]) || 0);    if (d > 0) out.push({ name: after[id][0], pts: d });  }  return out.sort((a, b) => b.pts - a.pts);}