강동현

RoomDescription, RoomInfo 수정

1 import { Connection } from "../../connection/Connection"; 1 import { Connection } from "../../connection/Connection";
2 import { RoomManager } from "../../room/RoomManager"; 2 import { RoomManager } from "../../room/RoomManager";
3 +import { RoomInfo } from "../../room/types";
3 import { User } from "../../user/User"; 4 import { User } from "../../user/User";
4 -import { MessageResponse, RoomInfo, RoomJoinMessage } from "../types"; 5 +import { MessageResponse, RoomJoinMessage } from "../types";
5 6
6 export function roomJoinHandler( 7 export function roomJoinHandler(
7 user: User, 8 user: User,
...@@ -9,8 +10,8 @@ export function roomJoinHandler( ...@@ -9,8 +10,8 @@ export function roomJoinHandler(
9 ): MessageResponse<RoomInfo> { 10 ): MessageResponse<RoomInfo> {
10 const room = RoomManager.instance().get(message.uuid); 11 const room = RoomManager.instance().get(message.uuid);
11 if (room !== undefined) { 12 if (room !== undefined) {
12 - const roomInfo = room.connect(user); 13 + room.connect(user);
13 - return { ok: roomInfo !== undefined, result: roomInfo }; 14 + return { ok: user.room !== undefined, result: user.room?.getInfo() };
14 } 15 }
15 return { ok: false }; 16 return { ok: false };
16 } 17 }
......
1 -import { RoomData } from "../room/types"; 1 +import { RoomDescription } from "../room/types";
2 import { UserData } from "../user/types"; 2 import { UserData } from "../user/types";
3 3
4 export interface Message { 4 export interface Message {
...@@ -39,11 +39,11 @@ export class LoginMessage implements Message { ...@@ -39,11 +39,11 @@ export class LoginMessage implements Message {
39 */ 39 */
40 export class RoomListMessage implements Message { 40 export class RoomListMessage implements Message {
41 readonly type = MessageType.ROOM_LIST; 41 readonly type = MessageType.ROOM_LIST;
42 - constructor(public rooms: RoomData[]) {} 42 + constructor(public rooms: RoomDescription[]) {}
43 } 43 }
44 44
45 /** 45 /**
46 - * 클라 -> 서버 -> RoomInfoMessage 46 + * 클라 -> 서버 -> RoomInfo
47 * 방에 접속합니다. 47 * 방에 접속합니다.
48 */ 48 */
49 export class RoomJoinMessage implements Message { 49 export class RoomJoinMessage implements Message {
...@@ -93,11 +93,3 @@ export class MessageType { ...@@ -93,11 +93,3 @@ export class MessageType {
93 static readonly ROOM_USER_UPDATE = "room_user_update"; 93 static readonly ROOM_USER_UPDATE = "room_user_update";
94 static readonly ROOM_CHAT = "room_chat"; 94 static readonly ROOM_CHAT = "room_chat";
95 } 95 }
96 -
97 -/**
98 - * 방의 정보를 담고 있습니다.
99 - * @param userdata 현재 방에 접속 중인 유저 목록입니다.
100 - */
101 -export interface RoomInfo {
102 - userdata: UserData[];
103 -}
......
1 import { Connection } from "../connection/Connection"; 1 import { Connection } from "../connection/Connection";
2 import { v4 as uuidv4 } from "uuid"; 2 import { v4 as uuidv4 } from "uuid";
3 -import { RoomData } from "./types"; 3 +import { RoomDescription, RoomInfo } from "./types";
4 import { 4 import {
5 Message, 5 Message,
6 RoomChatMessage, 6 RoomChatMessage,
7 - RoomInfo,
8 RoomUserUpdateMessage, 7 RoomUserUpdateMessage,
9 } from "../message/types"; 8 } from "../message/types";
10 import { UserData } from "../user/types"; 9 import { UserData } from "../user/types";
...@@ -26,23 +25,15 @@ export class Room { ...@@ -26,23 +25,15 @@ export class Room {
26 this.maxUsers = maxUsers; 25 this.maxUsers = maxUsers;
27 } 26 }
28 27
29 - public connect(user: User): RoomInfo | undefined { 28 + public connect(user: User): void {
30 if (this.users.includes(user) || this.users.length >= this.maxUsers) { 29 if (this.users.includes(user) || this.users.length >= this.maxUsers) {
31 - return undefined; 30 + return;
32 } 31 }
33 32
34 this.broadcast(new RoomUserUpdateMessage("added", user.getData())); 33 this.broadcast(new RoomUserUpdateMessage("added", user.getData()));
35 34
36 this.users.push(user); 35 this.users.push(user);
37 user.room = this; // TODO: 더 나은 관리 36 user.room = this; // TODO: 더 나은 관리
38 -
39 - var users: UserData[] = [];
40 - this.users.forEach((u) => {
41 - if (user !== u) {
42 - users.push(u.getData());
43 - }
44 - });
45 - return { userdata: users };
46 } 37 }
47 38
48 public disconnect(user: User): void { 39 public disconnect(user: User): void {
...@@ -59,7 +50,7 @@ export class Room { ...@@ -59,7 +50,7 @@ export class Room {
59 this.broadcast(new RoomChatMessage(message, user.username), user); 50 this.broadcast(new RoomChatMessage(message, user.username), user);
60 } 51 }
61 52
62 - public getData(): RoomData { 53 + public getDescription(): RoomDescription {
63 return { 54 return {
64 uuid: this.uuid, 55 uuid: this.uuid,
65 name: this.name, 56 name: this.name,
...@@ -68,6 +59,16 @@ export class Room { ...@@ -68,6 +59,16 @@ export class Room {
68 }; 59 };
69 } 60 }
70 61
62 + public getInfo(): RoomInfo {
63 + var users: UserData[] = this.users.map((u) => u.getData());
64 + return {
65 + uuid: this.uuid,
66 + name: this.name,
67 + maxUsers: this.maxUsers,
68 + users: users,
69 + };
70 + }
71 +
71 public broadcast(message: Message, except?: User): void { 72 public broadcast(message: Message, except?: User): void {
72 this.users.forEach((u) => { 73 this.users.forEach((u) => {
73 if (u !== except) { 74 if (u !== except) {
......
1 import { Connection } from "../connection/Connection"; 1 import { Connection } from "../connection/Connection";
2 import { RoomListMessage } from "../message/types"; 2 import { RoomListMessage } from "../message/types";
3 import { Room } from "./Room"; 3 import { Room } from "./Room";
4 -import { RoomData } from "./types"; 4 +import { RoomDescription } from "./types";
5 5
6 export class RoomManager { 6 export class RoomManager {
7 private static _instance: RoomManager; 7 private static _instance: RoomManager;
...@@ -36,9 +36,9 @@ export class RoomManager { ...@@ -36,9 +36,9 @@ export class RoomManager {
36 } 36 }
37 37
38 public sendList(connection: Connection): void { 38 public sendList(connection: Connection): void {
39 - var roomData: RoomData[] = []; 39 + var roomData: RoomDescription[] = [];
40 this.rooms.forEach((room) => { 40 this.rooms.forEach((room) => {
41 - roomData.push(room.getData()); 41 + roomData.push(room.getDescription());
42 }); 42 });
43 43
44 connection.send(new RoomListMessage(roomData)); 44 connection.send(new RoomListMessage(roomData));
......
1 -export interface RoomData { 1 +import { UserData } from "../user/types";
2 +
3 +/**
4 + * 방 리스트에서 사용됩니다.
5 + */
6 +export interface RoomDescription {
2 uuid: string; 7 uuid: string;
3 name: string; 8 name: string;
4 currentUsers: number; 9 currentUsers: number;
5 maxUsers: number; 10 maxUsers: number;
6 } 11 }
12 +
13 +/**
14 + * 방에 접속했을 때 사용됩니다.
15 + */
16 +export interface RoomInfo {
17 + uuid: string;
18 + name: string;
19 + maxUsers: number;
20 + users: UserData[];
21 +}
......