1 room per person

This commit is contained in:
not-nullptr 2024-03-21 15:26:01 +00:00
parent 02af273266
commit 31d119e85e
2 changed files with 22 additions and 0 deletions

View file

@ -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 () => {

View file

@ -5,12 +5,32 @@ import { v4 } from "uuid";
export class RoomManager {
private static rooms: Room[] = [];
static roomTimeout: Record<string, number> = {}; // 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;
}