diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls.spec.ts index ec899376..282855a8 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls.spec.ts @@ -25,39 +25,31 @@ describe("DS4 Rolls with one die and no modifications.", () => { it("Should do a regular success roll.", () => { const rollProvider = mockSingleThrow(4); - expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual( - new RollResult(4, RollResultStatus.SUCCESS, [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, new RollOptions(), rollProvider)).toEqual( - new RollResult(4, RollResultStatus.SUCCESS, [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, new RollOptions(), rollProvider)).toEqual( - new RollResult(0, RollResultStatus.FAILURE, [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, new RollOptions(), rollProvider)).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'.", () => { const rollProvider = mockSingleThrow(1); - expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), ); }); @@ -65,7 +57,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { it("Should do a single crit failure roll on '20'.", () => { const rollProvider = mockSingleThrow(20); - expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), ); }); @@ -169,15 +161,15 @@ describe("DS4 Rools 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, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]), ); }); - it("Should succeed with all rolls crit successes.", () => { + it("Should succeed normally with all rolls crit successes.", () => { const rollProvider = mockMultipleThrows([1, 1, 1]); - expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]), ); }); @@ -185,7 +177,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { it("Should succeed with the last roll not being suficient.", () => { const rollProvider = mockMultipleThrows([15, 15, 15]); - expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), ); }); @@ -193,7 +185,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { it("Should succeed with the last roll a crit success.", () => { const rollProvider = mockMultipleThrows([15, 15, 1]); - expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]), ); }); @@ -201,7 +193,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { it("Should succeed with the last roll being 20 and one crit success.", () => { const rollProvider = mockMultipleThrows([15, 1, 20]); - expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), ); }); diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index 102be1db..3cea5206 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -1,6 +1,6 @@ import { DS4RollProvider, RollProvider } from "./roll-provider"; -export function ds4test(testValue: number, rollOptions: RollOptions = new RollOptions()): RollResult { +export function ds4test(testValue: number, rollOptions: Partial = {}): RollResult { const finalRollValue = testValue; if (finalRollValue <= 20) { return rollCheckSingleDie(finalRollValue, rollOptions); @@ -11,15 +11,16 @@ export function ds4test(testValue: number, rollOptions: RollOptions = new RollOp export function rollCheckSingleDie( testValue: number, - rollOptions: RollOptions, + rollOptions: Partial, provider: RollProvider = new DS4RollProvider(), ): RollResult { + const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); const roll = provider.getNextRoll(); const dice = [roll]; - if (roll <= rollOptions.maxCritSucc) { + if (roll <= usedOptions.maxCritSucc) { return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice); - } else if (roll >= rollOptions.minCritFail) { + } else if (roll >= usedOptions.minCritFail) { return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); } else { if (roll <= testValue) { @@ -32,9 +33,10 @@ export function rollCheckSingleDie( export function rollCheckMultipleDice( testValue: number, - rollOptions: RollOptions, + rollOptions: Partial, provider: RollProvider = new DS4RollProvider(), ): RollResult { + const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); const finalCheck = testValue % 20; const numberOfDice = Math.ceil(testValue / 20); @@ -42,12 +44,12 @@ export function rollCheckMultipleDice( const firstResult = dice[0]; - if (firstResult >= rollOptions.minCritFail) { + if (firstResult >= usedOptions.minCritFail) { return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); } const partitionCallback = (prev: [Array, Array], cur: number) => { - if (cur <= rollOptions.maxCritSucc) { + if (cur <= usedOptions.maxCritSucc) { prev[0].push(cur); } else { prev[1].push(cur); @@ -64,13 +66,16 @@ export function rollCheckMultipleDice( const evaluationResult = sortedRollResults .map((value, index) => { if (index == numberOfDice - 1) { + console.log(`Last dice: ${value}, checking against ${finalCheck}`); if (value == 1) { return finalCheck; + } else if (value <= finalCheck) { + return value; } else { - return value <= finalCheck ? value : 0; + return 0; } } else { - if (value <= rollOptions.maxCritSucc) { + if (value <= usedOptions.maxCritSucc) { return 20; } else { return value; @@ -82,11 +87,22 @@ export function rollCheckMultipleDice( return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); } -export class RollOptions { +export interface RollOptions { + maxCritSucc: number; + minCritFail: number; + useSlayingDice: boolean; + slayingDiceRepetition: boolean; +} + +class DefaultRollOptions implements RollOptions { public maxCritSucc = 1; public minCritFail = 20; public useSlayingDice = false; public slayingDiceRepetition = false; + + mergeWith(other: Partial): RollOptions { + return { ...this, ...other } as RollOptions; + } } export class RollResult {