Add support for hidden polls

This commit is contained in:
calzoneman 2013-09-11 20:22:00 -05:00
parent 020ceecd40
commit 7e9673425d
5 changed files with 66 additions and 12 deletions

View file

@ -66,6 +66,7 @@ var Channel = function(name, Server) {
playlistclear: 2,
pollctl: 1.5,
pollvote: -1,
viewhiddenpoll: 1.5,
mute: 1.5,
kick: 1.5,
ban: 2,
@ -1095,11 +1096,29 @@ Channel.prototype.broadcastUserUpdate = function(user) {
}
Channel.prototype.broadcastPoll = function() {
this.sendAll("newPoll", this.poll.packUpdate());
var self = this;
var unhidden = this.poll.packUpdate(true);
var hidden = this.poll.packUpdate(false);
this.users.forEach(function (u) {
if (self.hasPermission(u, "viewhiddenpoll"))
u.socket.emit("newPoll", unhidden);
else
u.socket.emit("newPoll", hidden);
});
}
Channel.prototype.broadcastPollUpdate = function() {
this.sendAll("updatePoll", this.poll.packUpdate());
var self = this;
var unhidden = this.poll.packUpdate(true);
var hidden = this.poll.packUpdate(false);
this.users.forEach(function (u) {
if (self.hasPermission(u, "viewhiddenpoll"))
u.socket.emit("updatePoll", unhidden);
else
u.socket.emit("updatePoll", hidden);
});
}
Channel.prototype.broadcastPollClose = function() {
@ -1679,7 +1698,8 @@ Channel.prototype.tryOpenPoll = function(user, data) {
return;
}
var poll = new Poll(user.name, data.title, data.opts);
var obscured = (data.obscured === true);
var poll = new Poll(user.name, data.title, data.opts, obscured);
this.poll = poll;
this.broadcastPoll();
this.logger.log("*** " + user.name + " Opened Poll: '" + poll.title + "'");
@ -1691,6 +1711,10 @@ Channel.prototype.tryClosePoll = function(user) {
}
if(this.poll) {
if (this.poll.obscured) {
this.poll.obscured = false;
this.broadcastPollUpdate();
}
this.logger.log("*** " + user.name + " closed the active poll");
this.poll = false;
this.broadcastPollClose();

View file

@ -73,7 +73,10 @@ function handle(chan, user, msg, data) {
handleUnban(chan, user, msg.substring(7).split(" "));
}
else if(msg.indexOf("/poll ") == 0) {
handlePoll(chan, user, msg.substring(6));
handlePoll(chan, user, msg.substring(6), false);
}
else if(msg.indexOf("/hpoll ") == 0) {
handlePoll(chan, user, msg.substring(6), true);
}
else if(msg.indexOf("/d") == 0 && msg.length > 2 &&
msg[2].match(/[-0-9 ]/)) {
@ -195,12 +198,12 @@ function handleUnban(chan, user, args) {
}
}
function handlePoll(chan, user, msg) {
function handlePoll(chan, user, msg, hidden) {
if(chan.hasPermission(user, "pollctl")) {
var args = msg.split(",");
var title = args[0];
args.splice(0, 1);
var poll = new Poll(user.name, title, args);
var poll = new Poll(user.name, title, args, hidden === true);
chan.poll = poll;
chan.broadcastPoll();
chan.logger.log("*** " + user.name + " Opened Poll: '" + poll.title + "'");

View file

@ -9,10 +9,11 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Poll = function(initiator, title, options) {
var Poll = function(initiator, title, options, obscured) {
this.initiator = initiator;
this.title = title;
this.options = options;
this.obscured = obscured || false;
this.counts = new Array(options.length);
for(var i = 0; i < this.counts.length; i++) {
this.counts[i] = 0;
@ -34,13 +35,22 @@ Poll.prototype.unvote = function(ip) {
}
}
Poll.prototype.packUpdate = function() {
return {
Poll.prototype.packUpdate = function (showhidden) {
var counts = Array.prototype.slice.call(this.counts);
if (this.obscured) {
for(var i = 0; i < counts.length; i++) {
if (!showhidden)
counts[i] = "";
counts[i] += "?";
}
}
var packed = {
title: this.title,
options: this.options,
counts: this.counts,
counts: counts,
initiator: this.initiator
}
};
return packed;
}
exports.Poll = Poll;