From 27c494e4502e25742be99981d547f4f715a8f209 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Mon, 22 Apr 2013 15:37:42 -0500 Subject: [PATCH] Implement shuffle/clear (Issue #48) --- channel.js | 39 +++++++++++++++++++++++++++++++++++++++ user.js | 12 ++++++++++++ www/assets/js/client.js | 8 ++++++++ www/index.html | 6 +++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/channel.js b/channel.js index 0036c228..9552abb3 100644 --- a/channel.js +++ b/channel.js @@ -785,6 +785,45 @@ Channel.prototype.tryJumpTo = function(user, data) { this.jumpTo(data.pos); } +Channel.prototype.clearqueue = function() { + this.queue = []; + for(var i = 0; i < this.users.length; i++) { + this.sendPlaylist(this.users[i]); + } +} + +Channel.prototype.tryClearqueue = function(user) { + if(!Rank.hasPermission(user, "queue")) { + return; + } + this.clearqueue(); +} + +Channel.prototype.shufflequeue = function() { + var n = []; + var current = false; + while(this.queue.length > 0) { + var i = parseInt(Math.random() * this.queue.length); + n.push(this.queue[i]); + if(!current && i == this.position) { + this.position = n.length - 1; + current = true; + } + this.queue.splice(i, 1); + } + this.queue = n; + for(var i = 0; i < this.users.length; i++) { + this.sendPlaylist(this.users[i]); + } +} + +Channel.prototype.tryShufflequeue = function(user) { + if(!Rank.hasPermission(user, "queue")) { + return; + } + this.shufflequeue(); +} + Channel.prototype.tryUpdate = function(user, data) { if(this.leader != user) { return; diff --git a/user.js b/user.js index 27cb17a1..1a626a46 100644 --- a/user.js +++ b/user.js @@ -153,6 +153,18 @@ User.prototype.initCallbacks = function() { } }.bind(this)); + this.socket.on("clearqueue", function() { + if(this.channel != null) { + this.channel.tryClearqueue(this); + } + }.bind(this)); + + this.socket.on("shufflequeue", function() { + if(this.channel != null) { + this.channel.tryShufflequeue(this); + } + }.bind(this)); + this.socket.on("queueLock", function(data) { if(this.channel != null) { this.channel.trySetLock(this, data); diff --git a/www/assets/js/client.js b/www/assets/js/client.js index 75d893b8..a1f1c6a4 100644 --- a/www/assets/js/client.js +++ b/www/assets/js/client.js @@ -295,6 +295,14 @@ $("#chatline").keydown(function(ev) { $("#messagebuffer").mouseenter(function() { SCROLLCHAT = false; }); $("#messagebuffer").mouseleave(function() { SCROLLCHAT = true; }); +$("#clearplaylist").click(function() { + socket.emit("clearqueue"); +}); + +$("#shuffleplaylist").click(function() { + socket.emit("shufflequeue"); +}); + $("#getplaylist").click(function() { var callback = function(data) { socket.listeners("playlist").splice( diff --git a/www/index.html b/www/index.html index 54d0f538..2597fd0f 100644 --- a/www/index.html +++ b/www/index.html @@ -86,7 +86,11 @@ - +
+ + + +