-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
420 lines (367 loc) · 9.71 KB
/
server.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.use('/', express.static('./client'));
var server = app.listen(port);
var io = require('socket.io')({
transports: ["xhr-polling"],
'polling duration': 10,
'pingInterval': 100,
'pingTimeout': Infinity
}).listen(server);
var users = {};
var queue = [];
var votes = {};
var upvotes = 0;
var downvotes = 0;
var current;
var timeTotal;
var timeLeft;
var sync;
var set;
var switched = false;
var adjectives = [
"nameless",
"undisclosed",
"unidentified",
"unnamed",
"incognito",
"secret",
"hidden",
"discreet",
"concealed",
"obscured",
"masked",
"veiled",
"shrouded",
"disguised",
"repressed",
"shy",
"anonymous",
"unknown",
"sly",
"sneaky",
"stealthy",
"furtive",
"covert",
"shady",
"clandestine",
"hushed",
"closeted",
"surreptitious",
"unauthorized",
"cloaked",
"invisible",
"undercover"
]
var creatures = [
"oyster",
"impala",
"mackerel",
"roedeer",
"greyhound",
"wasp",
"magpie",
"polecat",
"zebra",
"clam",
"pidgeon",
"rattlesnake",
"pheasant",
"ptarmigan",
"gnat",
"quail",
"heron",
"elk",
"camel",
"panda",
"turtledove",
"wombat",
"bullfinche",
"lapwing",
"dolphin",
"moth",
"wildebeest",
"lemur",
"sheldrake",
"woodchuck",
"otter",
"bobolink",
"smelt",
"raccoon",
"ostrich",
"rhinoceros",
"pelican",
"gibbon",
"dingo",
"mandrill",
"weasel",
"penguin",
"guillemot",
"walrus",
"opossum",
"bongo",
"yak",
"goosander",
"gorilla"
]
var uploads = {};
var getRandom = function(words) {
var index = Math.floor(Math.random() * words.length);
return words[index];
}
//Helper function to reset the state of clients connected
var reset = function() {
votes = {};
upvotes = 0;
downvotes = 0;
io.emit('clearVotes');
if (current && current.type === 'upload') {
current.file = uploads[current.id];
delete uploads[current.id];
}
io.emit('nextVideo', current);
io.emit('setQueue', queue);
}
io.on('connection', function(socket) {
socket.on('upload', function(upload, callback) {
uploads[upload.key] = upload.file;
callback(upload.file.name);
})
//Receives username from client and emits username, socket id, users online back to client
socket.on('username', function(username) {
if (username === 'anonymous') {
username = getRandom(adjectives) + "_" + getRandom(creatures);
}
users[socket.id] = username;
io.sockets.connected[socket.id].emit('setUser', username);
io.sockets.connected[socket.id].emit('setId', socket.id);
socket.broadcast.emit('joinMessage', {joined: "›› " + users[socket.id] + " has joined ››"});
io.sockets.connected[socket.id].emit('chatMessage', {
username: "playbot",
message: "Hello, " + users[socket.id] + "! " + "To get started, search for videos or sounds to add to the playlist. You can also drag and drop an mp3 track to the playlist. Type '/help' to see a list of commands."
});
io.emit('usersOnline', users);
io.sockets.connected[socket.id].emit('setQueue', queue);
});
//Sends queue information to client
socket.on('getQueue', function() {
if (queue.length) {
io.sockets.connected[socket.id].emit('setQueue', queue);
}
});
//Sends current video to client
socket.on('getCurrent', function() {
if (current) {
io.sockets.connected[socket.id].emit('getCurrent', current);
} else {
io.sockets.connected[socket.id].emit('setVolume');
}
});
//Sends vote information for current video to client
socket.on('getVotes', function() {
io.sockets.connected[socket.id].emit('changeVotes', {up: upvotes, down: downvotes});
});
//Sends video duration to client
socket.on('getTime', function() {
io.sockets.connected[socket.id].emit('setTime', timeTotal - timeLeft || timeTotal);
});
//Emits message to all clients
socket.on('sendMessage', function(data) {
io.emit('chatMessage', data);
});
socket.on('enqueue', function(data) {
if (current) {
queue.push(data);
io.emit('addVideo', data);
} else {
set = false;
current = data;
reset();
}
});
socket.on('dequeue', function(data) {
var id = socket.id;
if (id.slice(2) === data.socket) {
io.emit('removeVideo', data.id);
}
});
socket.on('updateQueue', function(data) {
queue = data;
socket.broadcast.emit('refreshQueue', queue);
});
socket.on('easterEgg', function() {
if (queue.length) {
queue.unshift({ id: 'SbyZDq76T74',
title: 'Meow Mix song',
username: 'Meow Mode',
socket: current.socket });
current = queue.shift();
reset();
} else {
current = { id: 'SbyZDq76T74',
title: 'Meow Mix song',
username: 'Meow Mode',
socket: socket.id.slice(2) };
reset();
}
});
//Receives video ended from client and sets current to next in queue
//Jerry-rigged so that the fastest client emits the switch and locks other clients out for half of video duration
socket.on('ended', function() {
var id = socket.id;
if (current && id.slice(2) === current.socket) {
switched = false;
}
if (switched === false) {
switched = true;
console.log('switched', switched);
set = false;
if (queue.length) {
current = queue.shift();
var timer;
if (current.type === 'soundcloud') {
timer = current.duration/2;
} else if (current.type === 'youtube') {
timer = ((current.duration/60) * 1000)/2;
} else if (current.type === 'upload') {
timer = 7500;
}
reset();
setTimeout(function() {
switched = false;
}, timer);
} else {
switched = false;
set = false;
current = null;
reset();
io.emit('stopVideo');
}
}
});
socket.on('seekForward', function(seconds) {
var id = socket.id;
if (current && id.slice(2) === current.socket) {
timeLeft -= seconds;
io.emit('seekForward', seconds);
}
})
//Allows skipping if event is emitted by client who enqueued video
socket.on('skip', function(easterEgg) {
var id = socket.id;
if (current && id.slice(2) === current.socket || easterEgg) {
if (current.type === 'upload') {
io.emit('triggerEnded');
} else {
io.emit('resetTimer');
if (queue.length) {
set = false;
current = queue.shift();
reset();
} else {
set = false;
current = null;
reset();
io.emit('stopVideo');
}
}
}
});
//Start the video sync clock
socket.on('setDuration', function(video) {
if (video.sc === true) {
video.duration = video.duration/1000;
}
if (!set) {
set = true;
timeTotal = video.duration;
timeLeft = video.duration;
clearInterval(sync);
setTimeout(function() {
sync = setInterval(function() {
timeLeft--;
if (timeLeft < 0) {
clearInterval(sync);
}
}, 1000);
}, 2000);
}
});
//Removes client vote information and delete client on client disconnect
socket.on('disconnect', function() {
var name = users[socket.id] || 'someone';
if (votes[socket.id] === 'up') {
upvotes--;
}
if (votes[socket.id] === 'down') {
downvotes--;
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
io.emit('joinMessage', {left: "›› " + name + " has left ››"});
delete users[socket.id];
io.emit('usersOnline', users);
});
//Handles socket reconnect event with client
socket.on('reconnect', function() {
io.sockets.connected[socket.id].emit('comeback');
});
//Updates username on reconnection
socket.on('return', function(username) {
users[socket.id] = username;
io.sockets.connected[socket.id].emit('setUser', username);
io.sockets.connected[socket.id].emit('setId', socket.id);
io.emit('chatMessage', {username: "", message: users[socket.id] + " has reconnected"});
io.emit('usersOnline', users);
})
//Receives upvote from client and updates vote information; emitting to all clients
socket.on('upVote', function() {
if (votes[socket.id] === 'down') {
votes[socket.id] = 'up';
downvotes--;
upvotes++;
}
if (votes[socket.id] === undefined) {
votes[socket.id] = 'up';
upvotes++;
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
});
socket.on('downVote', function(){
if(votes[socket.id] === 'up'){
votes[socket.id] = 'down';
upvotes--;
downvotes++;
}
if(votes[socket.id] === undefined) {
votes[socket.id] = 'down';
downvotes++;
}
//Skips current video if more haters than non-haters
var haters = downvotes/Object.keys(users).length;
if(haters > 0.5) {
if (current.type === 'upload') {
io.emit('triggerEnded');
} else {
if (queue.length) {
set = false;
current = queue.shift();
reset();
} else {
set= false;
current = null;
reset();
io.emit('stopVideo');
}
}
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
});
//Sends clock time to client when requested
socket.on('getSync', function(type) {
io.sockets.connected[socket.id].emit('setSync', {duration: timeTotal - timeLeft || timeTotal, remaining: timeLeft});
});
socket.on('getDuration', function() {
io.sockets.connected[socket.id].emit('uploadDuration', {duration: timeTotal - timeLeft || timeTotal, remaining: timeLeft});
});
});