Spaces:
Running
Running
File size: 2,922 Bytes
b82d373 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
// Borrowed from the Droll library by thebinarypenguin
// https://github.com/thebinarypenguin/droll
// Licensed under MIT license
var droll = {};
// Define a "class" to represent a formula
function DrollFormula() {
this.numDice = 0;
this.numSides = 0;
this.modifier = 0;
this.minResult = 0;
this.maxResult = 0;
this.avgResult = 0;
}
// Define a "class" to represent the results of the roll
function DrollResult() {
this.rolls = [];
this.modifier = 0;
this.total = 0;
}
/**
* Returns a string representation of the roll result
*/
DrollResult.prototype.toString = function () {
if (this.rolls.length === 1 && this.modifier === 0) {
return this.rolls[0] + '';
}
if (this.rolls.length > 1 && this.modifier === 0) {
return this.rolls.join(' + ') + ' = ' + this.total;
}
if (this.rolls.length === 1 && this.modifier > 0) {
return this.rolls[0] + ' + ' + this.modifier + ' = ' + this.total;
}
if (this.rolls.length > 1 && this.modifier > 0) {
return this.rolls.join(' + ') + ' + ' + this.modifier + ' = ' + this.total;
}
if (this.rolls.length === 1 && this.modifier < 0) {
return this.rolls[0] + ' - ' + Math.abs(this.modifier) + ' = ' + this.total;
}
if (this.rolls.length > 1 && this.modifier < 0) {
return this.rolls.join(' + ') + ' - ' + Math.abs(this.modifier) + ' = ' + this.total;
}
};
/**
* Parse the formula into its component pieces.
* Returns a DrollFormula object on success or false on failure.
*/
droll.parse = function (formula) {
var pieces = null;
var result = new DrollFormula();
pieces = formula.match(/^([1-9]\d*)?d([1-9]\d*)([+-]\d+)?$/i);
if (!pieces) { return false; }
result.numDice = (pieces[1] - 0) || 1;
result.numSides = (pieces[2] - 0);
result.modifier = (pieces[3] - 0) || 0;
result.minResult = (result.numDice * 1) + result.modifier;
result.maxResult = (result.numDice * result.numSides) + result.modifier;
result.avgResult = (result.maxResult + result.minResult) / 2;
return result;
};
/**
* Test the validity of the formula.
* Returns true on success or false on failure.
*/
droll.validate = function (formula) {
return (droll.parse(formula)) ? true : false;
};
/**
* Roll the dice defined by the formula.
* Returns a DrollResult object on success or false on failure.
*/
droll.roll = function (formula) {
var pieces = null;
var result = new DrollResult();
pieces = droll.parse(formula);
if (!pieces) { return false; }
for (var a = 0; a < pieces.numDice; a++) {
result.rolls[a] = (1 + Math.floor(Math.random() * pieces.numSides));
}
result.modifier = pieces.modifier;
for (var b = 0; b < result.rolls.length; b++) {
result.total += result.rolls[b];
}
result.total += result.modifier;
return result;
};
// END OF DROLL CODE
|