강동현

RoomDescription, RoomInfo 수정

import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { RoomInfo } from "../../room/types";
import { User } from "../../user/User";
import { MessageResponse, RoomInfo, RoomJoinMessage } from "../types";
import { MessageResponse, RoomJoinMessage } from "../types";
export function roomJoinHandler(
user: User,
......@@ -9,8 +10,8 @@ export function roomJoinHandler(
): MessageResponse<RoomInfo> {
const room = RoomManager.instance().get(message.uuid);
if (room !== undefined) {
const roomInfo = room.connect(user);
return { ok: roomInfo !== undefined, result: roomInfo };
room.connect(user);
return { ok: user.room !== undefined, result: user.room?.getInfo() };
}
return { ok: false };
}
......
import { RoomData } from "../room/types";
import { RoomDescription } from "../room/types";
import { UserData } from "../user/types";
export interface Message {
......@@ -39,11 +39,11 @@ export class LoginMessage implements Message {
*/
export class RoomListMessage implements Message {
readonly type = MessageType.ROOM_LIST;
constructor(public rooms: RoomData[]) {}
constructor(public rooms: RoomDescription[]) {}
}
/**
* 클라 -> 서버 -> RoomInfoMessage
* 클라 -> 서버 -> RoomInfo
* 방에 접속합니다.
*/
export class RoomJoinMessage implements Message {
......@@ -93,11 +93,3 @@ export class MessageType {
static readonly ROOM_USER_UPDATE = "room_user_update";
static readonly ROOM_CHAT = "room_chat";
}
/**
* 방의 정보를 담고 있습니다.
* @param userdata 현재 방에 접속 중인 유저 목록입니다.
*/
export interface RoomInfo {
userdata: UserData[];
}
......
import { Connection } from "../connection/Connection";
import { v4 as uuidv4 } from "uuid";
import { RoomData } from "./types";
import { RoomDescription, RoomInfo } from "./types";
import {
Message,
RoomChatMessage,
RoomInfo,
RoomUserUpdateMessage,
} from "../message/types";
import { UserData } from "../user/types";
......@@ -26,23 +25,15 @@ export class Room {
this.maxUsers = maxUsers;
}
public connect(user: User): RoomInfo | undefined {
public connect(user: User): void {
if (this.users.includes(user) || this.users.length >= this.maxUsers) {
return undefined;
return;
}
this.broadcast(new RoomUserUpdateMessage("added", user.getData()));
this.users.push(user);
user.room = this; // TODO: 더 나은 관리
var users: UserData[] = [];
this.users.forEach((u) => {
if (user !== u) {
users.push(u.getData());
}
});
return { userdata: users };
}
public disconnect(user: User): void {
......@@ -59,7 +50,7 @@ export class Room {
this.broadcast(new RoomChatMessage(message, user.username), user);
}
public getData(): RoomData {
public getDescription(): RoomDescription {
return {
uuid: this.uuid,
name: this.name,
......@@ -68,6 +59,16 @@ export class Room {
};
}
public getInfo(): RoomInfo {
var users: UserData[] = this.users.map((u) => u.getData());
return {
uuid: this.uuid,
name: this.name,
maxUsers: this.maxUsers,
users: users,
};
}
public broadcast(message: Message, except?: User): void {
this.users.forEach((u) => {
if (u !== except) {
......
import { Connection } from "../connection/Connection";
import { RoomListMessage } from "../message/types";
import { Room } from "./Room";
import { RoomData } from "./types";
import { RoomDescription } from "./types";
export class RoomManager {
private static _instance: RoomManager;
......@@ -36,9 +36,9 @@ export class RoomManager {
}
public sendList(connection: Connection): void {
var roomData: RoomData[] = [];
var roomData: RoomDescription[] = [];
this.rooms.forEach((room) => {
roomData.push(room.getData());
roomData.push(room.getDescription());
});
connection.send(new RoomListMessage(roomData));
......
export interface RoomData {
import { UserData } from "../user/types";
/**
* 방 리스트에서 사용됩니다.
*/
export interface RoomDescription {
uuid: string;
name: string;
currentUsers: number;
maxUsers: number;
}
/**
* 방에 접속했을 때 사용됩니다.
*/
export interface RoomInfo {
uuid: string;
name: string;
maxUsers: number;
users: UserData[];
}
......