UNPKG

@seroh/roll

Version:

An RPG dice-rolling library with a variety of built-in roll mechanics.

34 lines 1.28 kB
import { Mechanic } from "./Mechanic"; export class RerollMechanic extends Mechanic { options; constructor(options) { super(); this.options = options; this.options.maxRerollCount = options.maxRerollCount ?? 1; this.validateOptions(options); } do(min, max, randomizer) { const rolls = []; let result = randomizer.generate(min, max); let remainingRerolls = this.options.maxRerollCount ?? 0; rolls.push(result); while (this.options.target.includes(result) && remainingRerolls > 0) { result = randomizer.generate(min, max); rolls.push(result); remainingRerolls--; } return { result, rolls }; } validateOptions({ target: target, maxRerollCount: times }) { if (target.length === 0) { throw new Error("Target can not be an empty array."); } if (target.some((target) => typeof target !== "number")) { throw new Error("All values in the target array must be numbers."); } if (typeof times !== "number" || !Number.isInteger(times) || times <= 0) { throw new Error("Times must be a positive integer."); } } } //# sourceMappingURL=RerollMechanic.js.map