RoomManager.ts
1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Connection } from "../connection/Connection";
import { RoomListMessage } from "../message/types";
import { Room } from "./Room";
import { RoomData } from "./types";
export class RoomManager {
private static _instance: RoomManager;
private rooms: Map<string, Room>;
constructor() {
RoomManager._instance = this;
this.rooms = new Map<string, Room>();
}
public static instance(): RoomManager {
return RoomManager._instance;
}
public create(name: string, maxConnections: number): Room {
const room = new Room(name, maxConnections);
this.rooms.set(name, room);
return room;
}
public get(uuid: string): Room | undefined {
return this.rooms.get(uuid);
}
public delete(uuid: string): void {
const room = this.get(uuid);
if (room !== undefined) {
room.close();
this.rooms.delete(uuid);
}
}
public sendList(connection: Connection): void {
var roomData: RoomData[] = [];
this.rooms.forEach((room) => {
roomData.push(room.getData());
});
connection.send(new RoomListMessage(roomData));
}
}