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

And there's the modifiers. Beautiful.

This commit is contained in:
Psychedelic Squid 2012-03-07 03:33:18 +00:00
parent fea815a24f
commit e925021714

View File

@ -1,12 +1,13 @@
var parseDiceSpec = function (specString) { 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 !== 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)
}; };
} else { } else {
return false; return false;
@ -15,7 +16,28 @@ var parseDiceSpec = function (specString) {
var normalizeDiceSpec = function (specString) { var normalizeDiceSpec = function (specString) {
var diceSpec = parseDiceSpec(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) { var dice = function(dbot) {
@ -34,7 +56,7 @@ var dice = function(dbot) {
} else { } else {
rolls.push([normalizeDiceSpec(params[i]), []]); rolls.push([normalizeDiceSpec(params[i]), []]);
for (var j = 0; j < diceSpec["count"] ; j++) { 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"]);
} }
} }
} }