-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_class.js
65 lines (54 loc) · 1.36 KB
/
room_class.js
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
'use strict';
module.exports = class Room {
constructor(name, roomID) {
this.name = name;
this.event = undefined;
this.roomTime = 0;
this.users = [];
this.usersLength = 0;
this.shareURL = undefined;
this.roomID = roomID;
}
getSocketsArr() {
const socketList = [];
for (let i = 0; i < this.usersLength; i++) {
socketList.push(this.users[i].socket);
}
return socketList;
}
getUsers() {
return this.users;
}
addUser(socket, name) {
if (!this.users.find(item => item.name === name)) {
this.users.push({ name, socket });
this.usersLength++;
return true;
} else return false;
}
disconnectUser(name) {
const userIndex = this.users.findIndex(item => item.name === name);
if (userIndex !== -1) {
console.log(`room ${this.name}: ${this.getUser(name).name} disconnected`);
this.users.splice(userIndex, 1);
this.usersLength--;
return true;
} else return false;
}
getUser(name) {
const user = this.users.find(item => item.name === name);
if (user) return user; else return false;
}
getUsersNames() {
const list = [];
for (const key in this.users) list.push(this.users[key].name);
return list.sort();
}
nullUsers() {
if (this.usersLength <= 0) return true;
return false;
}
getRoomID() {
return this.roomID;
}
};