-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrooms.js
123 lines (109 loc) · 2.97 KB
/
rooms.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
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
const rooms = new Map(); // roomName: [userId]
const users = new Map(); // userId: {userName, roomName}
const maxUsers = 6;
const _formatUser = (userId) => {
let obj = {};
obj["userId"] = userId;
obj["userName"] = users.get(userId).userName;
return obj;
};
function _makeRoomCode(length) {
var result = "";
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const addUserToRoom = (id, name, room) => {
// name = name.trim().toLowerCase();
room = room.trim().toLowerCase();
if (rooms.has(room)) {
const roomUsers = rooms.get(room);
const userNames = roomUsers.map((userId) => users.get(userId).userName);
if (userNames.includes(name)) {
return { status: 2, message: "Username already taken" };
} else {
if (roomUsers.length >= maxUsers) {
return { status: 3, message: "Room is full" };
}
rooms.get(room).push(id);
users.set(id, { userName: name, roomName: room });
return { status: 0, message: "Success" };
}
} else {
return { status: 1, message: "Room does not exist" };
}
};
const removeUserFromRoom = (id, room) => {
room = room.trim().toLowerCase();
if (rooms.has(room)) {
const index = rooms.get(room).findIndex((userId) => userId === id);
if (index > -1) {
rooms.get(room).splice(index, 1);
users.delete(id);
if (rooms.get(room).length == 0) {
closeRoom(room);
}
return { status: 0, message: "Success" };
} else {
return { status: 2, message: "User does not exist in room" };
}
} else {
return { status: 1, message: "Room does not exist" };
}
};
const getUsersFromRoom = (roomName) => {
var room = roomName.trim().toLowerCase();
if (rooms.has(room)) {
return rooms.get(room).map(_formatUser);
} else {
return null;
}
};
const getUser = (userId) => {
if (users.has(userId)) {
return users.get(userId);
} else {
return null;
}
};
const isRoomOpen = (roomName) => {
var room = roomName.trim().toLowerCase();
if (rooms.has(room)) {
return true;
} else {
return false;
}
};
const createRoom = () => {
var roomCode = _makeRoomCode(4);
while (rooms.has(roomCode)) {
roomCode = _makeRoomCode(4);
}
rooms.set(roomCode.toLowerCase(), new Array());
return roomCode;
};
const closeRoom = (roomName) => {
console.log(`closing room ${roomName}`);
if (rooms.has(roomName)) {
// Remove all users in roomName
const roomUsers = rooms.get(roomName);
roomUsers.forEach((userId) => users.delete(userId));
// Remove roomName from rooms
rooms.delete(roomName);
return { status: 0, message: "Success" };
} else {
return { status: 1, message: "Room does not exist" };
}
};
module.exports = {
addUserToRoom,
createRoom,
removeUserFromRoom,
getUsersFromRoom,
getUser,
closeRoom,
isRoomOpen,
};