ds4/src/rolls/slaying-dice-modifier.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
//
// SPDX-License-Identifier: MIT
2021-07-07 19:22:35 +02:00
import { getGame } from "../helpers";
2021-03-13 22:08:04 +01:00
import { DS4Check } from "./check";
export default function registerSlayingDiceModifier(): void {
2021-06-30 04:32:10 +02:00
PoolTerm.MODIFIERS.x = slay;
2021-03-13 22:08:04 +01:00
}
2021-06-30 04:32:10 +02:00
function slay(this: PoolTerm, modifier: string): void {
2021-03-13 22:08:04 +01:00
const rgx = /[xX]/;
const match = modifier.match(rgx);
if (!match || !this.rolls) return;
let checked = 0;
while (checked < (this.dice.length ?? 0)) {
const diceTerm = this.dice[checked];
checked++;
if (diceTerm instanceof DS4Check && diceTerm.coup) {
const formula = `dsv${diceTerm.checkTargetNumber}c${diceTerm.maximumCoupResult}:${diceTerm.minimumFumbleResult}n`;
2021-07-01 01:51:50 +02:00
const additionalRoll = Roll.create(formula).evaluate({ async: false });
2021-03-13 22:08:04 +01:00
this.rolls.push(additionalRoll);
this.results.push({ result: additionalRoll.total ?? 0, active: true });
2021-07-01 01:51:50 +02:00
this.terms.push(formula);
2021-03-13 22:08:04 +01:00
}
2021-07-07 19:22:35 +02:00
if (checked > 1000) throw new Error(getGame().i18n.localize("DS4.ErrorSlayingDiceRecursionLimitExceeded"));
2021-03-13 22:08:04 +01:00
}
}