2014-05-21 02:30:14 +00:00
|
|
|
var ChannelModule = require("./module");
|
|
|
|
var Flags = require("../flags");
|
|
|
|
var Poll = require("../poll").Poll;
|
|
|
|
|
|
|
|
function VoteskipModule(channel) {
|
|
|
|
ChannelModule.apply(this, arguments);
|
|
|
|
|
|
|
|
this.poll = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VoteskipModule.prototype = Object.create(ChannelModule.prototype);
|
|
|
|
|
|
|
|
VoteskipModule.prototype.onUserPostJoin = function (user) {
|
|
|
|
user.socket.on("voteskip", this.handleVoteskip.bind(this, user));
|
|
|
|
};
|
|
|
|
|
2014-05-31 14:31:45 +00:00
|
|
|
VoteskipModule.prototype.onUserPart = function(user) {
|
|
|
|
if (!this.poll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:42:13 +00:00
|
|
|
this.unvote(user.realip);
|
2014-05-31 14:31:45 +00:00
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
2014-05-21 02:30:14 +00:00
|
|
|
VoteskipModule.prototype.handleVoteskip = function (user) {
|
|
|
|
if (!this.channel.modules.options.get("allow_voteskip")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.channel.modules.playlist) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.channel.modules.permissions.canVoteskip(user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.poll) {
|
|
|
|
this.poll = new Poll("[server]", "voteskip", ["skip"], false);
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:42:13 +00:00
|
|
|
this.poll.vote(user.realip, 0);
|
2014-05-21 02:30:14 +00:00
|
|
|
|
|
|
|
var title = "";
|
|
|
|
if (this.channel.modules.playlist.current) {
|
2014-05-23 02:51:35 +00:00
|
|
|
title = " " + this.channel.modules.playlist.current.media.title;
|
2014-05-21 02:30:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 01:13:22 +00:00
|
|
|
var name = user.getName() || "(anonymous)";
|
2014-05-21 02:30:14 +00:00
|
|
|
|
|
|
|
this.channel.logger.log("[playlist] " + name + " voteskipped " + title);
|
2014-06-16 01:13:22 +00:00
|
|
|
user.setAFK(false);
|
2014-05-21 02:30:14 +00:00
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
2014-05-31 14:31:45 +00:00
|
|
|
VoteskipModule.prototype.unvote = function(ip) {
|
|
|
|
if (!this.poll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.poll.unvote(ip);
|
|
|
|
};
|
|
|
|
|
2014-05-21 02:30:14 +00:00
|
|
|
VoteskipModule.prototype.update = function () {
|
|
|
|
if (!this.channel.modules.options.get("allow_voteskip")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.poll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.channel.modules.playlist.meta.count === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var max = this.calcVoteskipMax();
|
|
|
|
var need = Math.ceil(max * this.channel.modules.options.get("voteskip_ratio"));
|
|
|
|
if (this.poll.counts[0] >= need) {
|
|
|
|
this.channel.logger.log("[playlist] Voteskip passed.");
|
|
|
|
this.channel.modules.playlist._playNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendVoteskipData(this.channel.users);
|
|
|
|
};
|
|
|
|
|
|
|
|
VoteskipModule.prototype.sendVoteskipData = function (users) {
|
|
|
|
var max = this.calcVoteskipMax();
|
|
|
|
var data = {
|
|
|
|
count: this.poll ? this.poll.counts[0] : 0,
|
|
|
|
need: this.poll ? Math.ceil(max * this.channel.modules.options.get("voteskip_ratio"))
|
|
|
|
: 0
|
|
|
|
};
|
|
|
|
|
2014-09-07 03:59:28 +00:00
|
|
|
var perms = this.channel.modules.permissions;
|
|
|
|
|
2014-05-21 02:30:14 +00:00
|
|
|
users.forEach(function (u) {
|
2014-09-07 03:59:28 +00:00
|
|
|
if (perms.canSeeVoteskipResults(u)) {
|
2014-05-21 02:30:14 +00:00
|
|
|
u.socket.emit("voteskip", data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
VoteskipModule.prototype.calcVoteskipMax = function () {
|
|
|
|
var perms = this.channel.modules.permissions;
|
|
|
|
return this.channel.users.map(function (u) {
|
|
|
|
if (!perms.canVoteskip(u)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.is(Flags.U_AFK) ? 0 : 1;
|
|
|
|
}).reduce(function (a, b) {
|
|
|
|
return a + b;
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
VoteskipModule.prototype.onMediaChange = function (data) {
|
|
|
|
this.poll = false;
|
|
|
|
this.sendVoteskipData(this.channel.users);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = VoteskipModule;
|