Refresh waiting room cleanup on activity
Some checks failed
CI / Deploy / test (push) Successful in 1m15s
CI / Deploy / deploy (push) Has been cancelled

- Extend waiting rooms while players or spectators are active
- Make cleanup time configurable for tests and defaults
- Tweak lobby layout for smaller screens
This commit is contained in:
syntaxbullet
2026-04-10 12:00:59 +02:00
parent 2fb8d559a6
commit 9e85ba1fa4
3 changed files with 85 additions and 31 deletions

View File

@@ -154,4 +154,34 @@ describe("RoomManager", () => {
expect(empty.length).toBe(0);
});
});
describe("waiting room cleanup", () => {
it("should remove waiting rooms after the configured timeout", async () => {
const shortLivedManager = new RoomManager({ WAITING_CLEANUP_MS: 20 });
const create = shortLivedManager.createRoom("stub", "player1");
if (!create.ok) throw new Error("Failed to create room");
await new Promise(resolve => setTimeout(resolve, 35));
expect(shortLivedManager.getRoom(create.roomId)).toBeUndefined();
});
it("should refresh the waiting room timeout when the room is active", async () => {
const shortLivedManager = new RoomManager({ WAITING_CLEANUP_MS: 25 });
const create = shortLivedManager.createRoom("stub", "player1");
if (!create.ok) throw new Error("Failed to create room");
await new Promise(resolve => setTimeout(resolve, 15));
const spectatorJoin = shortLivedManager.joinRoom(create.roomId, "spectator1", "spectator");
expect(spectatorJoin.ok).toBe(true);
expect(shortLivedManager.getRoom(create.roomId)).toBeDefined();
await new Promise(resolve => setTimeout(resolve, 15));
expect(shortLivedManager.getRoom(create.roomId)).toBeDefined();
await new Promise(resolve => setTimeout(resolve, 20));
expect(shortLivedManager.getRoom(create.roomId)).toBeUndefined();
});
});
});

View File

@@ -3,12 +3,14 @@ import { gameRegistry } from "@shared/games/registry";
import type { Room, RoomSummary } from "./types";
import type { RoundSettlement } from "@shared/games/types";
const ROOM_CONFIG = {
WAITING_CLEANUP_MS: 60_000,
const DEFAULT_ROOM_CONFIG = {
WAITING_CLEANUP_MS: 15 * 60_000,
FINISHED_CLEANUP_MS: 60_000,
PLAYING_MAX_MS: 30 * 60_000, // 30 minutes — safety net for stuck games
} as const;
type RoomManagerConfig = typeof DEFAULT_ROOM_CONFIG;
type ActionResult =
| { ok: true; state: unknown; gameOver: { winner: string | null; reason: string } | null; roundSettlements?: Record<string, RoundSettlement> }
| { ok: false; error: string };
@@ -34,8 +36,13 @@ type RoomEvents = {
export class RoomManager {
private rooms = new Map<string, Room>();
private cleanupTimers = new Map<string, Timer>();
private readonly config: RoomManagerConfig;
readonly emitter = mitt<RoomEvents>();
constructor(config: Partial<RoomManagerConfig> = {}) {
this.config = { ...DEFAULT_ROOM_CONFIG, ...config };
}
createRoom(gameSlug: string, hostId: string, options?: Record<string, unknown>): CreateResult {
const plugin = gameRegistry.get(gameSlug);
if (!plugin) return { ok: false, error: `Unknown game type: ${gameSlug}` };
@@ -56,7 +63,7 @@ export class RoomManager {
};
this.rooms.set(id, room);
this.scheduleCleanup(id, ROOM_CONFIG.WAITING_CLEANUP_MS);
this.refreshWaitingCleanup(id, room);
this.emitter.emit("room:created", { roomId: id, gameSlug, hostId });
this.emitter.emit("room:list:changed");
@@ -71,11 +78,13 @@ export class RoomManager {
// Reconnecting player: must be checked before the in-progress spectator guard.
if (preferAs !== "spectator" && room.players.includes(playerId)) {
room.spectators.delete(playerId);
this.refreshWaitingCleanup(roomId, room);
return { ok: true, joinedAs: "player", started: room.status === "playing" };
}
if (preferAs === "spectator" || room.status !== "waiting") {
room.spectators.add(playerId);
this.refreshWaitingCleanup(roomId, room);
return { ok: true, joinedAs: "spectator", started: room.status === "playing" };
}
@@ -93,6 +102,7 @@ export class RoomManager {
if (room.players.length >= plugin.maxPlayers && !plugin.manualStart) {
// Defer start when bets are involved — GameServer handles async deduction first
if (room.betAmount > 0) {
this.refreshWaitingCleanup(roomId, room);
this.emitter.emit("room:list:changed");
return { ok: true, joinedAs: "player", started: false, readyToStart: true };
}
@@ -100,6 +110,7 @@ export class RoomManager {
return { ok: true, joinedAs: "player", started: true };
}
this.refreshWaitingCleanup(roomId, room);
this.emitter.emit("room:list:changed");
return { ok: true, joinedAs: "player", started: false };
}
@@ -128,7 +139,7 @@ export class RoomManager {
const gameOver = plugin.isGameOver?.(room.state) ?? null;
if (gameOver) {
room.status = "finished";
this.scheduleCleanup(roomId, ROOM_CONFIG.FINISHED_CLEANUP_MS);
this.scheduleCleanup(roomId, this.config.FINISHED_CLEANUP_MS);
}
const spectatorView = plugin.getSpectatorView(room.state);
@@ -168,7 +179,7 @@ export class RoomManager {
const gameOver = plugin.isGameOver?.(room.state) ?? null;
if (gameOver) {
room.status = "finished";
this.scheduleCleanup(roomId, ROOM_CONFIG.FINISHED_CLEANUP_MS);
this.scheduleCleanup(roomId, this.config.FINISHED_CLEANUP_MS);
this.emitter.emit("game:ended", { roomId, winner: gameOver.winner, reason: gameOver.reason, payouts: gameOver.payouts });
}
}
@@ -180,6 +191,8 @@ export class RoomManager {
}
}
this.refreshWaitingCleanup(roomId, room);
this.emitter.emit("player:left", { roomId, playerId });
this.emitter.emit("room:list:changed");
}
@@ -203,6 +216,7 @@ export class RoomManager {
// Defer start when bets are involved
if (room.betAmount > 0) {
this.refreshWaitingCleanup(roomId, room);
this.emitter.emit("room:list:changed");
return { ok: true, readyToStart: true };
}
@@ -220,7 +234,7 @@ export class RoomManager {
const plugin = gameRegistry.get(room.gameSlug)!;
room.state = plugin.createInitialState(room.players, room.options);
room.status = "playing";
this.scheduleCleanup(roomId, ROOM_CONFIG.PLAYING_MAX_MS);
this.scheduleCleanup(roomId, this.config.PLAYING_MAX_MS);
const spectatorView = plugin.getSpectatorView(room.state);
const playerViews = new Map<string, unknown>();
@@ -238,6 +252,11 @@ export class RoomManager {
if (!room || room.status !== "waiting") return;
const idx = room.players.indexOf(playerId);
if (idx !== -1) room.players.splice(idx, 1);
if (room.players.length === 0) {
this.deleteRoom(roomId);
return;
}
this.refreshWaitingCleanup(roomId, room);
this.emitter.emit("room:list:changed");
}
@@ -285,6 +304,11 @@ export class RoomManager {
this.cleanupTimers.set(roomId, timer);
}
private refreshWaitingCleanup(roomId: string, room: Room): void {
if (room.status !== "waiting") return;
this.scheduleCleanup(roomId, this.config.WAITING_CLEANUP_MS);
}
private clearCleanup(roomId: string): void {
const existing = this.cleanupTimers.get(roomId);
if (existing) {

View File

@@ -156,7 +156,7 @@ function CreateRoomDialog({
>
{selectedGame ? (
<div className="space-y-6">
<div className="flex items-start justify-between gap-4">
<div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div className="min-w-0">
<button
onClick={onBack}
@@ -165,9 +165,9 @@ function CreateRoomDialog({
<ChevronLeft className="h-3.5 w-3.5" />
Back to game selection
</button>
<div className="flex items-center gap-3">
<div className="flex items-start gap-3">
<span className="text-3xl">{selectedGame.icon}</span>
<div>
<div className="min-w-0">
<h2 className="font-display text-2xl font-semibold text-foreground">{selectedGame.name}</h2>
<p className="mt-1 text-sm text-text-tertiary">{selectedGame.description}</p>
</div>
@@ -303,7 +303,7 @@ function CreateRoomDialog({
<div className="space-y-4">
<section className={cn("rounded-3xl border p-4", gameSurfaceClass("blackjack"))}>
<div className="text-[11px] uppercase tracking-[0.2em] text-text-disabled">Table Format</div>
<div className="mt-3 grid gap-3 sm:grid-cols-2">
<div className="mt-3 grid gap-3 md:grid-cols-2">
<div className="rounded-2xl border border-white/10 bg-black/12 p-4">
<div className="text-sm font-semibold text-foreground">Manual Start</div>
<div className="mt-1 text-sm text-text-tertiary">
@@ -381,7 +381,7 @@ function CreateRoomDialog({
</div>
) : (
<div className="space-y-6">
<div className="flex items-start justify-between gap-4">
<div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div>
<div className="inline-flex items-center gap-2 rounded-full border border-primary/20 bg-primary/10 px-3 py-1 text-xs font-semibold uppercase tracking-[0.22em] text-primary">
<Sparkles className="h-3.5 w-3.5" />
@@ -536,11 +536,11 @@ export function GameLobby() {
Browse open rooms, check seats and stakes at a glance, and create a table with the settings you need.
</p>
<div className="mt-5 flex flex-wrap items-center gap-3">
<div className="mt-5 flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-center">
<button
onClick={() => setShowCreate(true)}
disabled={!connected}
className="inline-flex items-center gap-2 rounded-2xl bg-primary px-5 py-3 text-sm font-semibold text-on-primary transition hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
className="inline-flex items-center justify-center gap-2 rounded-2xl bg-primary px-5 py-3 text-sm font-semibold text-on-primary transition hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
>
Create Room
<ArrowRight className="h-4 w-4" />
@@ -556,7 +556,7 @@ export function GameLobby() {
</div>
</div>
<div className="mt-6 grid gap-3 sm:grid-cols-3">
<div className="mt-6 grid gap-3 md:grid-cols-3">
<MetricCard label="Active Rooms" value={String(activeRooms.length)} hint="Waiting and live tables." />
<MetricCard label="Players" value={String(totalPlayers)} hint="Currently seated across games." />
<MetricCard label="Spectators" value={String(totalSpectators)} hint="Watching without occupying seats." />
@@ -589,7 +589,7 @@ export function GameLobby() {
<div className="rounded-[28px] border border-white/10 bg-card/70 p-4">
<div className="text-[11px] uppercase tracking-[0.2em] text-text-disabled">Lobby Snapshot</div>
<div className="mt-3 grid gap-3 sm:grid-cols-3 xl:grid-cols-1">
<div className="mt-3 grid gap-3 md:grid-cols-3 xl:grid-cols-1">
<div className="flex items-center gap-3 text-sm text-text-secondary">
<Swords className="h-4 w-4 text-primary" />
<span>{waitingRoomCount} room{waitingRoomCount !== 1 ? "s" : ""} waiting to start</span>
@@ -662,20 +662,20 @@ export function GameLobby() {
)}
>
<div className={cn("pointer-events-none absolute inset-0 bg-gradient-to-br opacity-60", status.accentClassName)} />
<div className="relative flex h-full flex-col">
<div className="flex items-start justify-between gap-4">
<div className="min-w-0">
<div className="flex items-center gap-3">
<span className="text-3xl">{plugin?.icon ?? "🎮"}</span>
<div>
<div className="font-display text-2xl font-semibold text-foreground">{room.gameName}</div>
<div className="mt-1 text-sm text-text-secondary">
{plugin?.description ?? "Game room"}
<div className="relative flex h-full flex-col">
<div className="flex items-start justify-between gap-4">
<div className="min-w-0">
<div className="flex items-center gap-3">
<span className="text-3xl">{plugin?.icon ?? "🎮"}</span>
<div className="min-w-0">
<div className="font-display text-2xl font-semibold text-foreground">{room.gameName}</div>
<div className="mt-1 text-sm text-text-secondary">
{plugin?.description ?? "Game room"}
</div>
</div>
</div>
</div>
</div>
<span className={cn("rounded-full border px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.18em]", status.chipClassName)}>
<span className={cn("shrink-0 rounded-full border px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.18em]", status.chipClassName)}>
{status.label}
</span>
</div>
@@ -688,7 +688,7 @@ export function GameLobby() {
))}
</div>
<div className="mt-5 grid gap-3 sm:grid-cols-3">
<div className="mt-5 grid gap-3 md:grid-cols-3">
<div className="rounded-2xl border border-white/10 bg-black/10 px-4 py-3">
<div className="flex items-center gap-2 text-[11px] uppercase tracking-[0.18em] text-text-disabled">
<Users className="h-3.5 w-3.5" />
@@ -714,8 +714,8 @@ export function GameLobby() {
</div>
</div>
<div className="mt-5 flex items-center justify-between gap-4">
<div className="text-xs text-text-tertiary">
<div className="mt-5 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div className="break-all text-xs text-text-tertiary">
Room ID {room.id.slice(0, 8).toUpperCase()}
</div>
<button
@@ -723,7 +723,7 @@ export function GameLobby() {
state: { preferAs: room.status === "waiting" ? "player" : "spectator" },
})}
className={cn(
"inline-flex items-center gap-2 rounded-2xl px-4 py-2.5 text-sm font-semibold transition-colors",
"inline-flex w-full items-center justify-center gap-2 rounded-2xl px-4 py-2.5 text-sm font-semibold transition-colors sm:w-auto",
room.status === "waiting"
? "bg-primary text-on-primary hover:opacity-90"
: "border border-white/10 bg-white/5 text-foreground hover:bg-white/10",
@@ -765,7 +765,7 @@ export function GameLobby() {
</div>
<div className="mt-2 text-sm text-text-tertiary">{game.tagline}</div>
</div>
<span className="rounded-full border border-white/10 bg-card px-2.5 py-1 text-xs text-text-secondary">
<span className="shrink-0 rounded-full border border-white/10 bg-card px-2.5 py-1 text-xs text-text-secondary">
{roomsForGame}
</span>
</div>