Room.ts
3.22 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Connection } from "../connection/Connection";
import { v4 as uuidv4 } from "uuid";
import { User } from "../user/User";
import { MessageHandler } from "../message/MessageHandler";
import {
ServerInboundMessage,
ServerOutboundMessage,
ServerOutboundMessageKey,
} from "../../common";
import { RoomDescription, RoomInfo, UserData } from "../../common/dataType";
import { RoomManager } from "./RoomManager";
export class Room {
public readonly uuid: string;
public name: string;
public readonly maxUsers: number;
public readonly roomManager: RoomManager;
public users: User[] = [];
public admin?: User;
public closed: boolean = false;
public handler: MessageHandler;
constructor(
roomManager: RoomManager,
name: string,
admin?: User,
maxUsers: number = 8
) {
this.uuid = uuidv4();
this.name = name;
this.maxUsers = maxUsers;
this.roomManager = roomManager;
this.admin = admin;
this.handler = new MessageHandler({
chat: (user, message) => {
this.sendChat(user, message.message);
return { ok: true };
},
leaveRoom: (user, message) => {
this.disconnect(user);
return { ok: true };
},
});
if (this.admin) {
this.connect(this.admin);
}
}
public connect(user: User): void {
if (this.users.includes(user) || this.users.length >= this.maxUsers) {
return;
}
this.broadcast("updateRoomUser", {
state: "added",
user: {
username: user.username,
admin: user === this.admin,
},
});
this.users.push(user);
user.room = this;
}
public disconnect(user: User): void {
const index = this.users.indexOf(user);
if (index > -1) {
this.users.splice(index, 1);
user.room = undefined;
this.broadcast("updateRoomUser", {
state: "removed",
user: {
username: user.username,
admin: user === this.admin,
},
});
if (this.users.length === 0) {
this.close();
} else {
this.setNextAdmin();
}
}
}
private setNextAdmin(): void {
const nextAdmin = this.users[Math.floor(Math.random() * this.users.length)];
this.admin = nextAdmin;
}
public sendChat(user: User, message: string): void {
this.broadcast("chat", { sender: user.username, message: message });
}
public getDescription(): RoomDescription {
return {
uuid: this.uuid,
name: this.name,
currentUsers: this.users.length,
maxUsers: this.maxUsers,
};
}
public getInfo(): RoomInfo {
return {
uuid: this.uuid,
name: this.name,
maxUsers: this.maxUsers,
users: this.users.map((u) => {
return {
username: u.username,
admin: u === this.admin,
};
}),
};
}
public broadcast<T extends ServerOutboundMessageKey>(
type: T,
message: ServerOutboundMessage<T>,
except?: User
): void {
this.users.forEach((u) => {
if (u !== except) {
u.connection.send(type, message);
}
});
}
public close(): void {
if (!this.closed) {
this.users.forEach((u) => this.disconnect(u));
this.closed = true;
this.roomManager.delete(this.uuid);
}
}
}