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 commands = {
'~newpoll': function(event) {
var name = event.input[1];
var options = event.input[2].split(',');
var description = event.input[3];
var av = event.input[1] != undefined;
var name = event.input[2];
var options = event.input[3].split(',');
var description = event.input[4];
if(name === undefined || name === 'help') {
event.reply(dbot.t('newpoll_usage'));
@ -16,13 +17,24 @@ var poll = function(dbot) {
if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_exists', {'name': name}));
} else {
polls[name] = {
'name': name,
'description': description,
'owner': event.user,
'votes': {},
'votees': {}
};
if(av) {
polls[name] = {
'av': av,
'name': name,
'description': description,
'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++) {
polls[name]['votes'][options[i]] = 0;
@ -83,22 +95,41 @@ var poll = function(dbot) {
var vote = event.input[2];
if(polls.hasOwnProperty(name)) {
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}));
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 {
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}));
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
} 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 {
event.reply(dbot.t('poll_unexistent', {'name': name}));
@ -113,8 +144,12 @@ var poll = function(dbot) {
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['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];

View File

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