2013-01-13 00:31:53 +01:00
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var poll = function(dbot) {
|
|
|
|
var polls = dbot.db.polls;
|
|
|
|
var commands = {
|
|
|
|
'~newpoll': function(event) {
|
2013-01-13 00:31:53 +01:00
|
|
|
var name = event.input[1],
|
|
|
|
options = event.input[2].split(','),
|
|
|
|
description = event.input[3];
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls, name)) {
|
|
|
|
event.reply(dbot.t('poll_exists', { 'name': name }));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
polls[name] = {
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'owner': event.user,
|
|
|
|
'votes': {},
|
|
|
|
'votees': {}
|
|
|
|
};
|
|
|
|
for(var i=0;i<options.length;i++) {
|
|
|
|
polls[name]['votes'][options[i]] = 0;
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('poll_created', {
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'url': dbot.t('url', {
|
|
|
|
'host': dbot.config.web.webHost,
|
|
|
|
'port': dbot.config.web.webPort,
|
|
|
|
'path': 'polls/' + name
|
|
|
|
})
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~addoption': function(event) {
|
2013-01-13 00:31:53 +01:00
|
|
|
var name = event.input[1],
|
|
|
|
option = event.input[2];
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls, name)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
if(polls[name].owner === event.user) {
|
2013-01-13 00:31:53 +01:00
|
|
|
if(!_.has(polls[name].votes, name)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
polls[name]['votes'][option] = 0;
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('option_added', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name,
|
|
|
|
'option': option
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('option_exists', {
|
|
|
|
'option': option,
|
|
|
|
'name': name,
|
|
|
|
'user': event.user
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('not_poll_owner', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~rmoption': function(event) {
|
2013-01-13 00:31:53 +01:00
|
|
|
var name = event.input[1],
|
|
|
|
option = event.input[2];
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls, name)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
if(polls[name].owner === event.user) {
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls[name].votes, option)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
delete polls[name]['votes'][option];
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('option_removed', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name,
|
|
|
|
'option': option
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('invalid_vote', { 'vote': option }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('not_poll_owner', { 'name': name }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~vote': function(event) {
|
2013-01-13 00:31:53 +01:00
|
|
|
var name = event.input[1],
|
|
|
|
vote = event.input[2];
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls, name)) {
|
|
|
|
if(_.has(polls[name].votes, vote)) {
|
|
|
|
if(_.has(polls[name].votees, event.user)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
var oldVote = polls[name].votees[event.user];
|
|
|
|
polls[name].votes[oldVote]--;
|
|
|
|
polls[name].votes[vote]++;
|
|
|
|
polls[name].votees[event.user] = vote;
|
2013-01-13 00:31:53 +01:00
|
|
|
|
|
|
|
event.reply(dbot.t('changed_vote', {
|
|
|
|
'vote': vote,
|
|
|
|
'poll': name,
|
|
|
|
'count': polls[name].votes[vote],
|
|
|
|
'user': event.user
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
|
|
|
polls[name].votes[vote]++;
|
|
|
|
polls[name].votees[event.user] = vote;
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('voted', {
|
|
|
|
'vote': vote,
|
|
|
|
'poll': name,
|
|
|
|
'count': polls[name].votes[vote],
|
|
|
|
'user': event.user
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('invalid_vote', { 'vote': vote }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~pdesc': function(event) {
|
|
|
|
var name = event.input[1];
|
2013-01-13 00:31:53 +01:00
|
|
|
|
|
|
|
if(_.has(polls, name)) {
|
|
|
|
event.reply(dbot.t('poll_describe', {
|
|
|
|
'name': name,
|
|
|
|
'description': polls[name].description,
|
|
|
|
'url': dbot.t('url', {
|
|
|
|
'host': dbot.config.web.webHost,
|
|
|
|
'port': dbot.config.web.webPort,
|
|
|
|
'path': 'polls/' + name
|
|
|
|
})
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~count': function(event) {
|
|
|
|
var name = event.input[1];
|
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(polls, name)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
var order;
|
2013-01-13 00:31:53 +01:00
|
|
|
var votesArr = [];
|
|
|
|
|
|
|
|
var order = _.chain(polls[name].votes)
|
|
|
|
.pairs()
|
|
|
|
.sortBy(function(option) { return option[1] })
|
|
|
|
.reverse()
|
|
|
|
.value();
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-13 00:31:53 +01:00
|
|
|
var orderString = "";
|
|
|
|
for(var i=0;i<order.length;i++) {
|
|
|
|
orderString += order[i][0] +
|
|
|
|
" (" + order[i][1] + "), ";
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
2013-01-13 00:31:53 +01:00
|
|
|
orderString = orderString.slice(0, -2);
|
|
|
|
|
|
|
|
event.reply(dbot.t('count', {
|
|
|
|
'poll': name,
|
|
|
|
'description': polls[name].description,
|
|
|
|
'places': orderString
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-01-13 00:31:53 +01:00
|
|
|
commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
|
2012-12-11 17:04:52 +01:00
|
|
|
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
|
|
|
commands['~count'].regex = [/~count ([^ ]+)/, 2];
|
2012-12-24 06:47:47 +01:00
|
|
|
|
|
|
|
var pages = {
|
|
|
|
// Shows the results of a poll
|
|
|
|
'/polls/:key': function(req, res) {
|
|
|
|
var key = req.params.key.toLowerCase();
|
2013-01-13 00:31:53 +01:00
|
|
|
if(_.has(dbot.db.polls, key)) {
|
|
|
|
var totalVotes = _.reduce(dbot.db.polls[key].votes,
|
|
|
|
function(memo, option) {
|
|
|
|
return memo += option;
|
|
|
|
}, 0);
|
|
|
|
res.render('polls', {
|
|
|
|
'name': dbot.config.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()
|
|
|
|
}
|
|
|
|
});
|
2012-12-24 06:47:47 +01:00
|
|
|
} else {
|
2013-01-13 00:31:53 +01:00
|
|
|
res.render('error', {
|
|
|
|
'name': dbot.config.name,
|
|
|
|
'message': 'No polls under that key.'
|
|
|
|
});
|
2012-12-24 06:47:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lists all of the polls
|
|
|
|
'/polls': function(req, res) {
|
2013-01-13 00:31:53 +01:00
|
|
|
res.render('polllist', {
|
|
|
|
'name': dbot.config.name,
|
|
|
|
'polllist': Object.keys(dbot.db.polls)
|
|
|
|
});
|
2012-12-24 06:47:47 +01:00
|
|
|
},
|
|
|
|
};
|
2012-12-11 17:53:34 +01:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
return {
|
|
|
|
'name': 'poll',
|
|
|
|
'ignorable': true,
|
2012-12-24 06:47:47 +01:00
|
|
|
'commands': commands,
|
|
|
|
'pages': pages
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return poll(dbot);
|
|
|
|
}
|