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,15 +44,18 @@ 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)) {
poll.votes[option] = 0;
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('option_added', { event.reply(dbot.t('option_added', {
'user': event.user, 'user': event.user,
'name': name, 'name': name,
'option': option 'option': option
})); }));
}.bind(this));
} else { } else {
event.reply(dbot.t('option_exists', { event.reply(dbot.t('option_exists', {
'option': option, 'option': option,
@ -64,8 +70,11 @@ var commands = function(dbot) {
})); }));
} }
} else { } else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name })); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}
}.bind(this));
}, },
'~rmoption': function(event) { '~rmoption': function(event) {
@ -73,15 +82,19 @@ 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)) {
delete poll.votes[option];
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('option_removed', { event.reply(dbot.t('option_removed', {
'user': event.user, 'user': event.user,
'name': name, 'name': name,
'option': option 'option': option
})); }));
}.bind(this));
} else { } else {
event.reply(dbot.t('invalid_vote', { 'vote': option })); event.reply(dbot.t('invalid_vote', { 'vote': option }));
} }
@ -89,8 +102,11 @@ var commands = function(dbot) {
event.reply(dbot.t('not_poll_owner', { 'name': name })); event.reply(dbot.t('not_poll_owner', { 'name': name }));
} }
} else { } else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name })); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}
}.bind(this));
}, },
'~vote': function(event) { '~vote': function(event) {
@ -98,43 +114,43 @@ 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;
event.reply(dbot.t('changed_vote', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} else { } else {
polls[name].votes[vote]++; poll.votes[vote]++;
polls[name].votees[user] = vote; poll.votees[user] = vote;
}
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('voted', { event.reply(dbot.t('voted', {
'vote': vote, 'vote': vote,
'poll': name, 'poll': name,
'count': polls[name].votes[vote], 'count': poll.votes[vote],
'user': event.user 'user': event.user
})); }));
} }.bind(this));
} else { } else {
event.reply(dbot.t('invalid_vote', { 'vote': vote })); event.reply(dbot.t('invalid_vote', { 'vote': vote }));
} }
} else { } else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name })); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}
}.bind(this));
}, },
'~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(_.has(polls, name)) { if(!err) {
var options = _.keys(polls[name].votes); var options = _.keys(poll.votes);
var optionString = " Choices: "; var optionString = " Choices: ";
for(var i=0;i<options.length;i++) { for(var i=0;i<options.length;i++) {
optionString += options[i] + ', '; optionString += options[i] + ', ';
@ -143,26 +159,29 @@ var commands = function(dbot) {
event.reply(dbot.t('poll_describe', { event.reply(dbot.t('poll_describe', {
'name': name, 'name': name,
'description': polls[name].description, 'description': poll.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
}) })
}) + optionString); }) + optionString);
} else { } else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name})); 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()
@ -177,13 +196,16 @@ var commands = function(dbot) {
event.reply(dbot.t('count', { event.reply(dbot.t('count', {
'poll': name, 'poll': name,
'description': polls[name].description, 'description': poll.description,
'places': orderString 'places': orderString
})); }));
} else { } else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
} }
} }
});
}
}; };
commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4]; commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3]; commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];