2012-06-13 22:11:00 +02:00
|
|
|
var poll = function(dbot) {
|
|
|
|
var polls = dbot.db.polls;
|
|
|
|
var commands = {
|
|
|
|
'~newpoll': function(event) {
|
2012-06-18 21:11:54 +02:00
|
|
|
var av = event.input[1] != undefined;
|
|
|
|
var name = event.input[2];
|
|
|
|
var options = event.input[3].split(',');
|
|
|
|
var description = event.input[4];
|
2012-06-15 00:32:16 +02:00
|
|
|
|
2012-06-13 22:11:00 +02:00
|
|
|
if(name === undefined || name === 'help') {
|
|
|
|
event.reply(dbot.t('newpoll_usage'));
|
|
|
|
} else {
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
2012-06-15 01:07:15 +02:00
|
|
|
event.reply(dbot.t('poll_exists', {'name': name}));
|
2012-06-13 22:11:00 +02:00
|
|
|
} else {
|
2012-06-18 21:11:54 +02:00
|
|
|
if(av) {
|
|
|
|
polls[name] = {
|
|
|
|
'av': av,
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'owner': event.user,
|
|
|
|
'votes': {},
|
2012-06-18 22:16:22 +02:00
|
|
|
'options': []
|
2012-06-18 21:11:54 +02:00
|
|
|
};
|
2012-06-18 22:16:22 +02:00
|
|
|
for(var i=0;i<options.length;i++) {
|
|
|
|
polls[name].options.push(options[i]);
|
|
|
|
}
|
2012-06-18 21:11:54 +02:00
|
|
|
} else {
|
|
|
|
polls[name] = {
|
|
|
|
'av': av,
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'owner': event.user,
|
|
|
|
'votes': {},
|
|
|
|
'votees': {}
|
|
|
|
};
|
2012-06-18 22:16:22 +02:00
|
|
|
for(var i=0;i<options.length;i++) {
|
|
|
|
polls[name]['votes'][options[i]] = 0;
|
|
|
|
}
|
2012-06-13 22:11:00 +02:00
|
|
|
}
|
2012-06-15 00:32:16 +02:00
|
|
|
|
2012-06-15 18:51:45 +02:00
|
|
|
event.reply(dbot.t('poll_created', {'name': name, 'description': description}) +
|
2012-11-29 15:45:51 +01:00
|
|
|
' - http://nourishedcloud.com:8080/polls/' + name);
|
2012-06-13 22:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-17 13:51:25 +02:00
|
|
|
'~addoption': function(event) {
|
|
|
|
var name = event.input[1];
|
|
|
|
var option = event.input[2];
|
|
|
|
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
|
|
|
if(polls[name].owner === event.user) {
|
|
|
|
if(!polls[name].votes.hasOwnProperty(name)) {
|
|
|
|
polls[name]['votes'][option] = 0;
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_added', {'user': event.user,
|
|
|
|
'name': name, 'option': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
} else {
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_exists', {'option': option,
|
|
|
|
'name': name, 'user': event.user}));
|
2012-06-17 13:51:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_poll_owner', {'user': event.user,
|
|
|
|
'name': name}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~rmoption': function(event) {
|
|
|
|
var name = event.input[1];
|
|
|
|
var option = event.input[2];
|
|
|
|
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
|
|
|
if(polls[name].owner === event.user) {
|
|
|
|
if(polls[name].votes.hasOwnProperty(option)) {
|
|
|
|
delete polls[name]['votes'][option];
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_removed', {'user': event.user,
|
|
|
|
'name': name, 'option': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
} else {
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('invalid_vote', {'vote': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_poll_owner', {'name': name}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-13 22:11:00 +02:00
|
|
|
'~vote': function(event) {
|
2012-06-15 00:32:16 +02:00
|
|
|
var name = event.input[1];
|
|
|
|
var vote = event.input[2];
|
2012-06-13 22:11:00 +02:00
|
|
|
|
2012-06-15 00:32:16 +02:00
|
|
|
if(polls.hasOwnProperty(name)) {
|
2012-06-18 21:11:54 +02:00
|
|
|
if(polls[name].av) {
|
|
|
|
var prefs = vote.split(',');
|
2012-06-19 15:44:50 +02:00
|
|
|
prefs = prefs.uniq();
|
2012-06-18 21:11:54 +02:00
|
|
|
var valid = true;
|
|
|
|
|
|
|
|
prefs.each(function(pref) {
|
2012-06-19 15:44:50 +02:00
|
|
|
valid = valid && polls[name].options.indexOf(pref) != -1;
|
2012-06-18 21:11:54 +02:00
|
|
|
});
|
|
|
|
if(valid){
|
|
|
|
if(polls[name].votes.hasOwnProperty(event.user)) {
|
|
|
|
polls[name].votes[event.user] = prefs;
|
2012-06-19 15:44:50 +02:00
|
|
|
event.reply(dbot.t('av_changed_vote', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
2012-06-18 21:11:54 +02:00
|
|
|
} else {
|
|
|
|
polls[name].votes[event.user] = prefs;
|
2012-06-19 15:44:50 +02:00
|
|
|
event.reply(dbot.t('av_voted', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
|
2012-06-18 21:11:54 +02:00
|
|
|
}
|
2012-06-15 00:32:16 +02:00
|
|
|
} else {
|
2012-06-18 21:11:54 +02:00
|
|
|
event.reply(dbot.t('invalid_vote', {'vote': vote}));
|
2012-06-15 00:32:16 +02:00
|
|
|
}
|
2012-06-15 18:20:05 +02:00
|
|
|
} else {
|
2012-06-18 21:11:54 +02:00
|
|
|
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}));
|
|
|
|
}
|
2012-06-15 00:32:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-15 01:07:15 +02:00
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
2012-06-15 00:32:16 +02:00
|
|
|
}
|
2012-06-13 22:11:00 +02:00
|
|
|
},
|
|
|
|
|
2012-06-15 18:51:45 +02:00
|
|
|
'~pdesc': function(event) {
|
2012-06-15 00:57:07 +02:00
|
|
|
var name = event.input[1];
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
2012-11-29 17:47:44 +01:00
|
|
|
event.reply(name + ': ' + polls[name].description + ' - http://nourishedcloud.com:8080/polls/' + name);
|
2012-06-15 00:57:07 +02:00
|
|
|
} else {
|
2012-06-15 01:07:15 +02:00
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
2012-06-15 00:57:07 +02:00
|
|
|
}
|
2012-06-18 22:16:22 +02:00
|
|
|
},
|
2012-06-18 21:11:54 +02:00
|
|
|
|
2012-06-20 00:39:26 +02:00
|
|
|
'~count': function(event) {
|
2012-06-18 22:16:22 +02:00
|
|
|
var name = event.input[1];
|
2012-06-18 21:11:54 +02:00
|
|
|
|
2012-06-18 22:16:22 +02:00
|
|
|
if(polls.hasOwnProperty(name)) {
|
2012-06-20 00:39:26 +02:00
|
|
|
var order;
|
2012-06-18 22:16:22 +02:00
|
|
|
if(polls[name].av) {
|
|
|
|
var finished = false;
|
|
|
|
var rounds = [];
|
|
|
|
var eliminated = [];
|
2012-06-19 15:44:50 +02:00
|
|
|
var voted;
|
2012-06-18 22:16:22 +02:00
|
|
|
|
2012-06-20 00:39:26 +02:00
|
|
|
for(var roundn = 0; roundn < polls[name].options.length; roundn++) {
|
2012-06-19 15:44:50 +02:00
|
|
|
var roundLoser;
|
|
|
|
|
2012-06-18 22:16:22 +02:00
|
|
|
// Populate candidates for this round
|
|
|
|
rounds[roundn] = {};
|
|
|
|
polls[name].options.each(function (option) {
|
2012-06-19 15:44:50 +02:00
|
|
|
if(eliminated.indexOf(option) == -1)
|
2012-06-18 22:16:22 +02:00
|
|
|
rounds[roundn][option] = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Count votes
|
2012-06-20 01:52:49 +02:00
|
|
|
polls[name].votes.withAll(function (name, vote) {
|
2012-06-19 15:44:50 +02:00
|
|
|
voted = false;
|
|
|
|
vote.each(function (pref) {
|
|
|
|
if(!voted && rounds[roundn].hasOwnProperty(pref)) {
|
|
|
|
rounds[roundn][pref]++;
|
|
|
|
voted = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-06-20 00:39:26 +02:00
|
|
|
// Find the loser
|
|
|
|
var min = polls[name].votes.length() + 1;
|
2012-06-20 01:52:49 +02:00
|
|
|
rounds[roundn].withAll(function (option, count) {
|
2012-06-19 15:44:50 +02:00
|
|
|
if(count < min) {
|
|
|
|
roundLoser = option;
|
|
|
|
min = count;
|
|
|
|
}
|
2012-06-18 22:16:22 +02:00
|
|
|
});
|
2012-06-19 15:44:50 +02:00
|
|
|
|
|
|
|
// Eliminate loser
|
|
|
|
eliminated.push(roundLoser);
|
2012-06-18 22:16:22 +02:00
|
|
|
}
|
2012-06-20 00:39:26 +02:00
|
|
|
order = eliminated.reverse().join(', ')
|
2012-06-18 22:16:22 +02:00
|
|
|
} else {
|
2012-06-20 00:39:26 +02:00
|
|
|
var votesArr = [];
|
2012-06-20 01:52:49 +02:00
|
|
|
polls[name].votes.withAll(function(option, count) {
|
2012-06-20 00:39:26 +02:00
|
|
|
votesArr.push([option, count]);
|
2012-06-19 15:44:50 +02:00
|
|
|
});
|
2012-06-20 00:39:26 +02:00
|
|
|
|
|
|
|
votesArr = votesArr.sort(function(a, b) { return b[1] - a[1]; });
|
|
|
|
|
|
|
|
order = votesArr.map(function(vote) { return vote[0]; });
|
2012-06-18 22:16:22 +02:00
|
|
|
}
|
2012-06-20 00:39:26 +02:00
|
|
|
event.reply(dbot.t('count', {'poll': name, 'description': polls[name].description, 'places': order}));
|
2012-06-18 22:16:22 +02:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
2012-06-18 21:11:54 +02:00
|
|
|
}
|
2012-06-13 22:11:00 +02:00
|
|
|
};
|
2012-06-18 21:11:54 +02:00
|
|
|
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) \[options=([^ ]+)\] (.+)/, 5];
|
2012-06-17 13:53:52 +02:00
|
|
|
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
2012-06-15 00:42:06 +02:00
|
|
|
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
2012-06-15 18:51:45 +02:00
|
|
|
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
2012-06-20 00:39:26 +02:00
|
|
|
commands['~count'].regex = [/~count ([^ ]+)/, 2];
|
2012-06-13 22:11:00 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'poll',
|
|
|
|
'ignorable': true,
|
|
|
|
'commands': commands
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return poll(dbot);
|
|
|
|
}
|