diff --git a/api/src/games/RoomManager.test.ts b/api/src/games/RoomManager.test.ts index cb187ec..20c6fa7 100644 --- a/api/src/games/RoomManager.test.ts +++ b/api/src/games/RoomManager.test.ts @@ -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(); + }); + }); }); diff --git a/api/src/games/RoomManager.ts b/api/src/games/RoomManager.ts index 015e10a..e36fecf 100644 --- a/api/src/games/RoomManager.ts +++ b/api/src/games/RoomManager.ts @@ -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 } | { ok: false; error: string }; @@ -34,8 +36,13 @@ type RoomEvents = { export class RoomManager { private rooms = new Map(); private cleanupTimers = new Map(); + private readonly config: RoomManagerConfig; readonly emitter = mitt(); + constructor(config: Partial = {}) { + this.config = { ...DEFAULT_ROOM_CONFIG, ...config }; + } + createRoom(gameSlug: string, hostId: string, options?: Record): 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(); @@ -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) { diff --git a/panel/src/games/GameLobby.tsx b/panel/src/games/GameLobby.tsx index a63157e..5a0ee1f 100644 --- a/panel/src/games/GameLobby.tsx +++ b/panel/src/games/GameLobby.tsx @@ -156,7 +156,7 @@ function CreateRoomDialog({ > {selectedGame ? (
-
+
-
+
{selectedGame.icon} -
+

{selectedGame.name}

{selectedGame.description}

@@ -303,7 +303,7 @@ function CreateRoomDialog({
Table Format
-
+
Manual Start
@@ -381,7 +381,7 @@ function CreateRoomDialog({
) : (
-
+
@@ -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.

-
+
-
+
@@ -589,7 +589,7 @@ export function GameLobby() {
Lobby Snapshot
-
+
{waitingRoomCount} room{waitingRoomCount !== 1 ? "s" : ""} waiting to start @@ -662,20 +662,20 @@ export function GameLobby() { )} >
-
-
-
-
- {plugin?.icon ?? "🎮"} -
-
{room.gameName}
-
- {plugin?.description ?? "Game room"} +
+
+
+
+ {plugin?.icon ?? "🎮"} +
+
{room.gameName}
+
+ {plugin?.description ?? "Game room"} +
-
- + {status.label}
@@ -688,7 +688,7 @@ export function GameLobby() { ))}
-
+
@@ -714,8 +714,8 @@ export function GameLobby() {
-
-
+
+
Room ID {room.id.slice(0, 8).toUpperCase()}
- + {roomsForGame}