RoomManager.ts 1.11 KB
import { RoomDescription } from "../../common/dataType";
import { WordDatabase } from "../database/WordDatabase";
import { User } from "../user/User";
import { Room } from "./Room";

export class RoomManager {
  private rooms: Map<string, Room>;
  public wordDatabase: WordDatabase;

  constructor() {
    this.rooms = new Map<string, Room>();
    this.wordDatabase = new WordDatabase("./database.db");
  }

  public create(name: string, maxConnections: number, admin?: User): Room {
    const room = new Room(this, name, admin, maxConnections);
    this.rooms.set(room.uuid, 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 list(): RoomDescription[] {
    var roomData: RoomDescription[] = [];
    this.rooms.forEach((room) => {
      roomData.push(room.getDescription());
    });
    return roomData;
  }

  public clear() {
    this.rooms.forEach((room) => {
      room.close();
    });
  }
}