votees stores vote, can change vote

This commit is contained in:
Luke Slater 2012-06-15 17:20:05 +01:00
parent 800faf5a88
commit 2e27d7dece
3 changed files with 18 additions and 12 deletions

View File

@ -20,7 +20,7 @@ var poll = function(dbot) {
'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++) {
@ -37,17 +37,23 @@ 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].votees.include(event.user)) {
event.reply(dbot.t('alread_voted'));
} else {
if(polls[name].votes.hasOwnProperty(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].votes[vote]++;
polls[name].votees.push(event.user); polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote})); event.reply(dbot.t('changed_voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote]}));
} 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]}));
}
} 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}));
} }

View File

@ -51,7 +51,7 @@ var webInterface = function(dbot) {
totalVotes += N; totalVotes += N;
} }
} }
res.render('polls', { 'name': dbot.name, 'description': dbot.db.polls[key].description, 'votees': dbot.db.polls[key].votees, 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } }); res.render('polls', { 'name': dbot.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() } });
} else { } else {
res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' }); res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' });
} }

View File

@ -205,11 +205,11 @@
"poll_created": { "poll_created": {
"english": "Poll '{name}' created ({description}). Cast thy votations!" "english": "Poll '{name}' created ({description}). Cast thy votations!"
}, },
"already_voted": { "changed_vote": {
"english": "You've already voted in this poll!" "english": "Changed vote in {poll} to '{vote}' ({count})"
}, },
"voted": { "voted": {
"english": "Voted for {vote}" "english": "Voted for '{vote}' in {poll} ({count})"
}, },
"invalid_vote": { "invalid_vote": {
"english": "Invalid vote: {vote}" "english": "Invalid vote: {vote}"