From 31d119e85e754abf2529f4bedde67f5c8d871a9d Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Thu, 21 Mar 2024 15:26:01 +0000 Subject: [PATCH] 1 room per person --- src/hooks.server.ts | 2 ++ src/lib/server/class/Room.ts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index e735216..466f44b 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -6,6 +6,7 @@ import { WebSocketServer } from "ws"; import { userRepo } from "$lib/server/repo"; import { globalData } from "$lib/server/other"; import type { Assets } from "./routes/api/webhooks/release/+server"; +import { RoomManager } from "$lib/server/class/Room"; let server: WebSocketServer; @@ -36,6 +37,7 @@ async function fetchGames() { async function setupGames() { await fetchGames(); setInterval(fetchGames, 1000 * 60 * 60 * 12); + setInterval(RoomManager.checkTimeouts, 1000); } const runAllTheInitFunctions = async () => { diff --git a/src/lib/server/class/Room.ts b/src/lib/server/class/Room.ts index 4117fab..2852066 100644 --- a/src/lib/server/class/Room.ts +++ b/src/lib/server/class/Room.ts @@ -5,12 +5,32 @@ import { v4 } from "uuid"; export class RoomManager { private static rooms: Room[] = []; + static roomTimeout: Record = {}; // room id, last heard from static createRoom(room: IRoomConfig) { + const existingRoom = this.rooms.find((r) => r.host.username === room.host.username); + if (existingRoom) { + existingRoom.delete(); + } const newRoom = new Room(room); + this.roomTimeout[newRoom.roomInfo.id] = Date.now(); this.rooms.push(newRoom); return newRoom; } + static checkTimeouts() { + if (!this.rooms) return; + const now = Date.now(); + this.rooms.forEach((room) => { + if (now - this.roomTimeout[room.roomInfo.id] > 1000 * 60) { + room.delete(); + } + }); + } + + static refreshRoom(id: string) { + this.roomTimeout[id] = Date.now(); + } + static getRooms() { return this.rooms; }