3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00
dbot/modules/poll.js

233 lines
9.8 KiB
JavaScript
Raw Normal View History

2012-06-13 22:11:00 +02:00
var poll = function(dbot) {
var polls = dbot.db.polls;
var commands = {
'~newpoll': function(event) {
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 {
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 22:16:22 +02:00
for(var i=0;i<options.length;i++) {
polls[name].options.push(options[i]);
}
} 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}) +
' - 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;
event.reply(dbot.t('option_added', {'user': event.user,
'name': name, 'option': option}));
2012-06-17 13:51:25 +02:00
} else {
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];
event.reply(dbot.t('option_removed', {'user': event.user,
'name': name, 'option': option}));
2012-06-17 13:51:25 +02:00
} else {
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)) {
if(polls[name].av) {
var prefs = vote.split(',');
2012-06-19 15:44:50 +02:00
prefs = prefs.uniq();
var valid = true;
prefs.each(function(pref) {
2012-06-19 15:44:50 +02:00
valid = valid && polls[name].options.indexOf(pref) != -1;
});
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}));
} 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-15 00:32:16 +02:00
} else {
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 {
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)) {
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-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 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
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;
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 = [];
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-13 22:11:00 +02:00
};
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) options=([^ ]+) (.+)/, 5];
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
commands['~newpoll'].usage = '~newpoll [pollname] options=[each,poll,option] [Poll Description]';
commands['~addoption'].usage = '~addoption [pollname] [newoption]';
commands['~rmoption'].usage= '~rmoption [pollname] [optiontoremove]';
commands['~vote'].usage= '~vote [pollname] [option]';
commands['~pdesc'].usage = '~pdesc [pollname]';
2012-06-13 22:11:00 +02:00
return {
'name': 'poll',
'ignorable': true,
'commands': commands
};
};
exports.fetch = function(dbot) {
return poll(dbot);
}