Added voting on AV polls. No counting yet.

This commit is contained in:
Joe MacMahon 2012-06-18 20:11:54 +01:00
parent bd91ea1171
commit 52d8136e9d
2 changed files with 63 additions and 24 deletions

View File

@ -6,9 +6,10 @@ 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 name = event.input[1]; var av = event.input[1] != undefined;
var options = event.input[2].split(','); var name = event.input[2];
var description = event.input[3]; var options = event.input[3].split(',');
var description = event.input[4];
if(name === undefined || name === 'help') { if(name === undefined || name === 'help') {
event.reply(dbot.t('newpoll_usage')); event.reply(dbot.t('newpoll_usage'));
@ -16,13 +17,24 @@ var poll = function(dbot) {
if(polls.hasOwnProperty(name)) { if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_exists', {'name': name})); event.reply(dbot.t('poll_exists', {'name': name}));
} else { } else {
if(av) {
polls[name] = { polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
'votes': {},
};
} else {
polls[name] = {
'av': av,
'name': name, 'name': name,
'description': description, 'description': description,
'owner': event.user, 'owner': event.user,
'votes': {}, 'votes': {},
'votees': {} 'votees': {}
}; };
}
for(var i=0;i<options.length;i++) { for(var i=0;i<options.length;i++) {
polls[name]['votes'][options[i]] = 0; polls[name]['votes'][options[i]] = 0;
@ -83,6 +95,24 @@ var poll = function(dbot) {
var vote = event.input[2]; var vote = event.input[2];
if(polls.hasOwnProperty(name)) { if(polls.hasOwnProperty(name)) {
if(polls[name].av) {
var prefs = vote.split(',');
var valid = true;
prefs.each(function(pref) {
valid = valid && polls[name].votes.hasOwnProperty(pref);
});
if(valid){
if(polls[name].votes.hasOwnProperty(event.user)) {
polls[name].votes[event.user] = prefs;
} else {
polls[name].votes[event.user] = prefs;
event.reply(dbot.t('av_voted', {'vote': vote, 'poll': name, 'user': event.user}));
}
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
} else {
if(polls[name].votes.hasOwnProperty(vote)) { if(polls[name].votes.hasOwnProperty(vote)) {
if(polls[name].votees.hasOwnProperty(event.user)) { if(polls[name].votees.hasOwnProperty(event.user)) {
var oldVote = polls[name].votees[event.user]; var oldVote = polls[name].votees[event.user];
@ -100,6 +130,7 @@ var poll = function(dbot) {
} 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}));
} }
@ -113,8 +144,12 @@ var poll = function(dbot) {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
} }
} }
'~count': function(event) {
}
}; };
commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4]; commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) \[options=([^ ]+)\] (.+)/, 5];
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];

View File

@ -245,5 +245,9 @@
"option_removed": { "option_removed": {
"english": "{user}: '{option}' removed from '{name}'", "english": "{user}: '{option}' removed from '{name}'",
"spanish" : "{user}: '{option}' eliminado de '{name}'" "spanish" : "{user}: '{option}' eliminado de '{name}'"
},
"av_voted": {
"english": "{user} voted '{vote}' in {poll}.",
"spanish": "{user} votó '{vote}' en {poll}."
} }
} }