mirror of
https://git.suyu.dev/suyu/website.git
synced 2026-01-05 22:18:05 +01:00
1 room per person
This commit is contained in:
parent
02af273266
commit
31d119e85e
2 changed files with 22 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ import { WebSocketServer } from "ws";
|
||||||
import { userRepo } from "$lib/server/repo";
|
import { userRepo } from "$lib/server/repo";
|
||||||
import { globalData } from "$lib/server/other";
|
import { globalData } from "$lib/server/other";
|
||||||
import type { Assets } from "./routes/api/webhooks/release/+server";
|
import type { Assets } from "./routes/api/webhooks/release/+server";
|
||||||
|
import { RoomManager } from "$lib/server/class/Room";
|
||||||
|
|
||||||
let server: WebSocketServer;
|
let server: WebSocketServer;
|
||||||
|
|
||||||
|
|
@ -36,6 +37,7 @@ async function fetchGames() {
|
||||||
async function setupGames() {
|
async function setupGames() {
|
||||||
await fetchGames();
|
await fetchGames();
|
||||||
setInterval(fetchGames, 1000 * 60 * 60 * 12);
|
setInterval(fetchGames, 1000 * 60 * 60 * 12);
|
||||||
|
setInterval(RoomManager.checkTimeouts, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
const runAllTheInitFunctions = async () => {
|
const runAllTheInitFunctions = async () => {
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,32 @@ import { v4 } from "uuid";
|
||||||
|
|
||||||
export class RoomManager {
|
export class RoomManager {
|
||||||
private static rooms: Room[] = [];
|
private static rooms: Room[] = [];
|
||||||
|
static roomTimeout: Record<string, number> = {}; // room id, last heard from
|
||||||
static createRoom(room: IRoomConfig) {
|
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);
|
const newRoom = new Room(room);
|
||||||
|
this.roomTimeout[newRoom.roomInfo.id] = Date.now();
|
||||||
this.rooms.push(newRoom);
|
this.rooms.push(newRoom);
|
||||||
return 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() {
|
static getRooms() {
|
||||||
return this.rooms;
|
return this.rooms;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue