rewrote all of quotes commands because array shims just werent good enough [#331]

This commit is contained in:
reality 2013-04-12 00:38:51 +00:00
parent 6bae0f47bf
commit b376a8fa54
2 changed files with 147 additions and 104 deletions

View File

@ -1,34 +1,45 @@
var _ = require('underscore')._, var _ = require('underscore')._,
databank = require('databank'), databank = require('databank'),
AlreadyExistsError = databank.AlreadyExistsError, uuid = require('node-uuid');
NoSuchThingError = databank.NoSuchThingError;
var commands = function(dbot) { var commands = function(dbot) {
var quotes = dbot.db.quoteArrs; var quotes = dbot.db.quoteArrs;
var commands = { var commands = {
/*** Quote Addition ***/ /*** Quote Addition ***/
// Add a quote to a category // Add a quote to a category
'~qadd': function(event) { '~qadd': function(event) {
var key = event.input[1].toLowerCase(); var key = event.input[1].toLowerCase().trim(),
var quote = event.input[2]; quote = event.input[2],
newCount;
var quoteAdded = function(err, result) {
this.rmAllowed = true;
dbot.api.event.emit('~qadd', {
'key': key,
'text': quote
});
event.reply(dbot.t('quote_saved', {
'category': key,
'count': newCount
}));
}.bind(this);
this.db.indexOf('quote_category', key, quote, function(err, index) { var category = false;
if(index == null || index == -1) { this.db.search('quote_category', { 'name': key }, function(result) {
this.db.append('quote_category', key, quote, function(err, newCount) { category = result;
this.rmAllowed = true; }, function(err) {
dbot.api.event.emit('~qadd', { if(!category) {
'key': key, var id = uuid.v4();
'text': quote newCount = 1;
}); this.db.create('quote_category', id, {
event.reply(dbot.t('quote_saved', { 'id': id,
'category': key, 'name': key,
'count': newCount 'quotes': [ quote ],
})); 'creator': event.user
}.bind(this)); }, quoteAdded);
} else { } else {
event.reply(dbot.t('quote_exists')); newCount = category.quotes.push(quote);
this.db.save('quote_category', category.id, category, quoteAdded);
} }
}.bind(this)); }.bind(this));
}, },
@ -42,27 +53,38 @@ var commands = function(dbot) {
// Retrieve quote from a category in the database. // Retrieve quote from a category in the database.
'~q': function(event) { '~q': function(event) {
var name = event.input[1].trim().toLowerCase(); var name = event.input[1].trim().toLowerCase(),
this.db.read('quote_category', name, function(err, category) { category = false;
if(!err) {
var quoteIndex = _.random(0, category.length - 1); this.db.search('quote_category', { 'name': name }, function(result) {
event.reply(name + ': ' + category[quoteIndex]); category = result;
} else if(err instanceof NoSuchThingError) { }, function(err) {
if(category) {
var quotes = category.quotes;
var index = _.random(0, quotes.length - 1);
event.reply(name + ': ' + quotes[index]);
} else {
event.reply(dbot.t('category_not_found', { 'category': name })); event.reply(dbot.t('category_not_found', { 'category': name }));
} }
}); });
}, },
// Choose a random quote category and a random quote from that
// TODO: This is quite inefficient, but databank must evolve to do otherwise.
'~rq': function(event) { '~rq': function(event) {
if(_.keys(quotes).length > 0) { var categories = [];
var category = _.keys(quotes)[_.random(0, _.size(quotes) -1)]; this.db.scan('quote_category', function(result) {
event.reply(category + ': ' + this.internalAPI.interpolatedQuote(event.server, event.channel.name, category)); categories.push(result);
} else { }, function(err) {
event.reply(dbot.t('no_results')); var cIndex = _.random(0, _.size(categories) -1);
} var qIndex = _.random(0, categories[cIndex].quotes.length - 1);
event.reply(categories[cIndex].name + ': ' + categories[cIndex].quotes[qIndex]);
});
}, },
/*** Quote Removal ***/ /*** Quote Removal
TODO: Remove empty quote categories
***/
// Show number of quotes in removal cache // Show number of quotes in removal cache
'~rmstatus': function(event) { '~rmstatus': function(event) {
@ -90,11 +112,10 @@ var commands = function(dbot) {
var rmCacheCount = rmCache.length; var rmCacheCount = rmCache.length;
_.each(rmCache, function(quote, index) { _.each(rmCache, function(quote, index) {
this.db.append('quote_category', quote.key, quote.quote, function(err, length) { //TODO: Add quote add API func
if(err) { var qadd = _.clone(event);
// QQ qadd.message = '~qadd ' + quote.key + '=' + quote.quote;
} dbot.instance.emit(qadd);
});
}); });
rmCache.length = 0; rmCache.length = 0;
@ -105,16 +126,22 @@ var commands = function(dbot) {
// Remove last quote from category // Remove last quote from category
'~rmlast': function(event) { '~rmlast': function(event) {
if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) { if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase(),
category = false;
this.db.slice('quote_category', key, -1, 1, function(err, removed) { this.db.search('quote_category', { 'name': key }, function(result) {
if(!err) { category = result;
this.internalAPI.resetRemoveTimer(event, key, removed); }, function(err) {
event.reply(dbot.t('removed_from', { if(category) {
'quote': removed, var removedQuote = category.quotes.pop();
'category': key this.db.save('quote_category', category.id, category, function(err) {
})); this.internalAPI.resetRemoveTimer(event, key, removedQuote);
} else if(err instanceof NoSuchThingError) { event.reply(dbot.t('removed_from', {
'quote': removedQuote,
'category': key
}));
}.bind(this));
} else {
event.reply(dbot.t('category_not_found', { 'category': key })); event.reply(dbot.t('category_not_found', { 'category': key }));
} }
}.bind(this)); }.bind(this));
@ -127,22 +154,30 @@ var commands = function(dbot) {
'~rm': function(event) { '~rm': function(event) {
if(this.rmAllowed == true || _.include(dbot.config.admins, event.user)) { if(this.rmAllowed == true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
var quote = event.input[2]; quote = event.input[2],
category = false;
this.db.remove('quote_category', key, quote, function(err) { this.db.search('quote_category', { 'name': key }, function(result) {
if(!err) { category = result;
this.internalAPI.resetRemoveTimer(event, key, quote); }, function(err) {
event.reply(dbot.t('removed_from', { if(category) {
'category': key, if(category.quotes.indexOf(quote) != -1) {
'quote': quote category.quotes = _.without(category.quotes, quote);
})); this.db.save('quote_category', category.id, category, function(err) {
} else if(err instanceof NoSuchThingError) { this.internalAPI.resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {
'category': key,
'quote': quote
}));
}.bind(this));
} else {
event.reply(dbot.t('q_not_exist_under', {
'category': key,
'quote': quote
}));
}
} 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)); }.bind(this));
} else { } else {
@ -156,39 +191,39 @@ var commands = function(dbot) {
'~qstats': function(event) { '~qstats': function(event) {
var quoteSizes = {}; var quoteSizes = {};
this.db.scan('quote_category', function(category) { this.db.scan('quote_category', function(category) {
// TODO: get name? if(category) {
quoteSizes[name] = category.length; quoteSizes[category.name] = category.quotes.length;
}.bind(this), function(err) {
if(err) {
// QQ
} }
}.bind(this), function(err) {
var qSizes = _.chain(quoteSizes)
.pairs()
.sortBy(function(category) { return category[1] })
.reverse()
.first(10)
.value();
var qString = dbot.t('large_categories');
for(var i=0;i<qSizes.length;i++) {
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), ";
}
event.reply(qString.slice(0, -2));
}); });
var qSizes = _.chain(quoteSizes)
.pairs()
.sortBy(function(category) { return category[1] })
.reverse()
.first(10)
.value();
var qString = dbot.t('large_categories');
for(var i=0;i<qSizes.length;i++) {
qString += qSizes[i][0] + " (" + qSizes[i][1].length + "), ";
}
event.reply(qString.slice(0, -2));
}, },
// Search a given category for some text. // Search a given category for some text.
'~qsearch': function(event) { '~qsearch': function(event) {
var haystack = event.input[1].trim().toLowerCase(); var haystack = event.input[1].trim().toLowerCase(),
var needle = event.input[2]; needle = event.input[2],
category = false;
this.db.read('quote_category', haystack, function(err, category) { this.db.search('quote_category', { 'name': haystack }, function(result) {
if(!err) { category = result;
var matches = _.filter(category, function(quote) { }, function(err) {
if(category) {
var matches = _.filter(category.quotes, function(quote) {
return quote.indexOf(needle) != -1; return quote.indexOf(needle) != -1;
}, this); });
if(matches.length == 0) { if(matches.length == 0) {
event.reply(dbot.t('no_results')); event.reply(dbot.t('no_results'));
@ -200,45 +235,53 @@ var commands = function(dbot) {
'matches': matches.length 'matches': matches.length
})); }));
} }
} else if(err == NoSuchThingError) { } else {
event.reply(dbot.t('empty_category')); event.reply(dbot.t('empty_category'));
} }
}.bind(this)); });
}, },
// Count quotes in a given category or total quotes overall // Count quotes in a given category or total quotes overall
'~qcount': function(event) { '~qcount': function(event) {
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(),
this.db.read('quote_category', key, function(err, category) { category = false;
if(!err) {
this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
if(category) {
event.reply(dbot.t('quote_count', { event.reply(dbot.t('quote_count', {
'category': key, 'category': key,
'count': category.length 'count': category.quotes.length
})); }));
} else if(err instanceof AlreadyExistsError) { } else {
event.reply(dbot.t('category_not_found', { 'category': name })); event.reply(dbot.t('category_not_found', { 'category': name }));
} }
}.bind(this)); });
} else { } else {
var quoteCount = 0; var quoteCount = 0;
this.db.scan('quote_category', function(category) { this.db.scan('quote_category', function(category) {
quoteCount += category.length; if(category) {
}.bind(this), function(err) { quoteCount += category.quotes.length;
if(!err) {
event.reply(dbot.t('total_quotes', { 'count': quoteCount }));
} }
}.bind(this)); }, function(err) {
event.reply(dbot.t('total_quotes', { 'count': quoteCount }));
});
} }
}, },
// Link to quote web page // Link to quote web page
'~link': function(event) { '~link': function(event) {
var key = event.input[1].toLowerCase(); var key = event.input[1].toLowerCase(),
this.db.read('quote_category', key, function(err, category) { category = false;
if(!err) {
if(_.has(dbot.config, 'web') && _.has(dbot.config.web, 'webHost') this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
if(category) {
if(_.has(dbot.config, 'web') && _.has(dbot.config.web, 'webHost')) {
event.reply(dbot.t('quote_link', { event.reply(dbot.t('quote_link', {
'category': key, 'category': key,
'url': dbot.t('url', { 'url': dbot.t('url', {
@ -250,7 +293,7 @@ var commands = function(dbot) {
} else { } else {
event.reply(dbot.t('web_not_configured')); event.reply(dbot.t('web_not_configured'));
} }
} else if(err == NoSuchThingError) { } else {
event.reply(dbot.t('category_not_found', { 'category': key })); event.reply(dbot.t('category_not_found', { 'category': key }));
} }
}); });

View File

@ -1,5 +1,5 @@
{ {
"dbKeys": [ "quoteArrs" ], "dbType": "redis",
"dependencies": [ "command", "users" ], "dependencies": [ "command", "users" ],
"rmLimit": 10, "rmLimit": 10,
"ignorable": true, "ignorable": true,