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 {
polls[name] = { if(av) {
'name': name, polls[name] = {
'description': description, 'av': av,
'owner': event.user, 'name': name,
'votes': {}, 'description': description,
'votees': {} 'owner': event.user,
}; 'votes': {},
};
} else {
polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
'votes': {},
'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,22 +95,41 @@ 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].votes.hasOwnProperty(vote)) { if(polls[name].av) {
if(polls[name].votees.hasOwnProperty(event.user)) { var prefs = vote.split(',');
var oldVote = polls[name].votees[event.user]; var valid = true;
polls[name].votes[oldVote]--;
polls[name].votes[vote]++; prefs.each(function(pref) {
polls[name].votees[event.user] = vote; valid = valid && polls[name].votes.hasOwnProperty(pref);
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name, });
'count': polls[name].votes[vote], 'user': event.user})); 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 { } else {
polls[name].votes[vote]++; event.reply(dbot.t('invalid_vote', {'vote': vote}));
polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote], 'user': event.user}));
} }
} else { } else {
event.reply(dbot.t('invalid_vote', {'vote': vote})); if(polls[name].votes.hasOwnProperty(vote)) {
if(polls[name].votees.hasOwnProperty(event.user)) {
var oldVote = polls[name].votees[event.user];
polls[name].votes[oldVote]--;
polls[name].votes[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}));
} else {
polls[name].votes[vote]++;
polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote], 'user': event.user}));
}
} else {
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}."
} }
} }