dicelang
Version:
JavaScript interpreter of the Roll20 dice language
93 lines (92 loc) • 3.06 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("./Constants");
var getRandomValues;
try {
getRandomValues = require('get-random-values');
}
catch (e) {
console.error(new Error('Enviroment does not support the crypto API.'));
}
;
var BaseRandomDevice = (function () {
function BaseRandomDevice() {
}
BaseRandomDevice.prototype.randomInt = function (max, min) {
if (min === void 0) { min = 1; }
min = Math.round(min);
max = Math.round(max);
if (min === max) {
return min;
}
else if (max < min) {
return 0;
}
else {
return Math.floor(this.randomReal() * (max - min + 1)) + min;
}
};
return BaseRandomDevice;
}());
exports.BaseRandomDevice = BaseRandomDevice;
var RandomDevice = (function (_super) {
__extends(RandomDevice, _super);
function RandomDevice() {
var _this = _super.call(this) || this;
_this._buff = new ArrayBuffer(8);
return _this;
}
RandomDevice.prototype.randomReal = function () {
if (getRandomValues) {
return this._getRandomNumberFromArrayBuff();
}
else {
return Math.random();
}
};
RandomDevice.prototype._getRandomNumberFromArrayBuff = function () {
getRandomValues(new Uint8Array(this._buff));
var view = new Uint32Array(this._buff);
var lsb = view[1];
var msb = view[0];
msb = msb & Constants_1.INT53_UPPER_MASK;
msb *= Constants_1.POW_2_32;
return (msb + lsb) / Constants_1.MAX_JS_INT;
};
return RandomDevice;
}(BaseRandomDevice));
exports.RandomDevice = RandomDevice;
;
var NonRandomDevice = (function (_super) {
__extends(NonRandomDevice, _super);
function NonRandomDevice(real, int) {
var _this = _super.call(this) || this;
_this._realGen = real;
_this._intGen = int;
return _this;
}
NonRandomDevice.prototype.randomReal = function () {
return this._realGen();
};
NonRandomDevice.prototype.randomInt = function (max, min) {
if (min === void 0) { min = 1; }
if (this._intGen) {
return this._intGen();
}
else {
return _super.prototype.randomInt.call(this, min, max);
}
};
return NonRandomDevice;
}(BaseRandomDevice));
exports.NonRandomDevice = NonRandomDevice;