-
Notifications
You must be signed in to change notification settings - Fork 0
/
roomManager.js
393 lines (339 loc) · 10.8 KB
/
roomManager.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
var lodash = require('lodash'),
randomstring = require('randomstring');
/*
{
roomName: {
host: String,
musicList: [{
id: String,
torrentLink: String,
musicName: String,
username: String,
votes: {
username: {
type: String,
user: String
}
},
voteCount: Number
}]
membersList: Array<String>
}
}
*/
var _rooms = {};
/**
* Checks if a room exists. If it doesn't, throws.
* @param {String} roomName
* @private
* @throws
*/
function _throwIfRoomNotExists(roomName) {
if (!lodash.has(_rooms, roomName)) {
throw new Error("Room does not exist");
}
}
/**
* Checks if a music exists and gets its index.
* If it doesn't, throws.
* @param {String} roomName
* @param {String} musicId
*/
function _throwIfMusicNotExists(roomName, musicId) {
var musicIndex = lodash.findIndex(_rooms[roomName].musicList,
function (musicInfoObj) {
if (String(musicInfoObj.id) === String(musicId)) {
return true;
}
return false;
});
if (musicIndex === -1) {
throw new Error("That music " + musicId +
" does not exist in this room!");
}
return musicIndex;
}
/**
* Checks if a user exists.
* @param {String} roomName
* @param {String} username
* @returns {Boolean}
*/
function _checkUserExists(roomName, username) {
var userIndex = _rooms[roomName].membersList.indexOf(username);
if (userIndex === -1) {
return false;
}
return true;
}
/*
* Checks if music exists in a room.
* @param {String} roomName
* @param {String} musicId
* @returns {Boolean}
*/
function _checkMusicExists(roomName, musicId) {
var musicExistence = false;
_rooms[roomName].musicList.forEach(function (musicInfoObj) {
if (String(musicInfoObj.id) === String(musicId)) {
musicExistence = true;
}
});
return musicExistence;
}
function _sortMusicListByVotes(roomName) {
_rooms[roomName].musicList.sort(function (musicA, musicB) {
if (musicA.voteCount > musicB.voteCount) {
return 1;
}
else if (musicA.voteCount < musicB.voteCount) {
return -1;
}
else {
return 0;
}
});
}
/**
* Creates a room and adds the initial user at this room
* @param {String} hostUsername - username of the room's host
* @return {String} The name of the room
*/
function createRoom(hostUsername) {
var roomName = randomstring.generate({
length: 7,
capitalization: "lowercase"
});
while (lodash.has(_rooms, roomName)) {
roomName = randomstring.generate({
length: 7,
capitalization: "lowercase"
});
}
_rooms[roomName] = {
host: hostUsername,
musicList: [],
membersList: [hostUsername]
};
return roomName;
}
/**
* Destroys the room
* @param {String} roomName - The name of the room
*/
function removeRoom(roomName) {
_throwIfRoomNotExists(roomName);
delete _rooms[roomName];
}
/**
* Provides a list of usernames on a specific room
* @param {String} roomName - The name of the room
* @returns {Array<String>} List of usernames on that specific room
*/
function getRoomMembers(roomName) {
_throwIfRoomNotExists(roomName);
return lodash.clone(_rooms[roomName].membersList);
}
/**
* Provides a list of music queued for a room
* @param {String} roomName - The name of the room
* @returns {Array<Object>} List of music queued on that specific room
*/
function getRoomMusicList(roomName) {
_throwIfRoomNotExists(roomName);
return lodash.cloneDeep(_rooms[roomName].musicList);
}
/**
* Adds a user to a room
* @param {String} roomName - The name of the room
* @param {String} username - username
*/
function addRoomMember(roomName, username) {
_throwIfRoomNotExists(roomName);
if (_checkUserExists(roomName, username)) {
throw new Error("User already exists");
}
_rooms[roomName].membersList.push(username);
}
/**
* Checks if a user is in the room
* @param {String} roomName - The name of the room
* @param {String} username - username
* @returns {Boolean} TRUE if user exists, FALSE otherwise
*/
function memberInRoom(roomName, username) {
return _checkUserExists(roomName, username);
}
/**
* Remove a user and their music from the room
* @param {String} roomName - The name of the room
* @param {String} username - username
*/
function removeRoomMember(roomName, username) {
_throwIfRoomNotExists(roomName);
// Remove that member from the room
lodash.remove(_rooms[roomName].membersList, function (someUsername) {
return String(someUsername) === String(username);
});
// Remove that member's music from the room
lodash.remove(_rooms[roomName].musicList, function (musicInfoObj) {
return String(musicInfoObj.username) ===
String(musicInfoObj.username);
});
}
/**
* Adds music to a queue on a room
* @param {String} roomName - The name of the room
* @param {String} musicInfoObj - {torrentLink, musicName, username}
* @returns {String} - The generated id for the music that's just added
*/
function addMusic(roomName, musicInfoObj) {
_throwIfRoomNotExists(roomName);
// Check that musicInfoObj abides by an interface
var requiredProperties = ["torrentLink", "musicName", "username"],
requiredPropsNotInObj =
requiredProperties.filter(function (propertyName) {
if (!lodash.has(_rooms, roomName)) {
return true;
}
return false;
});
if (requiredPropsNotInObj.length > 1) {
throw new Error("Tried adding a music without " +
requiredPropsNotInObj.join(" ") + " properties!");
}
// Check that the user who is adding music, exists in the room
if (!_checkUserExists(roomName, musicInfoObj.username)) {
throw new Error("That user does not exist in the room");
}
// Invalid provided torrent link
if (typeof musicInfoObj.torrentLink !== "string" ||
musicInfoObj.torrentLink.length === 0) {
throw new Error("Invalid Music Torrent URL");
}
// Invalid provided music name
if (typeof musicInfoObj.musicName !== "string" ||
musicInfoObj.musicName.length === 0) {
throw new Error("Invalid Music name");
}
var musicId = _rooms[roomName].musicList.length;
_rooms[roomName].musicList.push({
id: musicId,
torrentlink: musicInfoObj.torrentlink,
musicName: musicInfoObj.musicName,
username: musicInfoObj.username,
votes: {},
voteCount: 0
});
_sortMusicListByVotes(roomName);
return musicId;
}
/**
* Removes music on the queue of a room
* @param {String} roomName - The name of the room
* @param {String} musicId - The id of the music in that room's queue
*/
function removeMusic(roomName, musicId) {
_throwIfRoomNotExists(roomName);
// Remove the music from the queue of the room
lodash.remove(_rooms[roomName].musicList, function (musicInfoObj) {
if (String(musicInfoObj.id) === String(musicId)) {
return true;
}
return false;
});
}
/**
* Upvote a music on the queue of a room
* @param {String} roomName - The name of the room
* @param {String} musicId - The id of the music in that room's queue
*/
function upvoteMusic(roomName, musicId, username) {
_throwIfRoomNotExists(roomName);
if (!_checkMusicExists(roomName, musicId)) {
throw new Error("That music does not exist in the room's queue");
}
var musicIndex = _throwIfMusicNotExists(roomName, musicId),
votesObj = _rooms[roomName].musicList[musicIndex].votes,
voteCount = _rooms[roomName].musicList[musicIndex].voteCount;
if (lodash.has(votesObj, username)) {
// Second time they are upvoting? Reject
if (votesObj[username].type === "upvote") {
throw new Error("Already voted");
}
// They downvoted before? Reverse the downvote
else if (votesObj[username].type === "downvote") {
_rooms[roomName].musicList[musicIndex].voteCount++;
}
}
votesObj[username] = {
type: "upvote",
user: username
};
_rooms[roomName].musicList[musicIndex].voteCount++;
// Sort music by votes
_sortMusicListByVotes(roomName);
}
/**
* Downvote a music on the queue of a room
* @param {String} roomName - The name of the room
* @param {String} musicId - The id of the music in that room's queue
*/
function downvoteMusic(roomName, musicId, username) {
_throwIfRoomNotExists(roomName);
if (!_checkMusicExists(roomName, musicId)) {
throw new Error("That music does not exist in the room's queue");
}
var musicIndex = _throwIfMusicNotExists(roomName, musicId),
votesObj = _rooms[roomName].musicList[musicIndex].votes,
voteCount = _rooms[roomName].musicList[musicIndex].voteCount;
if (lodash.has(votesObj, username)) {
// Second time they are downvoting? Reject
if (votesObj[username].type === "downvote") {
throw new Error("Already voted");
}
// They upvoted before? Reverse the upvote
else if (votesObj[username].type === "upvote") {
_rooms[roomName].musicList[musicIndex].voteCount--;
}
}
votesObj[username] = {
type: "downvote",
user: username
};
_rooms[roomName].musicList[musicIndex].voteCount--;
// Sort music by votes
_sortMusicListByVotes(roomName);
}
/**
* Downvote a music on the queue of a room
* @param {String} roomName - The name of the room
* @param {String} musicId - The id of the music in that room's queue
* @returns {Number} votes, in Numbers
*/
function getVotes(roomName, musicId) {
_throwIfRoomNotExists(roomName);
if (!_checkMusicExists(roomName, musicId)) {
throw new Error("That music does not exist in the room's queue");
}
var musicIndex = lodash.findIndex(_rooms[roomName].musicList,
function (musicInfoObj) {
if (String(musicInfoObj.id) === musicId) {
return true;
}
return false;
});
return _rooms[roomName].musicList[musicIndex].voteCount;
}
exports.createRoom = createRoom;
exports.removeRoom = removeRoom;
exports.getRoomMembers = getRoomMembers;
exports.getRoomMusicList = getRoomMusicList;
exports.addRoomMember = addRoomMember;
exports.memberInRoom = memberInRoom;
exports.removeRoomMember = removeRoomMember;
exports.addMusic = addMusic;
exports.removeMusic = removeMusic;
exports.upvoteMusic = upvoteMusic;
exports.downvoteMusic = downvoteMusic;
exports.getVotes = getVotes;