mirror of
https://github.com/reality/dbot.git
synced 2024-11-27 14:29:29 +01:00
parent
9db515d44a
commit
33b9b8d1b0
@ -4,45 +4,44 @@ var _ = require('underscore')._,
|
|||||||
NoSuchThingError = databank.NoSuchThingError;
|
NoSuchThingError = databank.NoSuchThingError;
|
||||||
|
|
||||||
var commands = function(dbot) {
|
var commands = function(dbot) {
|
||||||
var polls = dbot.db.polls;
|
|
||||||
var commands = {
|
var commands = {
|
||||||
'~newpoll': function(event) {
|
'~newpoll': function(event) {
|
||||||
var name = event.input[1].toLowerCase(),
|
var name = event.input[1].toLowerCase(),
|
||||||
options = event.input[2].toLowerCase().split(','),
|
options = event.input[2].toLowerCase().split(','),
|
||||||
description = event.input[3];
|
description = event.input[3];
|
||||||
|
|
||||||
var votes = {};
|
_.each(options, function(item, index, list) { list[index] = [ item, 0 ]; })
|
||||||
for(var i=0;i<options.length;i++) {
|
votes = _.object(options);
|
||||||
votes[options[i]] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
||||||
this.db.create('poll', name, {
|
this.db.create('poll', name, {
|
||||||
|
'name': name,
|
||||||
'description': description,
|
'description': description,
|
||||||
'owner': dbot.api.users.resolveUser(event.server, event.user),
|
'owner': user.id,
|
||||||
'votes': votes,
|
'votes': votes,
|
||||||
'votees': {}
|
'votees': {}
|
||||||
},
|
}, function(err, value) {
|
||||||
function(err, value) {
|
|
||||||
if(!err) {
|
if(!err) {
|
||||||
event.reply(dbot.t('poll_created', {
|
event.reply(dbot.t('poll_created', {
|
||||||
'name': name,
|
'name': name,
|
||||||
'description': description,
|
'description': description,
|
||||||
'url': dbot.api.web.getUrl('/poll/' + name)
|
'url': dbot.api.web.getUrl('poll/' + name)
|
||||||
}));
|
}));
|
||||||
} else if(err instanceof AlreadyExistsError) {
|
} else if(err instanceof AlreadyExistsError) {
|
||||||
event.reply(dbot.t('poll_exists', { 'name': name }));
|
event.reply(dbot.t('poll_exists', { 'name': name }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
'~addoption': function(event) {
|
'~addoption': function(event) {
|
||||||
var name = event.input[1].toLowerCase(),
|
var name = event.input[1].toLowerCase(),
|
||||||
option = event.input[2].toLowerCase(),
|
option = event.input[2].toLowerCase();
|
||||||
user = dbot.api.users.resolveUser(event.server, event.user);
|
|
||||||
|
|
||||||
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
||||||
this.db.read('poll', name, function(err, poll) {
|
this.db.read('poll', name, function(err, poll) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
if(poll.owner === user) {
|
if(poll.owner === user.id) {
|
||||||
if(!_.has(poll.votes, option)) {
|
if(!_.has(poll.votes, option)) {
|
||||||
poll.votes[option] = 0;
|
poll.votes[option] = 0;
|
||||||
this.db.save('poll', name, poll, function(err) {
|
this.db.save('poll', name, poll, function(err) {
|
||||||
@ -51,7 +50,7 @@ var commands = function(dbot) {
|
|||||||
'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,
|
||||||
@ -71,6 +70,7 @@ var commands = function(dbot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
'~rmoption': function(event) {
|
'~rmoption': function(event) {
|
||||||
@ -107,20 +107,20 @@ var commands = function(dbot) {
|
|||||||
|
|
||||||
'~vote': function(event) {
|
'~vote': function(event) {
|
||||||
var name = event.input[1].toLowerCase(),
|
var name = event.input[1].toLowerCase(),
|
||||||
vote = event.input[2].toLowerCase(),
|
vote = event.input[2].toLowerCase();
|
||||||
user = dbot.api.users.resolveUser(event.server, event.user);
|
|
||||||
|
|
||||||
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
||||||
this.db.read('poll', name, function(err, poll) {
|
this.db.read('poll', name, function(err, poll) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
if(_.has(poll.votes, vote)) {
|
if(_.has(poll.votes, vote)) {
|
||||||
if(_.has(poll.votees, user)) {
|
if(_.has(poll.votees, user.id)) {
|
||||||
var oldVote = poll.votees[user];
|
var oldVote = poll.votees[user.id];
|
||||||
poll.votes[oldVote]--;
|
poll.votes[oldVote]--;
|
||||||
poll.votes[vote]++;
|
poll.votes[vote]++;
|
||||||
poll.votees[user] = vote;
|
poll.votees[user.id] = vote;
|
||||||
} else {
|
} else {
|
||||||
poll.votes[vote]++;
|
poll.votes[vote]++;
|
||||||
poll.votees[user] = vote;
|
poll.votees[user.id] = vote;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.db.save('poll', name, poll, function(err) {
|
this.db.save('poll', name, poll, function(err) {
|
||||||
@ -140,6 +140,7 @@ var commands = function(dbot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
'~pdesc': function(event) {
|
'~pdesc': function(event) {
|
||||||
@ -156,11 +157,7 @@ var commands = function(dbot) {
|
|||||||
event.reply(dbot.t('poll_describe', {
|
event.reply(dbot.t('poll_describe', {
|
||||||
'name': name,
|
'name': name,
|
||||||
'description': poll.description,
|
'description': poll.description,
|
||||||
'url': dbot.t('url', {
|
'url': dbot.api.web.getUrl('poll/' + name)
|
||||||
'host': 'test.com',
|
|
||||||
'port': 80,
|
|
||||||
'path': 'polls/' + name
|
|
||||||
})
|
|
||||||
}) + optionString);
|
}) + optionString);
|
||||||
} else {
|
} else {
|
||||||
if(err instanceof NoSuchThingError) {
|
if(err instanceof NoSuchThingError) {
|
||||||
@ -179,7 +176,7 @@ var commands = function(dbot) {
|
|||||||
|
|
||||||
var order = _.chain(poll.votes)
|
var order = _.chain(poll.votes)
|
||||||
.pairs()
|
.pairs()
|
||||||
.sortBy(function(option) { return option[1] })
|
.sortBy(function(option) { return option[1]; })
|
||||||
.reverse()
|
.reverse()
|
||||||
.value();
|
.value();
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
var _ = require('underscore')._;
|
var _ = require('underscore')._;
|
||||||
|
|
||||||
var pages = function(dbot) {
|
var pages = function(dbot) {
|
||||||
var polls = dbot.db.polls;
|
|
||||||
var pages = {
|
var pages = {
|
||||||
// Shows the results of a poll
|
// Shows the results of a poll
|
||||||
'/polls/:key': function(req, res) {
|
'/poll/:key': function(req, res) {
|
||||||
var key = req.params.key.toLowerCase();
|
var key = req.params.key.toLowerCase();
|
||||||
if(_.has(dbot.db.polls, key)) {
|
if(_.has(dbot.db.polls, key)) {
|
||||||
var totalVotes = _.reduce(dbot.db.polls[key].votes,
|
var totalVotes = _.reduce(dbot.db.polls[key].votes,
|
||||||
@ -31,9 +30,15 @@ var pages = function(dbot) {
|
|||||||
|
|
||||||
// Lists all of the polls
|
// Lists all of the polls
|
||||||
'/poll': function(req, res) {
|
'/poll': function(req, res) {
|
||||||
|
var pollKeys = [];
|
||||||
|
this.db.scan('poll', function(result) {
|
||||||
|
if(result) pollKeys.push(result.name);
|
||||||
|
}, function(err) {
|
||||||
|
console.log(pollKeys);
|
||||||
res.render('polllist', {
|
res.render('polllist', {
|
||||||
'name': dbot.config.name,
|
'name': dbot.config.name,
|
||||||
'polllist': Object.keys(dbot.db.polls)
|
'polllist': pollKeys
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -7,6 +7,6 @@ block content
|
|||||||
input(type="text", name="search", id="search-text", oninput="search(this.value)")
|
input(type="text", name="search", id="search-text", oninput="search(this.value)")
|
||||||
ul#quotelist
|
ul#quotelist
|
||||||
-each poll in polllist
|
-each poll in polllist
|
||||||
a(href='/polls/'+poll)
|
a(href='/poll/'+poll)
|
||||||
li.quotes #{poll}
|
li.quotes #{poll}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user