Skip to content

Commit

Permalink
Fixes #69: "Once in a million error"
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBanHammer committed May 21, 2016
1 parent f77be6c commit 2ebceaf
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions socketserver/djqueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var defaultVoteObj = function(){

function makeSongObj(input){
if (!input) return input;

delete input.thumbnail;
return input;
}
Expand All @@ -21,7 +21,7 @@ function djqueue(room){
if (!room){
return;
}

this.room = room;
this.users = [];
this.lastdj = null;
Expand All @@ -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();
}
Expand All @@ -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: {
Expand All @@ -73,62 +73,62 @@ 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;
};

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(),
Expand All @@ -144,19 +144,19 @@ 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,
uid2: sock2.user.data.uid,
pos1: from,
pos2: to
};

res.success = true;
}
return res;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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: {
Expand All @@ -210,60 +210,60 @@ 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;
}

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: {
Expand All @@ -281,7 +281,7 @@ djqueue.prototype._advance = function(){
});
that.room.updateLobbyServer(null, null);
}

return false;
};

Expand Down Expand Up @@ -325,23 +325,23 @@ 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){
this.vote('dislike', sock);
}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: {
Expand All @@ -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;
};

Expand All @@ -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;
};

Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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);
};
Expand All @@ -422,4 +422,4 @@ djqueue.prototype.isPlaying = function(sock){

/* DJ Queue Object End */

module.exports = djqueue;
module.exports = djqueue;

0 comments on commit 2ebceaf

Please sign in to comment.