3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 11:42:36 +01:00

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 polls = dbot.db.polls;
@ -13,7 +16,7 @@ var commands = function(dbot) {
votes[options[i]] = 0;
}
this.db.create(poll, name, {
this.db.create('poll', name, {
'description': description,
'owner': dbot.api.users.resolveUser(event.server, event.user),
'votes': votes,
@ -25,8 +28,8 @@ var commands = function(dbot) {
'name': name,
'description': description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'host': 'test.com',
'port': 80,
'path': 'polls/' + name
})
}));
@ -41,15 +44,18 @@ var commands = function(dbot) {
option = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) {
if(polls[name].owner === user) {
if(!_.has(polls[name].votes, option)) {
polls[name]['votes'][option] = 0;
this.db.read('poll', name, function(err, poll) {
if(!err) {
if(poll.owner === user) {
if(!_.has(poll.votes, option)) {
poll.votes[option] = 0;
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('option_added', {
'user': event.user,
'name': name,
'option': option
}));
}.bind(this));
} else {
event.reply(dbot.t('option_exists', {
'option': option,
@ -64,8 +70,11 @@ var commands = function(dbot) {
}));
}
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}
}.bind(this));
},
'~rmoption': function(event) {
@ -73,15 +82,19 @@ var commands = function(dbot) {
option = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) {
if(polls[name].owner === user) {
if(_.has(polls[name].votes, option)) {
delete polls[name]['votes'][option];
this.db.read('poll', name, function(err, poll) {
if(!err) {
if(poll.owner === user) {
if(_.has(poll.votes, option)) {
delete poll.votes[option];
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('option_removed', {
'user': event.user,
'name': name,
'option': option
}));
}.bind(this));
} else {
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 }));
}
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}
}.bind(this));
},
'~vote': function(event) {
@ -98,43 +114,43 @@ var commands = function(dbot) {
vote = event.input[2].toLowerCase(),
user = dbot.api.users.resolveUser(event.server, event.user);
if(_.has(polls, name)) {
if(_.has(polls[name].votes, vote)) {
if(_.has(polls[name].votees, user)) {
var oldVote = polls[name].votees[user];
polls[name].votes[oldVote]--;
polls[name].votes[vote]++;
polls[name].votees[user] = vote;
event.reply(dbot.t('changed_vote', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
this.db.read('poll', name, function(err, poll) {
if(!err) {
if(_.has(poll.votes, vote)) {
if(_.has(poll.votees, user)) {
var oldVote = poll.votees[user];
poll.votes[oldVote]--;
poll.votes[vote]++;
poll.votees[user] = vote;
} else {
polls[name].votes[vote]++;
polls[name].votees[user] = vote;
poll.votes[vote]++;
poll.votees[user] = vote;
}
this.db.save('poll', name, poll, function(err) {
event.reply(dbot.t('voted', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'count': poll.votes[vote],
'user': event.user
}));
}
}.bind(this));
} else {
event.reply(dbot.t('invalid_vote', { 'vote': vote }));
}
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
}
}.bind(this));
},
'~pdesc': function(event) {
var name = event.input[1].toLowerCase();
if(_.has(polls, name)) {
var options = _.keys(polls[name].votes);
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] + ', ';
@ -143,26 +159,29 @@ var commands = function(dbot) {
event.reply(dbot.t('poll_describe', {
'name': name,
'description': polls[name].description,
'description': poll.description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'host': 'test.com',
'port': 80,
'path': 'polls/' + name
})
}) + optionString);
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
}
});
},
'~count': function(event) {
var name = event.input[1].toLowerCase();
if(_.has(polls, name)) {
this.db.read('poll', name, function(err, poll) {
if(!err) {
var order;
var votesArr = [];
var order = _.chain(polls[name].votes)
var order = _.chain(poll.votes)
.pairs()
.sortBy(function(option) { return option[1] })
.reverse()
@ -177,13 +196,16 @@ var commands = function(dbot) {
event.reply(dbot.t('count', {
'poll': name,
'description': polls[name].description,
'description': poll.description,
'places': orderString
}));
} else {
if(err instanceof NoSuchThingError) {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
}
});
}
};
commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];