Fix up voteskip a bit

This commit is contained in:
calzoneman 2013-09-11 22:16:56 -05:00
parent 7e9673425d
commit 9d445b8ffd
5 changed files with 32 additions and 18 deletions

View File

@ -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

View File

@ -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 !== "")

View File

@ -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();

View File

@ -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();

View File

@ -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+"");