Merge remote-tracking branch 'origin/master' into 003-add-character-profile

This commit is contained in:
Johannes Loher 2021-01-04 21:52:26 +01:00
commit 6ba1032a12
7 changed files with 618 additions and 336 deletions

View file

@ -1,272 +0,0 @@
import {
rollCheckMultipleDice,
rollCheckSingleDie,
RollOptions,
RollResult,
RollResultStatus,
} from "../../src/module/rolls/roll-executor";
import { RollProvider } from "../../src/module/rolls/roll-provider";
import "jasmine";
describe("DS4 Rolls with one die and no modifications.", () => {
it("Should do a regular success roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
});
it("Should do a single success roll on success upper edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
});
it("Should do a single failure roll on lower edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(5);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [5]),
);
});
it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [19]),
);
});
it("Should do a single crit success roll on '1'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a single crit failure roll on '20'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rolls with one die and crit roll modifications.", () => {
it("Should do a crit success on `1`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a crit success on `maxCritSucc`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(2);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]),
);
});
it("Should do a success on lower edge case `3`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(3);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(3, RollResultStatus.SUCCESS, [3]),
);
});
it("Should do a success on upper edge case `18`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(18);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [18]),
);
});
it("Should do a crit fail on `minCritFail`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]),
);
});
it("Should do a crit fail on `20`", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rools with multiple dice and no modifiers.", () => {
it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([20, 15, 6]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]),
);
});
it("Should succeed with all rolls crit successes.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([1, 1, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]),
);
});
it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]),
);
});
it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 1, 20]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]),
);
});
});
describe("DS4 Rools with multiple dice and min/max modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
);
});
it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success `2`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]),
);
});
it("Should succeed with the last roll being `20` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 20]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]),
);
});
it("Should succeed with the last roll being `19` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 19]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]),
);
});
});
describe("DS4 Rools with multiple dice and fail modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
});
describe("DS4 Rools with multiple dice and success modifiers.", () => {
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
);
});
});

View file

@ -0,0 +1,341 @@
import { rollCheckMultipleDice, rollCheckSingleDie } from "../../../src/module/rolls/roll-executor";
import { RollProvider } from "../../../src/module/rolls/roll-provider";
import "jasmine";
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.", () => {
it("Should do a regular success roll.", () => {
const rollProvider = mockSingleThrow(4);
expect(rollCheckSingleDie(12, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
});
it("Should do a single success roll on success upper edge case.", () => {
const rollProvider = mockSingleThrow(4);
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
});
it("Should do a single failure roll on lower edge case.", () => {
const rollProvider = mockSingleThrow(5);
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [5]));
});
it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider = mockSingleThrow(19);
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [19]));
});
it("Should do a single crit success roll on '1'.", () => {
const rollProvider = mockSingleThrow(1);
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a single crit failure roll on '20'.", () => {
const rollProvider = mockSingleThrow(20);
expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rolls with one die and slaying dice, first throw.", () => {
it("Should do a crit success on `1`", () => {
const rollProvider = mockSingleThrow(1);
expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a crit fail on `20`", () => {
const rollProvider = mockSingleThrow(20);
expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rolls with one die and slaying dice, followup throw.", () => {
it("Should do a crit success on `1`", () => {
const rollProvider = mockSingleThrow(1);
expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a regular fail on `20`", () => {
const rollProvider = mockSingleThrow(20);
expect(rollCheckSingleDie(4, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [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 }, rollProvider)).toEqual(
new RollResult(20, RollResultStatus.SUCCESS, [20]),
);
});
});
describe("DS4 Rolls with one die and crit roll modifications.", () => {
it("Should do a crit success on `1`.", () => {
const rollProvider = mockSingleThrow(1);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a crit success on `maxCritSucc`.", () => {
const rollProvider = mockSingleThrow(2);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]),
);
});
it("Should do a success on lower edge case `3`.", () => {
const rollProvider = mockSingleThrow(3);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(3, RollResultStatus.SUCCESS, [3]),
);
});
it("Should do a success on upper edge case `18`.", () => {
const rollProvider = mockSingleThrow(18);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [18]),
);
});
it("Should do a crit fail on `minCritFail`.", () => {
const rollProvider = mockSingleThrow(19);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]),
);
});
it("Should do a crit fail on `20`", () => {
const rollProvider = mockSingleThrow(20);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rolls with multiple dice and no modifiers.", () => {
it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider = mockMultipleThrows([20, 15, 6]);
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]),
);
});
it("Should succeed normally with all rolls crit successes.", () => {
const rollProvider = mockMultipleThrows([1, 1, 1]);
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]),
);
});
it("Should succeed with the last roll not being suficient.", () => {
const rollProvider = mockMultipleThrows([15, 15, 15]);
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success.", () => {
const rollProvider = mockMultipleThrows([15, 15, 1]);
expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual(
new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 1]),
);
});
it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([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", () => {
const rollProvider = mockMultipleThrows([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.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider = mockMultipleThrows([19, 15, 6]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider = mockMultipleThrows([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.", () => {
const rollProvider = mockMultipleThrows([15, 15, 15]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success `2`.", () => {
const rollProvider = mockMultipleThrows([15, 15, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 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 }, rollProvider)).toEqual(
new RollResult(43, RollResultStatus.SUCCESS, [20, 15, 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 }, rollProvider)).toEqual(
new RollResult(42, RollResultStatus.SUCCESS, [19, 15, 2]),
);
});
});
describe("DS4 Rolls with multiple dice and fail modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider = mockMultipleThrows([19, 15, 6]);
expect(rollCheckMultipleDice(48, { minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
});
describe("DS4 Rolls with multiple dice and success modifiers.", () => {
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider = mockMultipleThrows([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", () => {
it("Should fail with the first roll being a `20`", () => {
const rollProvider = mockMultipleThrows([20, 2, 19]);
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", () => {
const rollProvider = mockMultipleThrows([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", () => {
it("Should regularly succeed with the first roll being a `20`", () => {
const rollProvider = mockMultipleThrows([20, 2, 19]);
expect(rollCheckMultipleDice(48, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual(
new RollResult(41, RollResultStatus.SUCCESS, [20, 19, 2]),
);
});
});

View file

@ -0,0 +1,24 @@
import "jasmine";
import { isDiceSwapNecessary } from "../../../src/module/rolls/roll-utils";
describe("Utility function testing if dice swap is necessary", () => {
it("Should not swap if all dice are crit successes.", () => {
expect(isDiceSwapNecessary([[1, 1, 1], []], 9)).toBeFalse();
});
it("Should not swap if no die is crit success.", () => {
expect(isDiceSwapNecessary([[], [2, 2, 2]], 9)).toBeFalse();
});
it("Should not swap if all dice are already in use", () => {
expect(isDiceSwapNecessary([[1], [9, 8]], 10)).toBeFalse();
});
it("Should not swap if result does not get any better", () => {
expect(isDiceSwapNecessary([[1], [8]], 4)).toBeFalse();
});
it("Should swap if result does get better", () => {
expect(isDiceSwapNecessary([[1], [19]], 18)).toBeTrue();
});
});

View file

@ -0,0 +1,28 @@
export interface RollOptions {
maxCritSucc: number;
minCritFail: number;
useSlayingDice: boolean;
slayingDiceRepetition: boolean;
}
export class DefaultRollOptions implements RollOptions {
public maxCritSucc = 1;
public minCritFail = 20;
public useSlayingDice = false;
public slayingDiceRepetition = false;
mergeWith(other: Partial<RollOptions>): RollOptions {
return { ...this, ...other } as RollOptions;
}
}
export class RollResult {
constructor(public value: number, public status: RollResultStatus, public dice: Array<number>) {}
}
export enum RollResultStatus {
FAILURE,
SUCCESS,
CRITICAL_FAILURE,
CRITICAL_SUCCESS,
}

View file

@ -1,28 +1,51 @@
import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data";
import { DS4RollProvider, RollProvider } from "./roll-provider";
import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition, separateCriticalHits } from "./roll-utils";
export function ds4test(testValue: number, rollOptions: RollOptions = new RollOptions()): RollResult {
const finalRollValue = testValue;
if (finalRollValue <= 20) {
return rollCheckSingleDie(finalRollValue, rollOptions);
/**
* Performs a roll against a check target number, e.g. for usage in battle, but not for herbs.
* @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 {
if (checkTargetValue <= 20) {
return rollCheckSingleDie(checkTargetValue, rollOptions);
} else {
return rollCheckMultipleDice(finalRollValue, rollOptions);
return rollCheckMultipleDice(checkTargetValue, rollOptions);
}
}
/**
* Performs a roll against a single die (CTN less than or equal 20).
*
* @internal
* This is not intended for direct usage. Use
* {@link ds4roll | the function that is not bound to an amount of Dice} instead.
*
* @remarks
* The `provider` is only exposed for testing.
*
* @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.
*
* @returns {RollResult} An object containing detailed information on the roll result.
*/
export function rollCheckSingleDie(
testValue: number,
rollOptions: RollOptions,
checkTargetValue: number,
rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(),
): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const roll = provider.getNextRoll();
const dice = [roll];
if (roll <= rollOptions.maxCritSucc) {
return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice);
} else if (roll >= rollOptions.minCritFail) {
if (roll <= usedOptions.maxCritSucc) {
return new RollResult(checkTargetValue, RollResultStatus.CRITICAL_SUCCESS, dice);
} else if (roll >= usedOptions.minCritFail && !isSlayingDiceRepetition(usedOptions)) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
} else {
if (roll <= testValue) {
if (roll <= checkTargetValue) {
return new RollResult(roll, RollResultStatus.SUCCESS, dice);
} else {
return new RollResult(0, RollResultStatus.FAILURE, dice);
@ -30,70 +53,60 @@ export function rollCheckSingleDie(
}
}
/**
* Performs a roll against a multitude of die (CTN greater than 20).
*
* @internal
* This is not intended for direct usage. Use
* {@link ds4roll | the function that is not bound to an amount of Dice} instead.
*
* @remarks
* The `provider` is only exposed for testing.
*
* @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.
*
* @returns {RollResult} An object containing detailed information on the roll result.
*/
export function rollCheckMultipleDice(
testValue: number,
rollOptions: RollOptions,
targetValue: number,
rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(),
): RollResult {
const finalCheck = testValue % 20;
const numberOfDice = Math.ceil(testValue / 20);
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const remainderTargetValue = targetValue % 20;
const numberOfDice = Math.ceil(targetValue / 20);
const dice = provider.getNextRolls(numberOfDice);
const firstResult = dice[0];
const slayingDiceRepetition = isSlayingDiceRepetition(usedOptions);
if (firstResult >= rollOptions.minCritFail) {
// Slaying Dice require a different handling.
if (firstResult >= usedOptions.minCritFail && !slayingDiceRepetition) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
}
const partitionCallback = (prev: [Array<number>, Array<number>], cur: number) => {
if (cur <= rollOptions.maxCritSucc) {
prev[0].push(cur);
} else {
prev[1].push(cur);
}
return prev;
};
const [critSuccesses, otherRolls] = separateCriticalHits(dice, usedOptions);
const [critSuccesses, otherRolls] = dice
.reduce(partitionCallback, [[], []])
.map((a) => a.sort((r1, r2) => r2 - r1));
const swapLastWithCrit: boolean = isDiceSwapNecessary([critSuccesses, otherRolls], remainderTargetValue);
const sortedRollResults: Array<number> = critSuccesses.concat(otherRolls);
let sortedRollResults: Array<number>;
const evaluationResult = sortedRollResults
.map((value, index) => {
if (index == numberOfDice - 1) {
if (value == 1) {
return finalCheck;
} else {
return value <= finalCheck ? value : 0;
}
} else {
if (value <= rollOptions.maxCritSucc) {
return 20;
} else {
return value;
}
}
})
.reduce((a, b) => a + b);
if (swapLastWithCrit) {
const diceToMove = critSuccesses[0];
const remainingSuccesses = critSuccesses.slice(1);
sortedRollResults = remainingSuccesses.concat(otherRolls).concat([diceToMove]);
} else {
sortedRollResults = critSuccesses.concat(otherRolls);
}
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults);
}
export class RollOptions {
public maxCritSucc = 1;
public minCritFail = 20;
}
export class RollResult {
constructor(public value: number, public status: RollResultStatus, public dice: Array<number>) {}
}
export enum RollResultStatus {
FAILURE,
SUCCESS,
CRITICAL_FAILURE,
CRITICAL_SUCCESS,
const evaluationResult = calculateRollResult(sortedRollResults, remainderTargetValue, usedOptions);
if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) {
return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, sortedRollResults);
} else {
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults);
}
}

View file

@ -1,4 +1,10 @@
export class DS4RollProvider {
/**
* Runtime-implementation of the {@link RollProvider}.
*
* @remarks
* Do not use for tests, it will inevitably fail because the `Roll` class is only provided from declarations, not as implementation!
*/
export class DS4RollProvider implements RollProvider {
getNextRoll(): number {
return new Roll("1d20").roll().total;
}
@ -10,7 +16,10 @@ export class DS4RollProvider {
}
}
/**
* Provides methods to fetch one or multiple rolls.
*/
export interface RollProvider {
getNextRoll(): number;
getNextRolls(number: number): Array<number>;
getNextRolls(amount: number): Array<number>;
}

View file

@ -0,0 +1,139 @@
import { RollOptions } from "./roll-data";
/**
* Separates critical hits ("Coups") from throws, that get counted with their regular value.
*
* @internal
*
* @private_remarks
* This uses an internal implementation of a `partition` method. Don't let typescript fool you, it will tell you that a partition method is available for Arrays, but that one's imported globally from foundry's declarations and not available during the test stage!
*
* @param {Array<number>} dice - The dice values.
* @param {RollOptions} usedOptions - Options that affect the check's behaviour.
* @returns {[Array<number>, Array<number>]} A tuple containing two arrays of dice values, the first one containing all critical hits, the second one containing all others. Both arrays are sorted descendingby value.
*/
export function separateCriticalHits(dice: Array<number>, usedOptions: RollOptions): CritsAndNonCrits {
const [critSuccesses, otherRolls] = partition(dice, (v: number) => {
return v <= usedOptions.maxCritSucc;
}).map((a) => a.sort((r1, r2) => r2 - r1));
return [critSuccesses, otherRolls];
}
/**
* Helper type to properly bind combinations of critical and non critical dice.
* @internal
*/
type CritsAndNonCrits = [Array<number>, Array<number>];
/**
* Partition an array into two, following a predicate.
* @param {Array<T>} input The Array to split.
* @param {(T) => boolean} predicate The predicate by which to split.
* @returns A tuple of two arrays, the first one containing all elements from `input` that matched the predicate, the second one containing those that don't.
*/
// TODO: Move to generic utils method?
function partition<T>(input: Array<T>, predicate: (v: T) => boolean) {
return input.reduce(
(p: [Array<T>, Array<T>], cur: T) => {
if (predicate(cur)) {
p[0].push(cur);
} else {
p[1].push(cur);
}
return p;
},
[[], []],
);
}
/**
* Calculates if a critical success should be moved to the last position in order to maximize the check's result.
*
* @example
* With regular dice rolling rules and a check target number of 31, the two dice 1 and 19 can get to a check result of 30.
* This method would be called as follows:
* ```
* isDiceSwapNecessary([[1], [19]], 11)
* ```
*
* @param {[Array<number>, Array<number>]} critsAndNonCrits the dice values thrown. It is assumed that both critical successes and other rolls are sorted descending.
* @param {number} remainingTargetValue the target value for the last dice, that is the only one that can be less than 20.
* @returns {boolean} Bool indicating whether a critical success has to be used as the last dice.
*/
export function isDiceSwapNecessary(
[critSuccesses, otherRolls]: CritsAndNonCrits,
remainingTargetValue: number,
): boolean {
if (critSuccesses.length == 0 || otherRolls.length == 0) {
return false;
}
const amountOfOtherRolls = otherRolls.length;
const lastDice = otherRolls[amountOfOtherRolls - 1];
if (lastDice <= remainingTargetValue) {
return false;
}
return lastDice + remainingTargetValue > 20;
}
/**
* Checks if the options indicate that the current check is emerging from a crit success on a roll with slaying dice.
*
* @internal
*
* @param {RollOptions} opts the roll options to check against
*/
export function isSlayingDiceRepetition(opts: RollOptions): boolean {
return opts.useSlayingDice && opts.slayingDiceRepetition;
}
/**
* Calculate the check value of an array of dice, assuming the dice should be used in order of occurence.
*
* @internal
*
* @param assignedRollResults The dice values in the order of usage.
* @param remainderTargetValue Target value for the last dice (the only one differing from `20`).
* @param rollOptions Config object containing options that change the way dice results are handled.
*
* @returns {number} The total check value.
*/
export function calculateRollResult(
assignedRollResults: Array<number>,
remainderTargetValue: number,
rollOptions: RollOptions,
): number {
const numberOfDice = assignedRollResults.length;
const maxResultPerDie: Array<number> = Array(numberOfDice).fill(20);
maxResultPerDie[numberOfDice - 1] = remainderTargetValue;
const rollsAndMaxValues = zip(assignedRollResults, maxResultPerDie);
return rollsAndMaxValues
.map(([v, m]) => {
return v <= rollOptions.maxCritSucc ? [m, m] : [v, m];
})
.filter(([v, m]) => v <= m)
.map(([v]) => v)
.reduce((a, b) => a + b);
}
// TODO: Move to generic utils method?
/**
* Zips two Arrays to an array of pairs of elements with corresponding indices. Excessive elements are dropped.
* @param {Array<T>} a1 First array to zip.
* @param {Array<U>} a2 Second array to zip.
*
* @typeParam T - Type of elements contained in `a1`.
* @typeParam U - Type of elements contained in `a2`.
*
* @returns {Array<[T,U]>} The array of pairs that had the same index in their source array.
*/
function zip<T, U>(a1: Array<T>, a2: Array<U>): Array<[T, U]> {
if (a1.length <= a2.length) {
return a1.map((e1, i) => [e1, a2[i]]);
} else {
return a2.map((e2, i) => [a1[i], e2]);
}
}