From cfe2c0db4f64318d1116a67b76b12b23ba347720 Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Wed, 7 Mar 2012 03:22:25 +0000 Subject: [PATCH 1/5] Sum function. --- snippets.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/snippets.js b/snippets.js index b769658..8e6a9af 100644 --- a/snippets.js +++ b/snippets.js @@ -27,6 +27,14 @@ Array.prototype.include = function(value) { return false; }; +Array.prototype.sum = function() { + var sum = 0; + for(var i=0;i Date: Wed, 7 Mar 2012 03:22:52 +0000 Subject: [PATCH 2/5] Basic dice-rolling functionality. Next, modifiers. --- modules/dice.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 modules/dice.js diff --git a/modules/dice.js b/modules/dice.js new file mode 100644 index 0000000..922baf4 --- /dev/null +++ b/modules/dice.js @@ -0,0 +1,66 @@ +var parseDiceSpec = function (specString) { + var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)$/i, 3); + if (rawSpec !== false) { + if (rawSpec[2] === "%") { + rawSpec[2] = 100; + } + return { + "count": parseInt(rawSpec[1] || 1), + "sides": parseInt(rawSpec[2] || 6) + }; + } else { + return false; + } +}; + +var normalizeDiceSpec = function (specString) { + var diceSpec = parseDiceSpec(specString); + return (diceSpec["count"] > 1 ? diceSpec["count"] : "") + "d" + (diceSpec["sides"] === 100 ? "%" : diceSpec["sides"]); +}; + +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"])); + } + } + } + + 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); +}; From e925021714367670cb696b0325461a197dab810a Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Wed, 7 Mar 2012 03:33:18 +0000 Subject: [PATCH 3/5] And there's the modifiers. Beautiful. --- modules/dice.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/dice.js b/modules/dice.js index 922baf4..d794e7b 100644 --- a/modules/dice.js +++ b/modules/dice.js @@ -1,12 +1,13 @@ var parseDiceSpec = function (specString) { - var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)$/i, 3); + 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) + "sides": parseInt(rawSpec[2] || 6), + "modifier": parseInt(rawSpec[3] || 0) }; } else { return false; @@ -15,7 +16,28 @@ var parseDiceSpec = function (specString) { var normalizeDiceSpec = function (specString) { var diceSpec = parseDiceSpec(specString); - return (diceSpec["count"] > 1 ? diceSpec["count"] : "") + "d" + (diceSpec["sides"] === 100 ? "%" : diceSpec["sides"]); + + 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) { @@ -34,7 +56,7 @@ var dice = function(dbot) { } 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"])); + rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"]) + diceSpec["modifier"]); } } } From 61c6aaf1877a625f327778177dcff2b076e9bbc3 Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Wed, 7 Mar 2012 04:25:07 +0000 Subject: [PATCH 4/5] Don't fail to have a realityonce quote array. --- run.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/run.js b/run.js index 9479300..4d683fc 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 = {}; } From 1d2659f3618506955c0e374e5572f0fe9044fd35 Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Wed, 7 Mar 2012 22:13:31 +0000 Subject: [PATCH 5/5] Interpolated quotes, because why not? --- modules/puns.js | 4 ++-- modules/quotes.js | 6 +++--- run.js | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) 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 4d683fc..1071f15 100644 --- a/run.js +++ b/run.js @@ -61,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