Poll module now functional using databank. What of it? [#272]

This commit is contained in:
reality 2013-03-16 00:02:35 +00:00
parent a5b65856a6
commit 54e3b84902

View File

@ -1,4 +1,7 @@
var _ = require('underscore')._; var _ = require('underscore')._,
databank = require('databank'),
AlreadyExistsError = databank.AlreadyExistsError;
NoSuchThingError = databank.NoSuchThingError;
var commands = function(dbot) { var commands = function(dbot) {
var polls = dbot.db.polls; var polls = dbot.db.polls;
@ -13,7 +16,7 @@ var commands = function(dbot) {
votes[options[i]] = 0; votes[options[i]] = 0;
} }
this.db.create(poll, name, { this.db.create('poll', name, {
'description': description, 'description': description,
'owner': dbot.api.users.resolveUser(event.server, event.user), 'owner': dbot.api.users.resolveUser(event.server, event.user),
'votes': votes, 'votes': votes,
@ -25,8 +28,8 @@ var commands = function(dbot) {
'name': name, 'name': name,
'description': description, 'description': description,
'url': dbot.t('url', { 'url': dbot.t('url', {
'host': dbot.config.web.webHost, 'host': 'test.com',
'port': dbot.config.web.webPort, 'port': 80,
'path': 'polls/' + name 'path': 'polls/' + name
}) })
})); }));
@ -41,31 +44,37 @@ var commands = function(dbot) {
option = event.input[2].toLowerCase(), option = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user); user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) { this.db.read('poll', name, function(err, poll) {
if(polls[name].owner === user) { if(!err) {
if(!_.has(polls[name].votes, option)) { if(poll.owner === user) {
polls[name]['votes'][option] = 0; if(!_.has(poll.votes, option)) {
event.reply(dbot.t('option_added', { poll.votes[option] = 0;
'user': event.user, this.db.save('poll', name, poll, function(err) {
'name': name, event.reply(dbot.t('option_added', {
'option': option 'user': event.user,
})); 'name': name,
'option': option
}));
}.bind(this));
} else {
event.reply(dbot.t('option_exists', {
'option': option,
'name': name,
'user': event.user
}));
}
} else { } else {
event.reply(dbot.t('option_exists', { event.reply(dbot.t('not_poll_owner', {
'option': option, 'user': event.user,
'name': name, 'name': name
'user': event.user
})); }));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', { if(err instanceof NoSuchThingError) {
'user': event.user, event.reply(dbot.t('poll_unexistent', { 'name': name }));
'name': name }
}));
} }
} else { }.bind(this));
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
}, },
'~rmoption': function(event) { '~rmoption': function(event) {
@ -73,24 +82,31 @@ var commands = function(dbot) {
option = event.input[2].toLowerCase(), option = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user); user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) { this.db.read('poll', name, function(err, poll) {
if(polls[name].owner === user) { if(!err) {
if(_.has(polls[name].votes, option)) { if(poll.owner === user) {
delete polls[name]['votes'][option]; if(_.has(poll.votes, option)) {
event.reply(dbot.t('option_removed', { delete poll.votes[option];
'user': event.user,
'name': name, this.db.save('poll', name, poll, function(err) {
'option': option event.reply(dbot.t('option_removed', {
})); 'user': event.user,
'name': name,
'option': option
}));
}.bind(this));
} else {
event.reply(dbot.t('invalid_vote', { 'vote': option }));
}
} else { } else {
event.reply(dbot.t('invalid_vote', { 'vote': option })); event.reply(dbot.t('not_poll_owner', { 'name': name }));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', { 'name': name })); if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
} }
} else { }.bind(this));
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}, },
'~vote': function(event) { '~vote': function(event) {
@ -98,91 +114,97 @@ var commands = function(dbot) {
vote = event.input[2].toLowerCase(), vote = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user); user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) { this.db.read('poll', name, function(err, poll) {
if(_.has(polls[name].votes, vote)) { if(!err) {
if(_.has(polls[name].votees, user)) { if(_.has(poll.votes, vote)) {
var oldVote = polls[name].votees[user]; if(_.has(poll.votees, user)) {
polls[name].votes[oldVote]--; var oldVote = poll.votees[user];
polls[name].votes[vote]++; poll.votes[oldVote]--;
polls[name].votees[user] = vote; poll.votes[vote]++;
poll.votees[user] = vote;
} else {
poll.votes[vote]++;
poll.votees[user] = vote;
}
event.reply(dbot.t('changed_vote', { this.db.save('poll', name, poll, function(err) {
'vote': vote, event.reply(dbot.t('voted', {
'poll': name, 'vote': vote,
'count': polls[name].votes[vote], 'poll': name,
'user': event.user 'count': poll.votes[vote],
})); 'user': event.user
}));
}.bind(this));
} else { } else {
polls[name].votes[vote]++; event.reply(dbot.t('invalid_vote', { 'vote': vote }));
polls[name].votees[user] = vote;
event.reply(dbot.t('voted', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} }
} else { } else {
event.reply(dbot.t('invalid_vote', { 'vote': vote })); if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
} }
} else { }.bind(this));
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}, },
'~pdesc': function(event) { '~pdesc': function(event) {
var name = event.input[1].toLowerCase(); var name = event.input[1].toLowerCase();
this.db.read('poll', name, function(err, poll) {
if(!err) {
var options = _.keys(poll.votes);
var optionString = " Choices: ";
for(var i=0;i<options.length;i++) {
optionString += options[i] + ', ';
}
optionString = optionString.slice(0, -2) + '.';
if(_.has(polls, name)) { event.reply(dbot.t('poll_describe', {
var options = _.keys(polls[name].votes); 'name': name,
var optionString = " Choices: "; 'description': poll.description,
for(var i=0;i<options.length;i++) { 'url': dbot.t('url', {
optionString += options[i] + ', '; 'host': 'test.com',
'port': 80,
'path': 'polls/' + name
})
}) + optionString);
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
} }
optionString = optionString.slice(0, -2) + '.'; });
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
})
}) + optionString);
} else {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}, },
'~count': function(event) { '~count': function(event) {
var name = event.input[1].toLowerCase(); var name = event.input[1].toLowerCase();
this.db.read('poll', name, function(err, poll) {
if(_.has(polls, name)) { if(!err) {
var order; var order;
var votesArr = []; var votesArr = [];
var order = _.chain(polls[name].votes) var order = _.chain(poll.votes)
.pairs() .pairs()
.sortBy(function(option) { return option[1] }) .sortBy(function(option) { return option[1] })
.reverse() .reverse()
.value(); .value();
var orderString = ""; var orderString = "";
for(var i=0;i<order.length;i++) { for(var i=0;i<order.length;i++) {
orderString += order[i][0] + orderString += order[i][0] +
" (" + order[i][1] + "), "; " (" + order[i][1] + "), ";
}
orderString = orderString.slice(0, -2);
event.reply(dbot.t('count', {
'poll': name,
'description': poll.description,
'places': orderString
}));
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
} }
orderString = orderString.slice(0, -2); });
event.reply(dbot.t('count', {
'poll': name,
'description': polls[name].description,
'places': orderString
}));
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
} }
}; };
commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4]; commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];