dicelang
Version:
JavaScript interpreter of the Roll20 dice language
209 lines (208 loc) • 6.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("../Common/Constants");
var Random_1 = require("../Common/Random");
var DiceMod_1 = require("./DiceMod");
var RD = new Random_1.RandomDevice();
var Dice = (function () {
function Dice(diceExpr, binding, device) {
if (device === void 0) { device = RD; }
this._minRoll = 1;
this._fate = false;
this._result = null;
this._rolls = [];
this._rawRolls = [];
this._device = device;
if (!diceExpr) {
this._d = 20;
this._n = 1;
this._mod = new DiceMod_1.DiceMod();
}
else {
var parseResult = Dice.diceRegExp.exec(diceExpr);
if (parseResult == null) {
throw new Error("\"" + diceExpr + "\" is not a valid expression.");
}
this._mod = new DiceMod_1.DiceMod(parseResult[3]);
if (parseResult[1] === undefined) {
this._n = 1;
}
else {
var n = parseInt(parseResult[1], 10);
Dice.checkN(n);
this._n = n;
}
if (parseResult[2] === 'f' || parseResult[2] === 'F') {
this.fate = true;
}
else {
var d = parseInt(parseResult[2], 10);
Dice.checkD(d);
this._fate = false;
this._d = d;
}
}
}
Dice.roll = function (d, n, device) {
if (device === void 0) { device = RD; }
Dice.checkD(d);
if (n === undefined) {
n = 1;
}
Dice.checkN(n);
var result = [];
for (var i = 0; i < n; ++i) {
result.push(device.randomInt(d));
}
return result;
};
Object.defineProperty(Dice, "maxD", {
get: function () {
return Dice._maxD;
},
set: function (d) {
Dice._maxD = d;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice, "maxN", {
get: function () {
return Dice._maxN;
},
set: function (n) {
Dice._maxN = n;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice, "diceRegExp", {
get: function () {
return /^(\d+)?d(f|\d+)(.*)$/ig;
},
enumerable: true,
configurable: true
});
Dice.checkD = function (d) {
if (d < 0 || d > Dice.maxD) {
throw new Error("The value of n must be between 0 and " + Dice.maxD + " inclusive (got " + d + ")");
}
};
Dice.checkN = function (n) {
if (n < 0 || n > Dice.maxN) {
throw new Error("The value of n must be between 0 and " + Dice.maxN + " inclusive (got " + n + ")");
}
};
Object.defineProperty(Dice.prototype, "n", {
get: function () {
return this._n;
},
set: function (value) {
value = Math.round(value);
Dice.checkN(value);
this._n = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "d", {
get: function () {
if (this._fate) {
return 3;
}
else {
return this._d;
}
},
set: function (value) {
value = Math.round(value);
Dice.checkD(value);
this._d = value;
this._fate = false;
this._minRoll = 1;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "fate", {
get: function () {
return this._fate;
},
set: function (value) {
value = !!value;
if (value !== this._fate) {
this._fate = value;
if (value) {
this._d = 1;
this._minRoll = -1;
}
else {
this._d = 3;
this._minRoll = 1;
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "mod", {
get: function () {
return this._mod;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "minRoll", {
get: function () {
return this._minRoll;
},
enumerable: true,
configurable: true
});
Dice.prototype.roll = function (n) {
if (n === void 0) { n = this.n; }
Dice.checkN(n);
var rolls = [];
this._rolls.length = 0;
this._rawRolls.length = 0;
for (var i = 0; i < n; ++i) {
var roll = this._device.randomInt(this._d, this._minRoll);
this._rawRolls.push(roll);
this._mod.rolled(roll, rolls, this);
}
this._result = this._mod.modResult(rolls);
this._rolls = rolls;
return this.result;
};
Object.defineProperty(Dice.prototype, "result", {
get: function () {
return this._result;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "rolls", {
get: function () {
return this._rolls.slice();
},
enumerable: true,
configurable: true
});
Object.defineProperty(Dice.prototype, "rawRolls", {
get: function () {
return this._rawRolls.slice();
},
enumerable: true,
configurable: true
});
Dice.prototype.toString = function () {
return this._n + "d" + (this._fate ? 'F' : this._d) + this._mod.toString();
};
Dice.prototype.toStringPlaintext = function () {
return "Roll " + this._n + " " + (this._fate ? 'fate dice' : "d" + this._d + (this._n > 1 ? 's' : '')) + ". " + this._mod.toStringPlaintext();
};
return Dice;
}());
Dice._maxD = Constants_1.MAX_JS_INT;
Dice._maxN = Constants_1.MAX_JS_INT;
exports.Dice = Dice;