Refactor polls
This commit is contained in:
parent
c290f9fcca
commit
f84892dc6a
5 changed files with 390 additions and 86 deletions
|
|
@ -42,12 +42,7 @@ PollModule.prototype.unload = function () {
|
|||
PollModule.prototype.load = function (data) {
|
||||
if ("poll" in data) {
|
||||
if (data.poll !== null) {
|
||||
this.poll = new Poll(data.poll.initiator, "", [], data.poll.obscured);
|
||||
this.poll.title = data.poll.title;
|
||||
this.poll.options = data.poll.options;
|
||||
this.poll.counts = data.poll.counts;
|
||||
this.poll.votes = data.poll.votes;
|
||||
this.poll.timestamp = data.poll.timestamp;
|
||||
this.poll = Poll.fromChannelData(data.poll);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,15 +55,7 @@ PollModule.prototype.save = function (data) {
|
|||
return;
|
||||
}
|
||||
|
||||
data.poll = {
|
||||
title: this.poll.title,
|
||||
initiator: this.poll.initiator,
|
||||
options: this.poll.options,
|
||||
counts: this.poll.counts,
|
||||
votes: this.poll.votes,
|
||||
obscured: this.poll.obscured,
|
||||
timestamp: this.poll.timestamp
|
||||
};
|
||||
data.poll = this.poll.toChannelData();
|
||||
};
|
||||
|
||||
PollModule.prototype.onUserPostJoin = function (user) {
|
||||
|
|
@ -97,8 +84,7 @@ PollModule.prototype.addUserToPollRoom = function (user) {
|
|||
};
|
||||
|
||||
PollModule.prototype.onUserPart = function(user) {
|
||||
if (this.poll) {
|
||||
this.poll.unvote(user.realip);
|
||||
if (this.poll && this.poll.uncountVote(user.realip)) {
|
||||
this.broadcastPoll(false);
|
||||
}
|
||||
};
|
||||
|
|
@ -112,10 +98,10 @@ PollModule.prototype.sendPoll = function (user) {
|
|||
|
||||
user.socket.emit("closePoll");
|
||||
if (perms.canViewHiddenPoll(user)) {
|
||||
var unobscured = this.poll.packUpdate(true);
|
||||
var unobscured = this.poll.toUpdateFrame(true);
|
||||
user.socket.emit("newPoll", unobscured);
|
||||
} else {
|
||||
var obscured = this.poll.packUpdate(false);
|
||||
var obscured = this.poll.toUpdateFrame(false);
|
||||
user.socket.emit("newPoll", obscured);
|
||||
}
|
||||
};
|
||||
|
|
@ -125,8 +111,8 @@ PollModule.prototype.broadcastPoll = function (isNewPoll) {
|
|||
return;
|
||||
}
|
||||
|
||||
var obscured = this.poll.packUpdate(false);
|
||||
var unobscured = this.poll.packUpdate(true);
|
||||
var obscured = this.poll.toUpdateFrame(false);
|
||||
var unobscured = this.poll.toUpdateFrame(true);
|
||||
|
||||
const event = isNewPoll ? "newPoll" : "updatePoll";
|
||||
if (isNewPoll) {
|
||||
|
|
@ -197,7 +183,7 @@ PollModule.prototype.handleNewPoll = function (user, data, ack) {
|
|||
return;
|
||||
}
|
||||
|
||||
var poll = new Poll(user.getName(), data.title, data.opts, data.obscured);
|
||||
var poll = Poll.create(user.getName(), data.title, data.opts, { hideVotes: data.obscured });
|
||||
var self = this;
|
||||
if (data.hasOwnProperty("timeout")) {
|
||||
poll.timer = setTimeout(function () {
|
||||
|
|
@ -223,9 +209,10 @@ PollModule.prototype.handleVote = function (user, data) {
|
|||
}
|
||||
|
||||
if (this.poll) {
|
||||
this.poll.vote(user.realip, data.option);
|
||||
this.dirty = true;
|
||||
this.broadcastPoll(false);
|
||||
if (this.poll.countVote(user.realip, data.option)) {
|
||||
this.dirty = true;
|
||||
this.broadcastPoll(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -235,9 +222,9 @@ PollModule.prototype.handleClosePoll = function (user) {
|
|||
}
|
||||
|
||||
if (this.poll) {
|
||||
if (this.poll.obscured) {
|
||||
this.poll.obscured = false;
|
||||
this.channel.broadcastAll("updatePoll", this.poll.packUpdate(true));
|
||||
if (this.poll.hideVotes) {
|
||||
this.poll.hideVotes = false;
|
||||
this.channel.broadcastAll("updatePoll", this.poll.toUpdateFrame(true));
|
||||
}
|
||||
|
||||
if (this.poll.timer) {
|
||||
|
|
@ -270,7 +257,7 @@ PollModule.prototype.handlePollCmd = function (obscured, user, msg, _meta) {
|
|||
return;
|
||||
}
|
||||
|
||||
var poll = new Poll(user.getName(), title, args, obscured);
|
||||
var poll = Poll.create(user.getName(), title, args, { hideVotes: obscured });
|
||||
this.poll = poll;
|
||||
this.dirty = true;
|
||||
this.broadcastPoll(true);
|
||||
|
|
|
|||
|
|
@ -37,10 +37,10 @@ VoteskipModule.prototype.handleVoteskip = function (user) {
|
|||
}
|
||||
|
||||
if (!this.poll) {
|
||||
this.poll = new Poll("[server]", "voteskip", ["skip"], false);
|
||||
this.poll = Poll.create("[server]", "voteskip", ["skip"]);
|
||||
}
|
||||
|
||||
if (!this.poll.vote(user.realip, 0)) {
|
||||
if (!this.poll.countVote(user.realip, 0)) {
|
||||
// Vote was already recorded for this IP, no update needed
|
||||
return;
|
||||
}
|
||||
|
|
@ -62,7 +62,7 @@ VoteskipModule.prototype.unvote = function(ip) {
|
|||
return;
|
||||
}
|
||||
|
||||
this.poll.unvote(ip);
|
||||
this.poll.uncountVote(ip);
|
||||
};
|
||||
|
||||
VoteskipModule.prototype.update = function () {
|
||||
|
|
@ -78,10 +78,11 @@ VoteskipModule.prototype.update = function () {
|
|||
return;
|
||||
}
|
||||
|
||||
const { counts } = this.poll.toUpdateFrame(false);
|
||||
const { total, eligible, noPermission, afk } = this.calcUsercounts();
|
||||
const need = Math.ceil(eligible * this.channel.modules.options.get("voteskip_ratio"));
|
||||
if (this.poll.counts[0] >= need) {
|
||||
const info = `${this.poll.counts[0]}/${eligible} skipped; ` +
|
||||
if (counts[0] >= need) {
|
||||
const info = `${counts[0]}/${eligible} skipped; ` +
|
||||
`eligible voters: ${eligible} = total (${total}) - AFK (${afk}) ` +
|
||||
`- no permission (${noPermission}); ` +
|
||||
`ratio = ${this.channel.modules.options.get("voteskip_ratio")}`;
|
||||
|
|
@ -107,11 +108,20 @@ VoteskipModule.prototype.update = function () {
|
|||
|
||||
VoteskipModule.prototype.sendVoteskipData = function (users) {
|
||||
const { eligible } = this.calcUsercounts();
|
||||
var data = {
|
||||
count: this.poll ? this.poll.counts[0] : 0,
|
||||
need: this.poll ? Math.ceil(eligible * this.channel.modules.options.get("voteskip_ratio"))
|
||||
: 0
|
||||
};
|
||||
let data;
|
||||
|
||||
if (this.poll) {
|
||||
const { counts } = this.poll.toUpdateFrame(false);
|
||||
data = {
|
||||
count: counts[0],
|
||||
need: Math.ceil(eligible * this.channel.modules.options.get("voteskip_ratio"))
|
||||
};
|
||||
} else {
|
||||
data = {
|
||||
count: 0,
|
||||
need: 0
|
||||
};
|
||||
}
|
||||
|
||||
var perms = this.channel.modules.permissions;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue