3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 04:32:37 +01:00

Merge pull request #31 from danharibo/master

Recursive interpolatedQuote
This commit is contained in:
Luke Slater 2012-03-13 08:26:18 -07:00
commit 1753b79463

9
run.js
View File

@ -67,7 +67,9 @@ var DBot = function(timers) {
};
// Retrieve a random quote from a given category, interpolating any quote references (~~QUOTE CATEGORY~~) within it
DBot.prototype.interpolatedQuote = function(key) {
DBot.prototype.interpolatedQuote = function(key, quoteTree) {
if( quoteTree !== undefined && quoteTree.indexOf( key ) != -1 ) return '';
else if( quoteTree === undefined ) quoteTree = [];
var quoteString = this.db.quoteArrs[key].random();
var quoteRefs;
while( (quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/)) ) {
@ -75,7 +77,10 @@ DBot.prototype.interpolatedQuote = function(key) {
for(var i=0;i<quoteRefs.length;i++) {
var cleanRef = this.cleanNick(quoteRefs[i].trim());
if (this.db.quoteArrs.hasOwnProperty(cleanRef)) {
quoteString = quoteString.replace("~~"+cleanRef+"~~", this.db.quoteArrs[cleanRef].random());
quoteTree.push( cleanRef );
console.log( "Tree: " + quoteTree, "clean: " + cleanRef );
quoteString = quoteString.replace("~~"+cleanRef+"~~", this.interpolatedQuote(cleanRef, quoteTree.slice()));
quoteTree.pop();
}
}
}