From 08bd67f4f04d6a73e883a3d7beb7c5847348a9aa Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 21:40:16 +0000 Subject: [PATCH] incomplete underscorisation in quotes [#81] --- modules/quotes/quotes.js | 135 ++++++++++++++++++++++----------------- run.js | 21 +++--- 2 files changed, 87 insertions(+), 69 deletions(-) diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index a6baab7..96524c5 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -1,22 +1,24 @@ +var _ = require('underscore')._; + var quotes = function(dbot) { - var name = 'quotes'; - var quotes = dbot.db.quoteArrs; - var addStack = []; - var rmAllowed = true; + var quotes = dbot.db.quoteArrs, + addStack = [], + rmAllowed = true, + rmCache = dbot.sessionData.rmCache, + rmTimer; dbot.sessionData.rmCache = []; - var rmCache = dbot.sessionData.rmCache; - var rmTimer; // Retrieve a random quote from a given category, interpolating any quote // references (~~QUOTE CATEGORY~~) within it var interpolatedQuote = function(event, key, quoteTree) { - if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) { + if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) { return ''; - } else if(quoteTree === undefined) { + } else if(_.isUndefined(quoteTree)) { quoteTree = []; } - var quoteString = quotes[key].random(); + var index = _.random(0, quotes[key].length - 1); + var quoteString = quotes[key][index]; // Parse quote interpolations var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g); @@ -25,10 +27,10 @@ var quotes = function(dbot) { while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) { var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim()); if(cleanRef === '-nicks-') { - var randomNick = Object.keys(event.channel.nicks).random(); + var randomNick = _.keys(event.channel.nicks)[_.random(0, _.size(event.channel.nicks) -1)]; quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick); quoteTree.pop(); - } else if(quotes.hasOwnProperty(cleanRef)) { + } else if(_.has(quotes, cleanRef)) { quoteTree.push(key); quoteString = quoteString.replace("~~" + cleanRef + "~~", interpolatedQuote(event, cleanRef, quoteTree.slice())); @@ -45,17 +47,20 @@ var quotes = function(dbot) { rmAllowed = true; }); - rmCache.push({'key': key, 'quote': quote}); + rmCache.push({ + 'key': key, + 'quote': quote + }); dbot.timers.clearTimeout(rmTimer); + if(rmCache.length < dbot.config.quotes.rmLimit) { rmTimer = dbot.timers.addOnceTimer(600000, function() { rmCache.length = 0; // lol what }); } else { - for(var i=0;i