3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

Underscorise poll. Unfortunately some stuff went missing in the switch-over. Whoops. Oh I also fixed it. [#81]

This commit is contained in:
reality 2013-01-12 23:31:53 +00:00
parent f36e6c781b
commit 6b8b617e71
2 changed files with 132 additions and 158 deletions

View File

@ -1,3 +1,4 @@
{ {
"help": "http://github.com/reality/depressionbot/blob/master/modules/poll/README.md" "help": "http://github.com/reality/depressionbot/blob/master/modules/poll/README.md",
"dbKeys": [ "polls" ]
} }

View File

@ -1,68 +1,63 @@
var _ = require('underscore')._;
var poll = function(dbot) { var poll = function(dbot) {
var polls = dbot.db.polls; var polls = dbot.db.polls;
var commands = { var commands = {
'~newpoll': function(event) { '~newpoll': function(event) {
var av = event.input[1] != undefined; var name = event.input[1],
var name = event.input[2]; options = event.input[2].split(','),
var options = event.input[3].split(','); description = event.input[3];
var description = event.input[4];
if(name === undefined || name === 'help') { if(_.has(polls, name)) {
event.reply(dbot.t('newpoll_usage')); event.reply(dbot.t('poll_exists', { 'name': name }));
} else { } else {
if(polls.hasOwnProperty(name)) { polls[name] = {
event.reply(dbot.t('poll_exists', {'name': name})); 'name': name,
} else { 'description': description,
if(av) { 'owner': event.user,
polls[name] = { 'votes': {},
'av': av, 'votees': {}
'name': name, };
'description': description, for(var i=0;i<options.length;i++) {
'owner': event.user, polls[name]['votes'][options[i]] = 0;
'votes': {},
'options': []
};
for(var i=0;i<options.length;i++) {
polls[name].options.push(options[i]);
}
} else {
polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
'votes': {},
'votees': {}
};
for(var i=0;i<options.length;i++) {
polls[name]['votes'][options[i]] = 0;
}
}
event.reply(dbot.t('poll_created', {'name': name, 'description': description,
'url': dbot.t('url', {'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort, 'path': 'polls/' + name})}));
} }
event.reply(dbot.t('poll_created', {
'name': name,
'description': description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'polls/' + name
})
}));
} }
}, },
'~addoption': function(event) { '~addoption': function(event) {
var name = event.input[1]; var name = event.input[1],
var option = event.input[2]; option = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].owner === event.user) { if(polls[name].owner === event.user) {
if(!polls[name].votes.hasOwnProperty(name)) { if(!_.has(polls[name].votes, name)) {
polls[name]['votes'][option] = 0; polls[name]['votes'][option] = 0;
event.reply(dbot.t('option_added', {'user': event.user, event.reply(dbot.t('option_added', {
'name': name, 'option': option})); 'user': event.user,
'name': name,
'option': option
}));
} else { } else {
event.reply(dbot.t('option_exists', {'option': option, event.reply(dbot.t('option_exists', {
'name': name, 'user': event.user})); 'option': option,
'name': name,
'user': event.user
}));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', {'user': event.user, event.reply(dbot.t('not_poll_owner', {
'name': name})); 'user': event.user,
'name': name
}));
} }
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
@ -70,147 +65,114 @@ var poll = function(dbot) {
}, },
'~rmoption': function(event) { '~rmoption': function(event) {
var name = event.input[1]; var name = event.input[1],
var option = event.input[2]; option = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].owner === event.user) { if(polls[name].owner === event.user) {
if(polls[name].votes.hasOwnProperty(option)) { if(_.has(polls[name].votes, option)) {
delete polls[name]['votes'][option]; delete polls[name]['votes'][option];
event.reply(dbot.t('option_removed', {'user': event.user, event.reply(dbot.t('option_removed', {
'name': name, 'option': option})); 'user': event.user,
'name': name,
'option': option
}));
} else { } else {
event.reply(dbot.t('invalid_vote', {'vote': option})); event.reply(dbot.t('invalid_vote', { 'vote': option }));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', {'name': name})); event.reply(dbot.t('not_poll_owner', { 'name': name }));
} }
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~vote': function(event) { '~vote': function(event) {
var name = event.input[1]; var name = event.input[1],
var vote = event.input[2]; vote = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].av) { if(_.has(polls[name].votes, vote)) {
var prefs = vote.split(','); if(_.has(polls[name].votees, event.user)) {
prefs = prefs.uniq();
var valid = true;
prefs.each(function(pref) {
valid = valid && polls[name].options.indexOf(pref) != -1;
});
if(valid){
if(polls[name].votes.hasOwnProperty(event.user)) {
polls[name].votes[event.user] = prefs;
event.reply(dbot.t('av_changed_vote', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
} else {
polls[name].votes[event.user] = prefs;
event.reply(dbot.t('av_voted', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
}
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
} else {
if(polls[name].votes.hasOwnProperty(vote)) {
if(polls[name].votees.hasOwnProperty(event.user)) {
var oldVote = polls[name].votees[event.user]; var oldVote = polls[name].votees[event.user];
polls[name].votes[oldVote]--; polls[name].votes[oldVote]--;
polls[name].votes[vote]++; polls[name].votes[vote]++;
polls[name].votees[event.user] = vote; polls[name].votees[event.user] = vote;
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote], 'user': event.user})); event.reply(dbot.t('changed_vote', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} else { } else {
polls[name].votes[vote]++; polls[name].votes[vote]++;
polls[name].votees[event.user] = vote; polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name, event.reply(dbot.t('voted', {
'count': polls[name].votes[vote], 'user': event.user})); 'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} }
} else { } else {
event.reply(dbot.t('invalid_vote', {'vote': vote})); event.reply(dbot.t('invalid_vote', { 'vote': vote }));
} }
}
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~pdesc': function(event) { '~pdesc': function(event) {
var name = event.input[1]; var name = event.input[1];
if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_describe', {'name': name, 'description': polls[name].description, if(_.has(polls, name)) {
'url': dbot.t('url', {'host': dbot.config.web.webHost, 'port': event.reply(dbot.t('poll_describe', {
dbot.config.web.webPort, 'path': 'polls/' + name})})); 'name': name,
'description': polls[name].description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'polls/' + name
})
}));
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~count': function(event) { '~count': function(event) {
var name = event.input[1]; var name = event.input[1];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
var order; var order;
if(polls[name].av) { var votesArr = [];
var finished = false;
var rounds = [];
var eliminated = [];
var voted;
for(var roundn = 0; roundn < polls[name].options.length; roundn++) {
var roundLoser;
// Populate candidates for this round
rounds[roundn] = {};
polls[name].options.each(function (option) {
if(eliminated.indexOf(option) == -1)
rounds[roundn][option] = 0;
});
// Count votes
polls[name].votes.withAll(function (name, vote) {
voted = false;
vote.each(function (pref) {
if(!voted && rounds[roundn].hasOwnProperty(pref)) {
rounds[roundn][pref]++;
voted = true;
}
});
});
// Find the loser
var min = polls[name].votes.length() + 1;
rounds[roundn].withAll(function (option, count) {
if(count < min) {
roundLoser = option;
min = count;
}
});
// Eliminate loser
eliminated.push(roundLoser);
}
order = eliminated.reverse().join(', ')
} else {
var votesArr = [];
polls[name].votes.withAll(function(option, count) {
votesArr.push([option, count]);
});
votesArr = votesArr.sort(function(a, b) { return b[1] - a[1]; }); var order = _.chain(polls[name].votes)
.pairs()
order = votesArr.map(function(vote) { return vote[0]; }); .sortBy(function(option) { return option[1] })
.reverse()
.value();
var orderString = "";
for(var i=0;i<order.length;i++) {
orderString += order[i][0] +
" (" + order[i][1] + "), ";
} }
event.reply(dbot.t('count', {'poll': name, 'description': polls[name].description, 'places': order})); orderString = orderString.slice(0, -2);
event.reply(dbot.t('count', {
'poll': name,
'description': polls[name].description,
'places': orderString
}));
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
} }
} }
}; };
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) options=([^ ]+) (.+)/, 5]; commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3]; commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3]; commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3]; commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
@ -221,24 +183,35 @@ var poll = function(dbot) {
// Shows the results of a poll // Shows the results of a poll
'/polls/:key': function(req, res) { '/polls/:key': function(req, res) {
var key = req.params.key.toLowerCase(); var key = req.params.key.toLowerCase();
if(dbot.db.polls.hasOwnProperty(key) && dbot.db.polls[key].hasOwnProperty('description')) { if(_.has(dbot.db.polls, key)) {
// tally the votes var totalVotes = _.reduce(dbot.db.polls[key].votes,
var totalVotes = 0; function(memo, option) {
for( var v in dbot.db.polls[key].votes ) { return memo += option;
var N = Number(dbot.db.polls[key].votes[v]); }, 0);
if( !isNaN(N) ) { res.render('polls', {
totalVotes += N; 'name': dbot.config.name,
} 'description': dbot.db.polls[key].description,
} 'votees': Object.keys(dbot.db.polls[key].votees),
res.render('polls', { 'name': dbot.config.name, 'description': dbot.db.polls[key].description, 'votees': Object.keys(dbot.db.polls[key].votees), 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } }); 'options': dbot.db.polls[key].votes,
locals: {
'totalVotes': totalVotes,
'url_regex': RegExp.prototype.url_regex()
}
});
} else { } else {
res.render('error', { 'name': dbot.config.name, 'message': 'No polls under that key.' }); res.render('error', {
'name': dbot.config.name,
'message': 'No polls under that key.'
});
} }
}, },
// Lists all of the polls // Lists all of the polls
'/polls': function(req, res) { '/polls': function(req, res) {
res.render('polllist', { 'name': dbot.config.name, 'polllist': Object.keys(dbot.db.polls) }); res.render('polllist', {
'name': dbot.config.name,
'polllist': Object.keys(dbot.db.polls)
});
}, },
}; };