Started working on transforming quotes. Need more input from evan and that before continuing I think. [#272]

This commit is contained in:
reality 2013-03-16 13:45:58 +00:00
parent 54e3b84902
commit 4e1beb7580
2 changed files with 59 additions and 52 deletions

View File

@ -1,6 +1,6 @@
var _ = require('underscore')._, var _ = require('underscore')._,
databank = require('databank'), databank = require('databank'),
AlreadyExistsError = databank.AlreadyExistsError; AlreadyExistsError = databank.AlreadyExistsError,
NoSuchThingError = databank.NoSuchThingError; NoSuchThingError = databank.NoSuchThingError;
var commands = function(dbot) { var commands = function(dbot) {

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 quotes = dbot.db.quoteArrs; var quotes = dbot.db.quoteArrs;
@ -41,20 +44,22 @@ var commands = function(dbot) {
{ 'count': rmCacheCount })); { 'count': rmCacheCount }));
}, },
// Retrieve quote from a category in the database. // Retrieve quote from a category in the database.
'~q': function(event) { '~q': function(event) {
var key = event.input[1].trim().toLowerCase(); var name = event.input[1].trim().toLowerCase();
var quote = this.api.getQuote(event, event.input[1]); this.db.read('quote_category', name, function(err, category) {
if(quote) { if(!err) {
event.reply(key + ': ' + quote); var quoteIndex = _.random(0, category.length - 1);
} else { event.reply(key + ': ' + category[quoteIndex]);
event.reply(dbot.t('category_not_found', {'category': key})); } else if(err instanceof AlreadyExistsError) {
event.reply(dbot.t('category_not_found', { 'category': name }));
} }
});
}, },
// Shows a list of the biggest categories // Shows a list of the biggest categories
'~qstats': function(event) { '~qstats': function(event) {
this.db.readAll('quote_category)
var qSizes = _.chain(quotes) var qSizes = _.chain(quotes)
.pairs() .pairs()
.sortBy(function(category) { return category[1].length }) .sortBy(function(category) { return category[1].length })
@ -122,23 +127,22 @@ var commands = function(dbot) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
var quote = event.input[2]; var quote = event.input[2];
if(_.has(quotes, key)) { this.db.remove('quote_category', key, quote, function(err) {
var category = quotes[key]; if(!err) {
var index = category.indexOf(quote);
if(index !== -1) {
category.splice(index, 1);
if(category.length === 0) {
delete quotes[key];
}
this.internalAPI.resetRemoveTimer(event, key, quote); this.internalAPI.resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {
event.reply(dbot.t('removed_from', {'category': key, 'quote': quote})); 'category': key,
} else { 'quote': quote
event.reply(dbot.t('q_not_exist_under', {'category': key, 'quote': quote})); }));
} } else if(err instanceof NoSuchThingError) {
} else {
event.reply(dbot.t('category_not_found', { 'category': key })); event.reply(dbot.t('category_not_found', { 'category': key }));
} else if(err instanceof NoSuchItemError) {
event.reply(dbot.t('q_not_exist_under', {
'category': key,
'quote': quote
}));
} }
}.bind(this));
} else { } else {
event.reply(dbot.t('rmlast_spam')); event.reply(dbot.t('rmlast_spam'));
} }
@ -148,15 +152,17 @@ var commands = function(dbot) {
var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2); var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2);
if(input) { // Give quote count for named category if(input) { // Give quote count for named category
var key = input[1].trim().toLowerCase(); var key = input[1].trim().toLowerCase();
if(_.has(quotes, key)) { this.db.get('quote_category', key, function(err, category) {
if(!err) {
event.reply(dbot.t('quote_count', { event.reply(dbot.t('quote_count', {
'category': key, 'category': key,
'count': quotes[key].length 'count': category.length
})); }));
} else { } else if(err instanceof AlreadyExistsError) {
event.reply(dbot.t('no_quotes', { 'category': key })); event.reply(dbot.t('category_not_found', { 'category': name }));
} }
} else { // Give total quote count }.bind(this));
} else { // TODO: databankise total quote count
var totalQuoteCount = _.reduce(quotes, function(memo, category) { var totalQuoteCount = _.reduce(quotes, function(memo, category) {
return memo + category.length; return memo + category.length;
}, 0); }, 0);
@ -166,24 +172,25 @@ var commands = function(dbot) {
'~qadd': function(event) { '~qadd': function(event) {
var key = event.input[1].toLowerCase(); var key = event.input[1].toLowerCase();
var text = event.input[2]; var quote = event.input[2];
if(!_.isArray(quotes[key])) {
quotes[key] = [];
}
if(_.include(quotes[key], text)) { this.db.indexOf('quote_category', key, text, function(err, index) {
event.reply(dbot.t('quote_exists')); if(index == -1) {
} else { this.db.append('quote_category', key, quote, function(err) {
quotes[key].push(text);
this.rmAllowed = true; this.rmAllowed = true;
dbot.api.event.emit('~qadd', {
'key': key,
'text': text
});
event.reply(dbot.t('quote_saved', { event.reply(dbot.t('quote_saved', {
'category': key, 'category': key,
'count': quotes[key].length 'count': 0 // TODO: Figure out a way to get the count in the shim
})); }));
}.bind(this));
return { 'key': key, 'text': text }; } else {
event.reply(dbot.t('quote_exists'));
} }
return false; }.bind(this));
}, },
'~rq': function(event) { '~rq': function(event) {