diff --git a/modules/dice.js b/modules/dice.js new file mode 100644 index 0000000..d794e7b --- /dev/null +++ b/modules/dice.js @@ -0,0 +1,88 @@ +var parseDiceSpec = function (specString) { + var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)(|[+-][0-9]+)$/i, 4); + if (rawSpec !== false) { + if (rawSpec[2] === "%") { + rawSpec[2] = 100; + } + return { + "count": parseInt(rawSpec[1] || 1), + "sides": parseInt(rawSpec[2] || 6), + "modifier": parseInt(rawSpec[3] || 0) + }; + } else { + return false; + } +}; + +var normalizeDiceSpec = function (specString) { + var diceSpec = parseDiceSpec(specString); + + if (diceSpec["count"] > 1) { + var count = diceSpec["count"]; + } else { + var count = ""; + } + + if (diceSpec["sides"] === 100) { + var sides = "%"; + } else { + var sides = diceSpec["sides"]; + } + + if (diceSpec["modifier"] > 0) { + var modifier = "+" + diceSpec["modifier"]; + } else if (diceSpec["modifier"] < 0) { + var modifier = diceSpec["modifier"]; + } else { + var modifier = ""; + } + + return (count + "d" + sides + modifier); +}; + +var dice = function(dbot) { + var commands = { + '~roll': function (data, params) { + var rolls = []; + + if (params.length === 1) { + params.push("d6"); + } + + for (var i = 1; i < params.length; i++) { + var diceSpec = parseDiceSpec(params[i]); + if (diceSpec === false) { + rolls.push([params[i], false]); + } else { + rolls.push([normalizeDiceSpec(params[i]), []]); + for (var j = 0; j < diceSpec["count"] ; j++) { + rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"]) + diceSpec["modifier"]); + } + } + } + + for (var i = 0; i < rolls.length; i++) { + if (rolls[i][1] === false) { + dbot.say(data.channel, rolls[i][0] + ": invalid dice spec"); + } else { + if (rolls[i][1].length > 1) { + var total = " (total " + rolls[i][1].sum() + ")"; + } else { + var total = ""; + } + dbot.say(data.channel, rolls[i][0] + ": " + rolls[i][1].join(" ") + total); + } + } + } + }; + + return { + 'onLoad': function() { + return commands; + } + }; +} + +exports.fetch = function(dbot) { + return dice(dbot); +}; diff --git a/modules/puns.js b/modules/puns.js index 1cc2661..29b7ae9 100644 --- a/modules/puns.js +++ b/modules/puns.js @@ -4,9 +4,9 @@ var puns = function(dbot) { return { 'listener': function(data) { if(data.user == 'reality') { - dbot.instance.say(data.channel, dbot.db.quoteArrs['realityonce'].random()); + dbot.instance.say(data.channel, dbot.interpolatedQuote('realityonce')); } else if(dbot.db.quoteArrs.hasOwnProperty(data.user.toLowerCase())) { - dbot.say(data.channel, data.user + ': ' + dbot.db.quoteArrs[data.user.toLowerCase()].random()); + dbot.say(data.channel, data.user + ': ' + dbot.interpolatedQuote(data.user.toLowerCase())); } else if(dbot.instance.inChannel(data.channel)) { dbot.instance.say('aisbot', '.karma ' + data.user); dbot.waitingForKarma = data.channel; diff --git a/modules/quotes.js b/modules/quotes.js index 03e62a7..340adf2 100644 --- a/modules/quotes.js +++ b/modules/quotes.js @@ -10,7 +10,7 @@ var quotes = function(dbot) { q[1] = q[1].trim(); key = q[1].toLowerCase(); if(quotes.hasOwnProperty(key)) { - dbot.say(data.channel, q[1] + ': ' + quotes[key].random()); + dbot.say(data.channel, q[1] + ': ' + dbot.interpolatedQuote(key)); } else { dbot.say(data.channel, 'Nobody loves ' + q[1]); } @@ -189,11 +189,11 @@ var quotes = function(dbot) { '~rq': function(data, params) { var rQuote = Object.keys(quotes).random(); - dbot.say(data.channel, rQuote + ': ' + quotes[rQuote].random()); + dbot.say(data.channel, rQuote + ': ' + dbot.interpolatedQuote(rQuote)); }, '~d': function(data, params) { - dbot.say(data.channel, data.user + ': ' + dbot.db.quoteArrs['depressionbot'].random()); + dbot.say(data.channel, data.user + ': ' + dbot.interpolatedQuote('depressionbot')); }, '~link': function(data, params) { diff --git a/run.js b/run.js index 9479300..1071f15 100644 --- a/run.js +++ b/run.js @@ -20,6 +20,9 @@ var DBot = function(timers) { if(!this.db.hasOwnProperty("quoteArrs")) { this.db.quoteArrs = {}; } + if(!this.db.quoteArrs.hasOwnProperty("realityonce")) { + this.db.quoteArrs.realityonce = []; + } if(!this.db.hasOwnProperty("kicks")) { this.db.kicks = {}; } @@ -58,6 +61,22 @@ var DBot = function(timers) { this.instance.connect(); }; +// Retrieve a random quote from a given category, interpolating any quote references (~~QUOTE CATEGORY~~) within it +DBot.prototype.interpolatedQuote = function(key) { + var quoteString = this.db.quoteArrs[key].random(); + var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/); + if (quoteRefs) { + quoteRefs = quoteRefs.slice(1); + for(var i=0;i