UNPKG

@half-elf/rogue

Version:

Utility library for all underhanded d20 needs.

42 lines (41 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.roll = void 0; const dice_typescript_1 = require("dice-typescript"); class OptionalRandomProvider { constructor(option) { this.opt = option; } numberBetween(min, max) { switch (this.opt) { case 0 /* MAX */: return max; case 1 /* MIN */: return min; case 2 /* AVG */: return max / 2 + 0.5; } } } const dice = new dice_typescript_1.Dice(); const maxDice = new dice_typescript_1.Dice(undefined, new OptionalRandomProvider(0 /* MAX */)); const minDice = new dice_typescript_1.Dice(undefined, new OptionalRandomProvider(1 /* MIN */)); const avgDice = new dice_typescript_1.Dice(undefined, new OptionalRandomProvider(2 /* AVG */)); function roll(input, options) { const inputWithoutBigD = input.replace(/D/g, 'd'); const diceResult = dice.roll(inputWithoutBigD); if (diceResult.errors.length) { diceResult.errors.forEach(console.error); throw new Error('That is not a valid piece of dice notation.'); } if (options) { const { max, min, avg } = options; const rollResult = Object.assign(Object.assign({}, diceResult), { input: input }); if (max) rollResult.max = maxDice.roll(inputWithoutBigD).total; if (min) rollResult.min = minDice.roll(inputWithoutBigD).total; if (avg) rollResult.avg = avgDice.roll(inputWithoutBigD).total; return rollResult; } return diceResult.total; } exports.roll = roll;