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,
'owner': event.user,
'votes': {},
'votees': []
'votees': {}
};
for(var i=0;i<options.length;i++) {
@ -37,16 +37,22 @@ var poll = function(dbot) {
var vote = event.input[2];
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].votees.push(event.user);
event.reply(dbot.t('voted', {'vote': vote}));
polls[name].votees[event.user] = vote;
event.reply(dbot.t('changed_voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote]}));
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
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 {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));

View File

@ -51,7 +51,7 @@ var webInterface = function(dbot) {
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 {
res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' });
}

View File

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