3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 22:39:26 +01:00

Merge pull request #20 from psquid/master

Interpolated quotes.
This commit is contained in:
Luke Slater 2012-03-07 14:17:57 -08:00
commit 18e93a5a66
5 changed files with 121 additions and 6 deletions

88
modules/dice.js Normal file
View File

@ -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);
};

View File

@ -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;

View File

@ -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) {

21
run.js
View File

@ -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<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());
}
}
}
return quoteString;
};
// Say something in a channel
DBot.prototype.say = function(channel, data) {
this.instance.say(channel, data);
@ -147,7 +166,7 @@ DBot.prototype.reloadModules = function() {
q[1] = q[1].trim();
key = this.cleanNick(q[1])
if(this.db.quoteArrs.hasOwnProperty(key)) {
this.say(data.channel, q[1] + ': ' + this.db.quoteArrs[key].random());
this.say(data.channel, q[1] + ': ' + this.interpolatedQuote(key));
} else {
// See if it's similar to anything
var winnerDistance = Infinity;

View File

@ -27,6 +27,14 @@ Array.prototype.include = function(value) {
return false;
};
Array.prototype.sum = function() {
var sum = 0;
for(var i=0;i<this.length;i++) {
sum += (parseFloat(this[i]) || 0);
}
return sum;
};
Array.prototype.allGroupings = function() {
if (this.length == 0) {
return []; /* short-circuit the empty-array case */