User.ts 1.15 KB
import { UserData } from "../../common/dataType";
import { Connection } from "../connection/Connection";
import { MessageHandler } from "../message/MessageHandler";
import { Room } from "../room/Room";
import { RoomManager } from "../room/RoomManager";

export class User {
  public readonly username: string;

  public readonly connection: Connection;

  public room?: Room;

  public handler: MessageHandler;

  constructor(username: string, connection: Connection) {
    this.username = username;
    this.connection = connection;
    this.handler = new MessageHandler({
      roomList: (user, message) => {
        return { ok: true, result: RoomManager.instance().list() };
      },
      joinRoom: (user, message) => {
        const room = RoomManager.instance().get(message.uuid);
        if (user.room || !room) {
          return { ok: false };
        }
        // TODO: 방 접속 실패 처리
        room.connect(user);
        if (user.room === undefined) {
          return { ok: false };
        }
        return { ok: true, result: room.getInfo() };
      },
    });
  }

  public getData(): UserData {
    return {
      username: this.username,
    };
  }
}