Make roll executor fir for modifier-approach.

This commit is contained in:
Oliver Rümpelein 2021-01-06 18:27:22 +01:00
parent 2db60b1b76
commit 9feaa5216d
3 changed files with 89 additions and 169 deletions

View file

@ -17,7 +17,12 @@ export class DefaultRollOptions implements RollOptions {
}
export class RollResult {
constructor(public value: number, public status: RollResultStatus, public dice: Array<number>) {}
constructor(
public value: number,
public status: RollResultStatus,
public dice: Array<number>,
public active: boolean = true,
) {}
}
export enum RollResultStatus {

View file

@ -1,5 +1,5 @@
import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data";
import { DS4RollProvider, RollProvider } from "./roll-provider";
import { DS4RollProvider } from "./roll-provider";
import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition, separateCriticalHits } from "./roll-utils";
/**
@ -7,11 +7,15 @@ import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition, sepa
* @param {number} checkTargetValue the final CTN, including all static modifiers.
* @param {Partial<RollOptions>} rollOptions optional, final option override that affect the checks outcome, e.g. different values for crits or whether slaying dice are used.
*/
export function ds4roll(checkTargetValue: number, rollOptions: Partial<RollOptions> = {}): RollResult {
export function ds4roll(
checkTargetValue: number,
rollOptions: Partial<RollOptions> = {},
dice: Array<number> = null,
): RollResult {
if (checkTargetValue <= 20) {
return rollCheckSingleDie(checkTargetValue, rollOptions);
return rollCheckSingleDie(checkTargetValue, rollOptions, dice);
} else {
return rollCheckMultipleDice(checkTargetValue, rollOptions);
return rollCheckMultipleDice(checkTargetValue, rollOptions, dice);
}
}
@ -34,21 +38,27 @@ export function ds4roll(checkTargetValue: number, rollOptions: Partial<RollOptio
export function rollCheckSingleDie(
checkTargetValue: number,
rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(),
dice: Array<number> = null,
): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const roll = provider.getNextRoll();
const dice = [roll];
if (dice == null || dice.length != 1) {
console.error("Rolled a dice!");
// TODO: Make "twist" from foundry.js available!
dice = [new DS4RollProvider().getNextRoll()];
}
const usedDice = dice;
const roll = usedDice[0];
if (roll <= usedOptions.maxCritSucc) {
return new RollResult(checkTargetValue, RollResultStatus.CRITICAL_SUCCESS, dice);
return new RollResult(checkTargetValue, RollResultStatus.CRITICAL_SUCCESS, usedDice, true);
} else if (roll >= usedOptions.minCritFail && !isSlayingDiceRepetition(usedOptions)) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, usedDice, true);
} else {
if (roll <= checkTargetValue) {
return new RollResult(roll, RollResultStatus.SUCCESS, dice);
return new RollResult(roll, RollResultStatus.SUCCESS, usedDice, true);
} else {
return new RollResult(0, RollResultStatus.FAILURE, dice);
return new RollResult(0, RollResultStatus.FAILURE, usedDice, true);
}
}
}
@ -66,29 +76,33 @@ export function rollCheckSingleDie(
* @param {number} checkTargetValue- - The target value to check against.
* @param {RollOptions} rollOptions - Options that affect the checks outcome, e.g. different values for crits or whether slaying dice are used.
* @param {RollProvider} provider - Service providing the various, real dice throws.
* @param {Array<number>} dice - Optional array of dice values to consider.
*
* @returns {RollResult} An object containing detailed information on the roll result.
*/
export function rollCheckMultipleDice(
targetValue: number,
rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(),
dice: Array<number> = null,
): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const remainderTargetValue = targetValue % 20;
const numberOfDice = Math.ceil(targetValue / 20);
const dice = provider.getNextRolls(numberOfDice);
if (!dice || dice.length != numberOfDice) {
dice = new DS4RollProvider().getNextRolls(numberOfDice);
}
const usedDice = dice;
const firstResult = dice[0];
const firstResult = usedDice[0];
const slayingDiceRepetition = isSlayingDiceRepetition(usedOptions);
// Slaying Dice require a different handling.
if (firstResult >= usedOptions.minCritFail && !slayingDiceRepetition) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, usedDice, true);
}
const [critSuccesses, otherRolls] = separateCriticalHits(dice, usedOptions);
const [critSuccesses, otherRolls] = separateCriticalHits(usedDice, usedOptions);
const swapLastWithCrit: boolean = isDiceSwapNecessary([critSuccesses, otherRolls], remainderTargetValue);
@ -105,8 +119,8 @@ export function rollCheckMultipleDice(
const evaluationResult = calculateRollResult(sortedRollResults, remainderTargetValue, usedOptions);
if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) {
return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, sortedRollResults);
return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, usedDice, true);
} else {
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults);
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, usedDice, true);
}
}