From 2ebceafa998b06a4e7749312a13803534606efe8 Mon Sep 17 00:00:00 2001 From: TheBanHammer Date: Sat, 21 May 2016 11:34:27 +0100 Subject: [PATCH] Fixes #69: "Once in a million error" --- socketserver/djqueue.js | 102 ++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/socketserver/djqueue.js b/socketserver/djqueue.js index 2f5ea27..97182f2 100644 --- a/socketserver/djqueue.js +++ b/socketserver/djqueue.js @@ -11,7 +11,7 @@ var defaultVoteObj = function(){ function makeSongObj(input){ if (!input) return input; - + delete input.thumbnail; return input; } @@ -21,7 +21,7 @@ function djqueue(room){ if (!room){ return; } - + this.room = room; this.users = []; this.lastdj = null; @@ -34,13 +34,13 @@ function djqueue(room){ this.cycle = config.room.queue.cycle; this.lock = config.room.queue.lock; this.votes = new defaultVoteObj; -} +} djqueue.prototype.add = function(sock, position){ var res = this._add(sock, position); if(res.success){ - + if(this.users.length == 1 && this.currentdj == null) this.advance(); } @@ -49,18 +49,18 @@ djqueue.prototype.add = function(sock, position){ djqueue.prototype._add = function(sock, position){ var res = {success: false}; - + if(sock.user && this.checkQueuePos(sock) == -1 && (this.currentdj == null ? null : this.currentdj.user.uid) != sock.user.uid){ res.success = true; position = parseInt(position); - + if (isNaN(position) || position < 0 || position >= this.users.length){ this.users.push(sock); }else{ res.position = position; this.users.splice(position, 0, sock); } - + this.room.sendAll({ type: "userJoinedQueue", data: { @@ -73,14 +73,14 @@ djqueue.prototype._add = function(sock, position){ djqueue.prototype.replaceSocket = function(sock_old, sock_new){ var qpos = this.users.indexOf(sock_old); - + if (qpos >= 0) this.users[qpos] = sock_new; if (this.currentdj == sock_old) this.currentdj = sock_new; - + var lpos = this.votes.like.indexOf(sock_old); var dpos = this.votes.dislike.indexOf(sock_old); var gpos = this.votes.grab.indexOf(sock_old); - + if (lpos >= 0) this.votes.like[lpos] = sock_new; if (dpos >= 0) this.votes.dislike[dpos] = sock_new; if (gpos >= 0) this.votes.grab[gpos] = sock_new; @@ -88,47 +88,47 @@ djqueue.prototype.replaceSocket = function(sock_old, sock_new){ djqueue.prototype.remove = function(sock, type){ var index = this.checkQueuePos(sock); - + // Remove their vote for (var i in this.votes){ if (this.votes[i].indexOf(sock) > -1) this.vote(i, sock, true); } - + if(index != -1){ this.users.splice(index, 1); - + this.room.sendAll({ type: type == undefined ? "userLeftQueue" : type, data: { queueList: this.makeClientObj() } }); - + return true; } else if (sock.user && this.currentdj && this.currentdj.user.uid == sock.user.uid){ this.advance( true ); return true; } - - - + + + return false; }; djqueue.prototype.move = function(sock, to){ var from = this.checkQueuePos(sock); var res = {success: false}; - + if (from == to){ //TODO: Return error to sender, might need to add a callback return res; } - + if(to < this.users.length && to >= 0 && from != -1 && sock != this.currentdj){ this.users.splice(from, 1); var pos = (to > from+1 ? to-1 : to); this.users.splice( pos, 0, sock); - + res.success = true; res.data = { queueList: this.makeClientObj(), @@ -144,11 +144,11 @@ djqueue.prototype.swap = function(sock1, sock2){ var from = this.checkQueuePos(sock1); var to = this.checkQueuePos(sock2); var res = {success: false}; - + if(from != -1 && to != -1 && sock1 != this.currentdj && sock2 != this.currentdj ){ this.users[from] = sock2; this.users[to] = sock1; - + res.data = { queueList: this.makeClientObj(), uid1: sock1.user.data.uid, @@ -156,7 +156,7 @@ djqueue.prototype.swap = function(sock1, sock2){ pos1: from, pos2: to }; - + res.success = true; } return res; @@ -170,10 +170,10 @@ djqueue.prototype.advance = function( ignoreCycle, lockSkipPosition ){ this.lastsong = this.currentsong; this.currentsong = null; clearTimeout(this.lasttimer); - + var isSetLockSkipPos = typeof lockSkipPosition === 'number' && !isNaN(lockSkipPosition); var res = {success: false}; - + if(this.users.length >= 1){ if (this.currentdj && (this.cycle || isSetLockSkipPos) && !ignoreCycle) { this.currentdj = null; @@ -186,7 +186,7 @@ djqueue.prototype.advance = function( ignoreCycle, lockSkipPosition ){ if((this.cycle || isSetLockSkipPos) && !ignoreCycle) { res.position = this._add(this.lastdj, lockSkipPosition).position; } - + this._advance(); res.success = true; } @@ -196,7 +196,7 @@ djqueue.prototype.advance = function( ignoreCycle, lockSkipPosition ){ djqueue.prototype._advance = function(){ var that = this; var lastStart = that.songstart; - + if (that.lastsong != null) { var historyObj = { votes: { @@ -210,16 +210,16 @@ djqueue.prototype._advance = function(){ }; that.room.addToHistory(historyObj); } - + if (this.users.length > 0){ this.currentdj = this.users.splice(0, 1)[0]; var pl = this.currentdj.user.playlistCache[this.currentdj.user.activepl]; - + pl.getFirstExpanded(function(err, data){ if (err){ return that._advance(); } - + //Show the unavailable song for a limited amount of time if(data.unavailable){ data.duration = 5; @@ -227,43 +227,43 @@ djqueue.prototype._advance = function(){ that.currentsong = data; pl.shiftToBottom(); - + that.songstart = Date.now(); that.room.sendAll({ type: "advance", data: { last: { song: makeSongObj(that.lastsong), - uid: (that.lastdj ? that.lastdj.user.uid : null), + uid: (that.lastdj && that.lastdj.user ? that.lastdj.user.uid : null), start: lastStart }, next: { song: makeSongObj(that.currentsong), - uid: (that.currentdj ? that.currentdj.user.uid : null), + uid: (that.currentdj && that.currentdj.user ? that.currentdj.user.uid : null), start: that.songstart } } }); - + // Allow to play streaming or live videos if (that.currentsong && that.currentsong.duration > 0 || !Roles.checkPermission(that.currentdj.user.role, 'djqueue.playLiveVideos')){ that.lasttimer = setTimeout(function(){ that.advance(that.currentdj.user.lastdj); }, that.currentsong.duration * 1000); } - + that.votes = new defaultVoteObj; - + if (that.currentsong && that.currentdj){ that.room.updateLobbyServer(that.currentsong, that.currentdj ? that.currentdj.user.getClientObj() : null); } }); }else{ //if (that.lastsong === null || that.lastdj === null) return; - + that.votes = new defaultVoteObj; that.songstart = null; - + that.room.sendAll({ type: "advance", data: { @@ -281,7 +281,7 @@ djqueue.prototype._advance = function(){ }); that.room.updateLobbyServer(null, null); } - + return false; }; @@ -325,12 +325,12 @@ djqueue.prototype.vote = function(voteType, sock, leaving){ if (!this.currentdj) return false; if (!sock.user) return false; if (sock.user.uid == this.currentdj.user.uid) return false; - + var ind = this.votes[voteType].indexOf(sock); - + if (ind > -1){ if (voteType == 'grab' && !leaving) return false; - + this.votes[voteType].splice(ind, 1); }else{ if (voteType == 'like' && this.votes.dislike.indexOf(sock) > -1){ @@ -338,10 +338,10 @@ djqueue.prototype.vote = function(voteType, sock, leaving){ }else if (voteType == 'dislike' && this.votes.like.indexOf(sock) > -1){ this.vote('like', sock); } - + this.votes[voteType].push(sock); } - + this.room.sendAll({ type: 'voteUpdate', data: { @@ -351,7 +351,7 @@ djqueue.prototype.vote = function(voteType, sock, leaving){ voted: (ind > -1 ? -1 : 1) // -1 for removed vote, 1 for added vote }, }); - + return true; }; @@ -360,7 +360,7 @@ djqueue.prototype.makeClientObj = function(){ for(var i =0; i < this.users.length; i++){ arr.push(this.users[i].user.uid); } - + return arr; }; @@ -377,7 +377,7 @@ djqueue.prototype.getVotes = function(){ var like = []; var dislike = []; var grab = []; - + for (var i in this.votes.like) like.push(this.votes.like[i].user.uid); for (var i in this.votes.dislike) dislike.push(this.votes.dislike[i].user.uid); for (var i in this.votes.grab) grab.push(this.votes.grab[i].user.uid); @@ -390,9 +390,9 @@ djqueue.prototype.getVotes = function(){ djqueue.prototype.getUserVote = function(socket){ var votes = []; - + if (!socket.user) return votes; - + if (this.votes.like.filter(function(a){ return a.user.uid == socket.user.uid;}).length != 0) votes.push('like'); if (this.votes.dislike.filter(function(a){ return a.user.uid == socket.user.uid;}).length != 0) votes.push('dislike'); if (this.votes.grab.filter(function(a){ return a.user.uid == socket.user.uid;}).length != 0) votes.push('grab'); @@ -402,7 +402,7 @@ djqueue.prototype.getUserVote = function(socket){ djqueue.prototype.getCurrentTime = function(){ if (!this.currentsong || this.currentsong.duration == 0 || !this.currentdj) return 0; - + return Math.abs((Date.now( )- this.songstart) / 1000); }; @@ -422,4 +422,4 @@ djqueue.prototype.isPlaying = function(sock){ /* DJ Queue Object End */ -module.exports = djqueue; \ No newline at end of file +module.exports = djqueue;