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) {