From 9d445b8ffdf82a3a319ca7f9944ed29ca3d549ea Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 11 Sep 2013 22:16:56 -0500 Subject: [PATCH] Fix up voteskip a bit --- changelog | 9 +++++++++ lib/api.js | 2 +- lib/channel.js | 29 ++++++++++++++++++----------- lib/user.js | 7 +------ www/assets/js/util.js | 3 +++ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/changelog b/changelog index cbada241..f6cdde08 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,12 @@ +Wed Sep 11 22:13 2013 CDT + * lib/channel.js, lib/user.js: Remove "afkers" array, replace afkcount + with a function that calculates how many users are eligible to + voteskip (not AFK + have permission). Check permission before + allowing voteskip + * lib/api.js: Replace "afkcount" in /api/channels/:channel with + voteskip_eligible + * www/assets/js/util.js: Add a permissions option for voteskip + Wed Sep 11 20:19 2013 CDT * lib/poll.js: Add support for hidden polls * lib/channel.js: Check permissions for viewing hidden polls diff --git a/lib/api.js b/lib/api.js index 077ef1e3..d6847e3d 100644 --- a/lib/api.js +++ b/lib/api.js @@ -38,7 +38,7 @@ module.exports = function (Server) { channel.playlist.current.media.pack() : {}; data.usercount = channel.users.length; - data.afkcount = channel.afkers.length; + data.voteskip_eligible = channel.calcVoteskipMax(); data.users = []; for(var i in channel.users) if(channel.users[i].name !== "") diff --git a/lib/channel.js b/lib/channel.js index a0129e63..f61e752d 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -34,7 +34,6 @@ var Channel = function(name, Server) { // Initialize defaults self.registered = false; self.users = []; - self.afkers = []; self.playlist = new Playlist(self); self.position = -1; self.drinks = 0; @@ -67,6 +66,7 @@ var Channel = function(name, Server) { pollctl: 1.5, pollvote: -1, viewhiddenpoll: 1.5, + voteskip: -1, mute: 1.5, kick: 1.5, ban: 2, @@ -842,9 +842,6 @@ Channel.prototype.userLeave = function(user) { var idx = this.users.indexOf(user); if(idx >= 0 && idx < this.users.length) this.users.splice(idx, 1); - idx = this.afkers.indexOf(user.name.toLowerCase()); - if(idx >= 0 && idx < this.afkers.length) - this.afkers.splice(idx, 1); this.checkVoteskipPass(); this.broadcastUsercount(); if(user.name != "") { @@ -1194,9 +1191,21 @@ Channel.prototype.broadcastChatFilters = function() { } } +Channel.prototype.calcVoteskipMax = function () { + var self = this; + // good ol' map-reduce + return self.users.map(function (u) { + if (!self.hasPermission(u, "voteskip")) + return 0; + return u.meta.afk ? 0 : 1; + }).reduce(function (a, b) { + return a + b; + }); +}; + Channel.prototype.broadcastVoteskipUpdate = function() { var amt = this.voteskip ? this.voteskip.counts[0] : 0; - var count = this.users.length - this.afkers.length; + var count = this.calcVoteskipMax(); var need = this.voteskip ? Math.ceil(count * this.opts.voteskip_ratio) : 0; for(var i = 0; i < this.users.length; i++) { if(Rank.hasPermission(this.users[i], "seeVoteskip") || @@ -1740,6 +1749,9 @@ Channel.prototype.tryVoteskip = function(user) { if(!this.opts.allow_voteskip) { return; } + + if(!this.hasPermission(user, "voteskip")) + return; // Voteskip = auto-unafk user.setAFK(false); user.autoAFK(); @@ -1758,13 +1770,8 @@ Channel.prototype.checkVoteskipPass = function () { if(!this.voteskip) return false; - var count = this.users.length - this.afkers.length; + var count = this.calcVoteskipMax(); var need = Math.ceil(count * this.opts.voteskip_ratio); - if(this.server.cfg["debug"]) { - console.log("afkers=", this.afkers.length); - console.log("users =", this.users.length); - console.log("DBG", this.voteskip.counts[0], "/", need); - } if(this.voteskip.counts[0] >= need) this.playNext(); diff --git a/lib/user.js b/lib/user.js index 880b9828..6cb3b8e0 100644 --- a/lib/user.js +++ b/lib/user.js @@ -89,14 +89,9 @@ User.prototype.setAFK = function (afk) { var chan = this.channel; this.meta.afk = afk; if(afk) { - if(chan.afkers.indexOf(this.name.toLowerCase()) == -1) - chan.afkers.push(this.name.toLowerCase()); if(chan.voteskip) chan.voteskip.unvote(this.ip); - } - else { - if(chan.afkers.indexOf(this.name.toLowerCase()) != -1) - chan.afkers.splice(chan.afkers.indexOf(this.name.toLowerCase()), 1); + } else { this.autoAFK(); } chan.checkVoteskipPass(); diff --git a/www/assets/js/util.js b/www/assets/js/util.js index 9fb8b4fe..e9a6c36b 100644 --- a/www/assets/js/util.js +++ b/www/assets/js/util.js @@ -1093,6 +1093,8 @@ function handlePermissionChange() { setVisible("#newpollbtn", hasPermission("pollctl")); + $("#voteskip").attr("disabled", !hasPermission("voteskip") || + !CHANNEL.opts.allow_voteskip); $("#pollwrap .active").find(".btn-danger").remove(); if(hasPermission("pollctl")) { @@ -1598,6 +1600,7 @@ function genPermissionsEditor() { makeOption("Open/Close poll", "pollctl", modleader, CHANNEL.perms.pollctl+""); makeOption("Vote", "pollvote", standard, CHANNEL.perms.pollvote+""); makeOption("View hidden poll results", "viewhiddenpoll", standard, CHANNEL.perms.viewhiddenpoll+""); + makeOption("Voteskip", "voteskip", standard, CHANNEL.perms.voteskip+""); addDivider("Moderation"); makeOption("Mute users", "mute", modleader, CHANNEL.perms.mute+"");