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; }