Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

Add the Apache-2.0 licence3efe3da
TypeScript36 lines1.1 KB
import { DurableObject } from 'cloudflare:workers';import { makeCode } from './room-logic.ts'; export interface RoomInfo { code: string; players: number; maxPlayers: number; open: boolean }const STALE_MS = 120000; export class Lobby extends DurableObject {  rooms = new Map<string, RoomInfo & { at: number }>();   prune() {    const now = Date.now();    for (const [c, r] of this.rooms) if (now - r.at > STALE_MS) this.rooms.delete(c);  }   async report(info: RoomInfo) {    if (info.players <= 0) this.rooms.delete(info.code);    else this.rooms.set(info.code, { ...info, at: Date.now() });  }   async quickplay(): Promise<string> {    this.prune();    let best: RoomInfo | null = null;    for (const r of this.rooms.values()) {      if (r.open && r.players < r.maxPlayers && (!best || r.players > best.players)) best = r;    }    return best ? best.code : makeCode((n) => Math.floor(Math.random() * n));  }   async stats() {    this.prune();    let players = 0;    for (const r of this.rooms.values()) players += r.players;    return { rooms: this.rooms.size, players };  }}