User.ts
1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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: connection.roomManager.list() };
},
createRoom: (user, message) => {
if (user.room) {
return { ok: false };
}
if (message.name.length >= 30 || message.name.trim().length === 0) {
return { ok: false };
}
const room = connection.roomManager.create(message.name, 8, user);
return { ok: true, result: room.getInfo() };
},
joinRoom: (user, message) => {
const room = connection.roomManager.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 disconnected(): void {
this.room?.disconnect(this);
}
}