Room.ts 958 Bytes
import { Connection } from "../connection/Connection";
import { v4 as uuidv4 } from "uuid";

export class Room {
  public readonly uuid: string;

  public name: string;
  public readonly maxConnections: number;

  private connections: Connection[] = [];

  private closed: boolean = false;

  constructor(name: string, maxConnections: number = 8) {
    this.uuid = uuidv4();
    this.name = name;
    this.maxConnections = maxConnections;
  }

  public connect(connection: Connection): void {
    if (
      this.connections.includes(connection) ||
      this.connections.length >= this.maxConnections
    ) {
      return;
    }

    this.connections.push(connection);
  }

  public disconnect(connection: Connection): void {
    const index = this.connections.indexOf(connection);
    if (index > -1) {
      this.connections.splice(index, 1);
    }
  }

  public close(): void {
    if (!this.closed) {
      // TODO
      this.closed = true;
    }
  }
}