gitflarePublicPartly agent-ready40
sketch-rush
Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
dbe1e99Oct 23, 2026
Add the Apache-2.0 licence
dbe1e99import { sanitizeName } from "../public/shared/sim.js"; export const MAX_PAYLOAD = 2048;export const RATE_LIMIT = 60;export const MAX_STRIKES = 3;export const RECONNECT_MS = 15000;export const EMPTY_CLEAR_MS = 60000;export const CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; export interface RateState { windowStart: number; count: number; strikes: number }export interface Slot { id: string; token: string; connected: boolean; disconnectedAt: number } export function newRate(now: number): RateState { return { windowStart: now, count: 0, strikes: 0 };} /** Records a message; returns whether it is allowed and whether the socket must close. */export function checkMessage(r: RateState, now: number, size: number): { ok: boolean; close: boolean } { if (now - r.windowStart >= 1000) { r.windowStart = now; r.count = 0; } r.count++; let bad = false; if (size > MAX_PAYLOAD) bad = true; else if (r.count === RATE_LIMIT + 1) bad = true; else if (r.count > RATE_LIMIT) return { ok: false, close: r.strikes >= MAX_STRIKES }; if (bad) r.strikes++; return { ok: !bad, close: r.strikes >= MAX_STRIKES };} export function isValidCode(code: string): boolean { return /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(code);} export function randomHex(bytes: number, fill: (a: Uint8Array) => void): string { const a = new Uint8Array(bytes); fill(a); let s = ""; for (const b of a) s += b.toString(16).padStart(2, "0"); return s;} export function randomCode(rand: () => number): string { let s = ""; for (let i = 0; i < 6; i++) s += CODE_ALPHABET[Math.floor(rand() * CODE_ALPHABET.length)]; return s;} export type JoinDecision = | { kind: "reconnect"; slot: Slot } | { kind: "player" } | { kind: "spectator" }; /** Decides how a joiner is admitted given current slots, phase and capacity. */export function decideJoin(slots: Map<string, Slot>, token: string | null, now: number, phase: string, maxPlayers: number): JoinDecision { if (token) { for (const s of slots.values()) { if (s.token === token && (s.connected || now - s.disconnectedAt <= RECONNECT_MS)) return { kind: "reconnect", slot: s }; } } if (phase === "results" || slots.size >= maxPlayers) return { kind: "spectator" }; return { kind: "player" };} export function expiredSlots(slots: Map<string, Slot>, now: number): string[] { const out: string[] = []; for (const s of slots.values()) if (!s.connected && now - s.disconnectedAt > RECONNECT_MS) out.push(s.id); return out;} export { sanitizeName }; export const BUCKET_RATE = 60;export const BUCKET_BURST = 90;export interface Bucket { tokens: number; last: number; strikes: number } export function newBucket(now: number): Bucket { return { tokens: BUCKET_BURST, last: now, strikes: 0 };} /** Token bucket: 60/s sustained, burst 90. Strike on empty bucket or payload > 2 KB. */export function takeToken(b: Bucket, now: number, size: number): { ok: boolean; close: boolean } { b.tokens = Math.min(BUCKET_BURST, b.tokens + Math.max(0, now - b.last) * BUCKET_RATE / 1000); b.last = now; let ok = true; if (size > MAX_PAYLOAD) { b.strikes++; ok = false; } else if (b.tokens < 1) { b.strikes++; ok = false; } else b.tokens -= 1; return { ok, close: b.strikes >= MAX_STRIKES };}