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

@ -1,76 +1,43 @@
import { rollCheckMultipleDice, rollCheckSingleDie } from "../../../src/module/rolls/roll-executor"; import { rollCheckMultipleDice, rollCheckSingleDie } from "../../../src/module/rolls/roll-executor";
import { RollProvider } from "../../../src/module/rolls/roll-provider";
import "jasmine"; import "jasmine";
import { RollResult, RollResultStatus } from "../../../src/module/rolls/roll-data"; import { RollResult, RollResultStatus } from "../../../src/module/rolls/roll-data";
function mockSingleThrow(value: number): RollProvider {
const rollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(value);
return rollProvider;
}
function mockMultipleThrows(values: Array<number>): RollProvider {
const rollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue(values);
return rollProvider;
}
describe("DS4 Rolls with one die and no modifications.", () => { describe("DS4 Rolls with one die and no modifications.", () => {
it("Should do a regular success roll.", () => { it("Should do a regular success roll.", () => {
const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(12, {}, [4])).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4], true));
expect(rollCheckSingleDie(12, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
}); });
it("Should do a single success roll on success upper edge case.", () => { it("Should do a single success roll on success upper edge case.", () => {
const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(4, {}, [4])).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4], true));
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
}); });
it("Should do a single failure roll on lower edge case.", () => { it("Should do a single failure roll on lower edge case.", () => {
const rollProvider = mockSingleThrow(5); expect(rollCheckSingleDie(4, {}, [5])).toEqual(new RollResult(0, RollResultStatus.FAILURE, [5], true));
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [5]));
}); });
it("Should do a single failure roll on upper edge case '19'.", () => { it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, {}, [19])).toEqual(new RollResult(0, RollResultStatus.FAILURE, [19]));
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [19]));
}); });
it("Should do a single crit success roll on '1'.", () => { it("Should do a single crit success roll on '1'.", () => {
const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, {}, [1])).toEqual(new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1], true));
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
}); });
it("Should do a single crit failure roll on '20'.", () => { it("Should do a single crit failure roll on '20'.", () => {
const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, {}, [20])).toEqual(new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]));
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
}); });
}); });
describe("DS4 Rolls with one die and slaying dice, first throw.", () => { describe("DS4 Rolls with one die and slaying dice, first throw.", () => {
it("Should do a crit success on `1`", () => { it("Should do a crit success on `1`", () => {
const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, { useSlayingDice: true }, [1])).toEqual(
expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
); );
}); });
it("Should do a crit fail on `20`", () => { it("Should do a crit fail on `20`", () => {
const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, { useSlayingDice: true }, [20])).toEqual(
expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
); );
}); });
@ -78,25 +45,19 @@ describe("DS4 Rolls with one die and slaying dice, first throw.", () => {
describe("DS4 Rolls with one die and slaying dice, followup throw.", () => { describe("DS4 Rolls with one die and slaying dice, followup throw.", () => {
it("Should do a crit success on `1`", () => { it("Should do a crit success on `1`", () => {
const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, [1])).toEqual(
expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
); );
}); });
it("Should do a regular fail on `20`", () => { it("Should do a regular fail on `20`", () => {
const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, [20])).toEqual(
expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [20]), new RollResult(0, RollResultStatus.FAILURE, [20]),
); );
}); });
it("Should do a regular success on `20` with a CTN of 20", () => { it("Should do a regular success on `20` with a CTN of 20", () => {
const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(20, { useSlayingDice: true, slayingDiceRepetition: true }, [20])).toEqual(
expect(rollCheckSingleDie(20, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(20, RollResultStatus.SUCCESS, [20]), new RollResult(20, RollResultStatus.SUCCESS, [20]),
); );
}); });
@ -104,49 +65,37 @@ describe("DS4 Rolls with one die and slaying dice, followup throw.", () => {
describe("DS4 Rolls with one die and crit roll modifications.", () => { describe("DS4 Rolls with one die and crit roll modifications.", () => {
it("Should do a crit success on `1`.", () => { it("Should do a crit success on `1`.", () => {
const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [1])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
); );
}); });
it("Should do a crit success on `maxCritSucc`.", () => { it("Should do a crit success on `maxCritSucc`.", () => {
const rollProvider = mockSingleThrow(2); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [2])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]),
); );
}); });
it("Should do a success on lower edge case `3`.", () => { it("Should do a success on lower edge case `3`.", () => {
const rollProvider = mockSingleThrow(3); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [3])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(3, RollResultStatus.SUCCESS, [3]), new RollResult(3, RollResultStatus.SUCCESS, [3]),
); );
}); });
it("Should do a success on upper edge case `18`.", () => { it("Should do a success on upper edge case `18`.", () => {
const rollProvider = mockSingleThrow(18); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [18])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [18]), new RollResult(0, RollResultStatus.FAILURE, [18]),
); );
}); });
it("Should do a crit fail on `minCritFail`.", () => { it("Should do a crit fail on `minCritFail`.", () => {
const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [19])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]),
); );
}); });
it("Should do a crit fail on `20`", () => { it("Should do a crit fail on `20`", () => {
const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, [20])).toEqual(
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
); );
}); });
@ -154,149 +103,109 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => {
describe("DS4 Rolls with multiple dice and no modifiers.", () => { describe("DS4 Rolls with multiple dice and no modifiers.", () => {
it("Should do a crit fail on `20` for first roll.", () => { it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider = mockMultipleThrows([20, 15, 6]); expect(rollCheckMultipleDice(48, {}, [20, 15, 6])).toEqual(
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]),
); );
}); });
it("Should succeed normally with all rolls crit successes.", () => { it("Should succeed normally with all rolls crit successes.", () => {
const rollProvider = mockMultipleThrows([1, 1, 1]); expect(rollCheckMultipleDice(48, {}, [1, 1, 1])).toEqual(
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]), new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]),
); );
}); });
it("Should succeed with the last roll not being suficient.", () => { it("Should succeed with the last roll not being suficient.", () => {
const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, {}, [15, 15, 15])).toEqual(
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
); );
}); });
it("Should succeed with the last roll a crit success.", () => { it("Should succeed with the last roll a crit success.", () => {
const rollProvider = mockMultipleThrows([15, 15, 1]); expect(rollCheckMultipleDice(48, {}, [15, 15, 1])).toEqual(
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 1]), new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 1]),
); );
}); });
it("Should succeed with the last roll being 20 and one crit success.", () => { it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider = mockMultipleThrows([15, 1, 20]); expect(rollCheckMultipleDice(48, {}, [15, 1, 20])).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [15, 1, 20]),
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [20, 15, 1]),
); );
}); });
it("Should properly maximize throw result with all dice success.", () => { it("Should properly maximize throw result with all dice success.", () => {
const rollProvider = mockMultipleThrows([15, 4, 12]); expect(rollCheckMultipleDice(46, {}, [15, 4, 12])).toEqual(
new RollResult(31, RollResultStatus.SUCCESS, [15, 4, 12]),
expect(rollCheckMultipleDice(46, {}, rollProvider)).toEqual(
new RollResult(31, RollResultStatus.SUCCESS, [15, 12, 4]),
); );
}); });
it("Should properly maximize throw result with one dice a failure.", () => { it("Should properly maximize throw result with one dice a failure.", () => {
const rollProvider = mockMultipleThrows([15, 8, 20]); expect(rollCheckMultipleDice(46, {}, [15, 8, 20])).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [15, 8, 20]),
expect(rollCheckMultipleDice(46, {}, rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [20, 15, 8]),
); );
}); });
it("Should maximize on 'lowest dice higher than last CTN and crit success thrown'-Edge case, no change required.", () => { it("Should maximize on 'lowest dice higher than last CTN and crit success thrown'-Edge case, no change required.", () => {
const rollProvider = mockMultipleThrows([15, 1, 8]); expect(rollCheckMultipleDice(46, {}, [15, 1, 8])).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [15, 1, 8]),
expect(rollCheckMultipleDice(46, {}, rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 8]),
); );
}); });
it("Should maximize on 2-dice 'lowest dice higher than last CTN and crit success thrown'-Edge case, no change required.", () => { it("Should maximize on 2-dice 'lowest dice higher than last CTN and crit success thrown'-Edge case, no change required.", () => {
const rollProvider = mockMultipleThrows([1, 8]); expect(rollCheckMultipleDice(24, {}, [1, 8])).toEqual(new RollResult(20, RollResultStatus.SUCCESS, [1, 8]));
expect(rollCheckMultipleDice(24, {}, rollProvider)).toEqual(
new RollResult(20, RollResultStatus.SUCCESS, [1, 8]),
);
}); });
it("Should maximize on 2-dice 'lowest dice higher than last CTN and crit success thrown'-Edge case, change required.", () => { it("Should maximize on 2-dice 'lowest dice higher than last CTN and crit success thrown'-Edge case, change required.", () => {
const rollProvider = mockMultipleThrows([1, 19]); expect(rollCheckMultipleDice(38, {}, [1, 19])).toEqual(new RollResult(37, RollResultStatus.SUCCESS, [1, 19]));
expect(rollCheckMultipleDice(38, {}, rollProvider)).toEqual(
new RollResult(37, RollResultStatus.SUCCESS, [19, 1]),
);
}); });
it("Should maximize correctly when swapping with more than one crit success", () => { it("Should maximize correctly when swapping with more than one crit success", () => {
const rollProvider = mockMultipleThrows([1, 1, 15]); expect(rollCheckMultipleDice(48, {}, [1, 1, 15])).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [1, 1, 15]),
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [1, 15, 1]),
); );
}); });
}); });
describe("DS4 Rolls with multiple dice and min/max modifiers.", () => { describe("DS4 Rolls with multiple dice and min/max modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => { it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [19, 15, 6])).toEqual(
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
); );
}); });
it("Should succeed with all rolls crit successes (1 and 2).", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [2, 1, 2])).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 1, 2]),
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
); );
}); });
it("Should succeed with the last roll not being sufficient.", () => { it("Should succeed with the last roll not being sufficient.", () => {
const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [15, 15, 15])).toEqual(
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
); );
}); });
it("Should succeed with the last roll a crit success `2`.", () => { it("Should succeed with the last roll a crit success `2`.", () => {
const rollProvider = mockMultipleThrows([15, 15, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [15, 15, 2])).toEqual(
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 2]), new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 2]),
); );
}); });
it("Should succeed with the last roll being `20` and one crit success '2'.", () => { it("Should succeed with the last roll being `20` and one crit success '2'.", () => {
const rollProvider = mockMultipleThrows([15, 2, 20]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [15, 2, 20])).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [15, 2, 20]),
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [20, 15, 2]),
); );
}); });
it("Should succeed with the last roll being `19` and one crit success '2'.", () => { it("Should succeed with the last roll being `19` and one crit success '2'.", () => {
const rollProvider = mockMultipleThrows([15, 2, 19]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, [15, 2, 19])).toEqual(
new RollResult(42, RollResultStatus.SUCCESS, [15, 2, 19]),
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(42, RollResultStatus.SUCCESS, [19, 15, 2]),
); );
}); });
}); });
describe("DS4 Rolls with multiple dice and fail modifiers.", () => { describe("DS4 Rolls with multiple dice and fail modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => { it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { minCritFail: 19 }, [19, 15, 6])).toEqual(
expect(rollCheckMultipleDice(48, { minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
); );
}); });
@ -304,38 +213,30 @@ describe("DS4 Rolls with multiple dice and fail modifiers.", () => {
describe("DS4 Rolls with multiple dice and success modifiers.", () => { describe("DS4 Rolls with multiple dice and success modifiers.", () => {
it("Should succeed with all rolls crit successes (1 and 2).", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2 }, [2, 1, 2])).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 1, 2]),
expect(rollCheckMultipleDice(48, { maxCritSucc: 2 }, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
); );
}); });
}); });
describe("DS4 Rolls with multiple and slaying dice, first throw", () => { describe("DS4 Rolls with multiple and slaying dice, first throw", () => {
it("Should fail with the first roll being a `20`", () => { it("Should fail with the first roll being a `20`", () => {
const rollProvider = mockMultipleThrows([20, 2, 19]); expect(rollCheckMultipleDice(48, { useSlayingDice: true }, [20, 2, 19])).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 2, 19], true),
expect(rollCheckMultipleDice(48, { useSlayingDice: true }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 2, 19]),
); );
}); });
it("Should issue a critical success, even with resorting dice", () => { it("Should issue a critical success, even with resorting dice", () => {
const rollProvider = mockMultipleThrows([2, 19, 15]); expect(rollCheckMultipleDice(48, { useSlayingDice: true, maxCritSucc: 2 }, [2, 19, 15])).toEqual(
new RollResult(42, RollResultStatus.CRITICAL_SUCCESS, [2, 19, 15]),
expect(rollCheckMultipleDice(48, { useSlayingDice: true, maxCritSucc: 2 }, rollProvider)).toEqual(
new RollResult(42, RollResultStatus.CRITICAL_SUCCESS, [19, 15, 2]),
); );
}); });
}); });
describe("DS4 Rolls with multiple and slaying dice, recurrent throw", () => { describe("DS4 Rolls with multiple and slaying dice, recurrent throw", () => {
it("Should regularly succeed with the first roll being a `20`", () => { it("Should regularly succeed with the first roll being a `20`", () => {
const rollProvider = mockMultipleThrows([20, 2, 19]); expect(rollCheckMultipleDice(48, { useSlayingDice: true, slayingDiceRepetition: true }, [20, 2, 19])).toEqual(
new RollResult(41, RollResultStatus.SUCCESS, [20, 2, 19]),
expect(rollCheckMultipleDice(48, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(41, RollResultStatus.SUCCESS, [20, 19, 2]),
); );
}); });
}); });

View file

@ -17,7 +17,12 @@ export class DefaultRollOptions implements RollOptions {
} }
export class RollResult { 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 { export enum RollResultStatus {

View file

@ -1,5 +1,5 @@
import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data"; 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"; 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 {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. * @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) { if (checkTargetValue <= 20) {
return rollCheckSingleDie(checkTargetValue, rollOptions); return rollCheckSingleDie(checkTargetValue, rollOptions, dice);
} else { } 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( export function rollCheckSingleDie(
checkTargetValue: number, checkTargetValue: number,
rollOptions: Partial<RollOptions>, rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(), dice: Array<number> = null,
): RollResult { ): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); 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) { 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)) { } 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 { } else {
if (roll <= checkTargetValue) { if (roll <= checkTargetValue) {
return new RollResult(roll, RollResultStatus.SUCCESS, dice); return new RollResult(roll, RollResultStatus.SUCCESS, usedDice, true);
} else { } 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 {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 {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 {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. * @returns {RollResult} An object containing detailed information on the roll result.
*/ */
export function rollCheckMultipleDice( export function rollCheckMultipleDice(
targetValue: number, targetValue: number,
rollOptions: Partial<RollOptions>, rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(), dice: Array<number> = null,
): RollResult { ): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const remainderTargetValue = targetValue % 20; const remainderTargetValue = targetValue % 20;
const numberOfDice = Math.ceil(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); const slayingDiceRepetition = isSlayingDiceRepetition(usedOptions);
// Slaying Dice require a different handling. // Slaying Dice require a different handling.
if (firstResult >= usedOptions.minCritFail && !slayingDiceRepetition) { 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); const swapLastWithCrit: boolean = isDiceSwapNecessary([critSuccesses, otherRolls], remainderTargetValue);
@ -105,8 +119,8 @@ export function rollCheckMultipleDice(
const evaluationResult = calculateRollResult(sortedRollResults, remainderTargetValue, usedOptions); const evaluationResult = calculateRollResult(sortedRollResults, remainderTargetValue, usedOptions);
if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) { if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) {
return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, sortedRollResults); return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, usedDice, true);
} else { } else {
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); return new RollResult(evaluationResult, RollResultStatus.SUCCESS, usedDice, true);
} }
} }