fix dice crash

This commit is contained in:
Dominic Brady 2012-11-24 13:18:18 +00:00
parent 96b2dd0ddd
commit 7faa5c1207

View File

@ -1,97 +1,105 @@
var parseDiceSpec = function (specString) { var parseDiceSpec = function (specString) {
var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)(|[+-][0-9]+)$/i, 4); var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)(|[+-][0-9]+)$/i, 4);
if (rawSpec !== false) { if (rawSpec !== false) {
if (rawSpec[2] === "%") { if (rawSpec[2] === "%") {
rawSpec[2] = 100; rawSpec[2] = 100;
} }
return { return {
"count": parseInt(rawSpec[1] || 1), "count": parseInt(rawSpec[1] || 1),
"sides": parseInt(rawSpec[2] || 6), "sides": parseInt(rawSpec[2] || 6),
"modifier": parseInt(rawSpec[3] || 0) "modifier": parseInt(rawSpec[3] || 0)
}; };
} else { } else {
return false; return false;
} }
}; };
var normalizeDiceSpec = function (specString) { var normalizeDiceSpec = function (specString) {
var diceSpec = parseDiceSpec(specString); var diceSpec = parseDiceSpec(specString);
if (diceSpec["count"] > 1) { if (diceSpec["sides"] > 10000) {
var count = diceSpec["count"]; return false;
} else { }
var count = "";
} if (diceSpec["count"] > 1000) {
return false;
if (diceSpec["sides"] === 100) { }
var sides = "%";
} else { if (diceSpec["count"] > 1) {
var sides = diceSpec["sides"]; var count = diceSpec["count"];
} } else {
var count = "";
if (diceSpec["modifier"] > 0) { }
var modifier = "+" + diceSpec["modifier"];
} else if (diceSpec["modifier"] < 0) { if (diceSpec["sides"] === 100) {
var modifier = diceSpec["modifier"]; var sides = "%";
} else { } else {
var modifier = ""; var sides = diceSpec["sides"];
} }
return (count + "d" + sides + modifier); if (diceSpec["modifier"] > 0) {
}; var modifier = "+" + diceSpec["modifier"];
} else if (diceSpec["modifier"] < 0) {
var dice = function(dbot) { var modifier = diceSpec["modifier"];
var commands = { } else {
'~roll': function (event) { var modifier = "";
var rolls = []; }
if (event.params.length === 1) { return (count + "d" + sides + modifier);
event.params.push("d6"); };
}
var dice = function(dbot) {
for (var i = 1; i < event.params.length; i++) { var commands = {
var diceSpec = parseDiceSpec(event.params[i]); '~roll': function (event) {
if (diceSpec === false) { var rolls = [];
rolls.push([event.params[i], false]);
} else { if (event.params.length === 1) {
rolls.push([normalizeDiceSpec(event.params[i]), [], diceSpec["modifier"]]); event.params.push("d6");
for (var j = 0; j < diceSpec["count"] ; j++) { }
rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"]));
} for (var i = 1; i < event.params.length; i++) {
} var diceSpec = parseDiceSpec(event.params[i]);
} if (diceSpec === false) {
rolls.push([event.params[i], false]);
for (var i = 0; i < rolls.length; i++) { } else {
if (rolls[i][1] === false) { rolls.push([normalizeDiceSpec(event.params[i]), [], diceSpec["modifier"]]);
event.reply(rolls[i][0] + ": invalid dice spec"); for (var j = 0; j < diceSpec["count"] ; j++) {
} else { rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"]));
if (rolls[i][1].length > 1) { }
var total = " (total " + rolls[i][1].sum(); }
if (rolls[i][2] != 0) { }
if (rolls[i][2] > 0) {
total += " + "; for (var i = 0; i < rolls.length; i++) {
} else { if (rolls[i][1] === false) {
total += " - "; event.reply(rolls[i][0] + ": invalid dice spec");
} } else {
total += Math.abs(rolls[i][2]) + " -> " + (rolls[i][1].sum() + rolls[i][2]); if (rolls[i][1].length > 1) {
} var total = " (total " + rolls[i][1].sum();
total += ")" if (rolls[i][2] != 0) {
} else { if (rolls[i][2] > 0) {
var total = ""; total += " + ";
} } else {
event.reply(rolls[i][0] + ": " + rolls[i][1].join(" ") + total); total += " - ";
} }
} total += Math.abs(rolls[i][2]) + " -> " + (rolls[i][1].sum() + rolls[i][2]);
} }
}; total += ")"
} else {
return { var total = "";
'name': 'dice', }
'commands': commands, event.reply(rolls[i][0] + ": " + rolls[i][1].join(" ") + total);
'ignorable': true }
}; }
} }
};
exports.fetch = function(dbot) {
return dice(dbot); return {
}; 'name': 'dice',
'commands': commands,
'ignorable': true
};
}
exports.fetch = function(dbot) {
return dice(dbot);
};