From 8ca93fc9c9505c7c29298526d60cdab97958138f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 13:06:32 +0100 Subject: [PATCH 01/10] Ease dice mocking. --- spec/support/ds4rolls.spec.ts | 112 +++++++++++----------------------- 1 file changed, 37 insertions(+), 75 deletions(-) diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls.spec.ts index f56fa46c..054f6818 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls.spec.ts @@ -9,11 +9,21 @@ import { RollProvider } from "../../src/module/rolls/roll-provider"; import "jasmine"; +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): 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: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4); + const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.SUCCESS, [4]), @@ -21,9 +31,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.SUCCESS, [4]), @@ -31,9 +39,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(5); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [5]), @@ -41,9 +47,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [19]), @@ -51,9 +55,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), @@ -61,9 +63,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), @@ -73,9 +73,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { 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); + const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), @@ -83,9 +81,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit success on `maxCritSucc`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(2); + const rollProvider = mockSingleThrow(2); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), @@ -93,9 +89,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(3); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(3, RollResultStatus.SUCCESS, [3]), @@ -103,9 +97,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); 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); + const rollProvider = mockSingleThrow(18); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [18]), @@ -113,9 +105,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit fail on `minCritFail`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19); + const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), @@ -123,9 +113,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit fail on `20`", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20); + const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), @@ -135,9 +123,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { 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]); + const rollProvider = mockMultipleThrows([20, 15, 6]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]), @@ -145,9 +131,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([1, 1, 1]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]), @@ -155,9 +139,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), @@ -165,9 +147,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 15, 1]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]), @@ -175,9 +155,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: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 1, 20]); + const rollProvider = mockMultipleThrows([15, 1, 20]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), @@ -187,9 +165,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { 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]); + const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), @@ -197,9 +173,7 @@ describe("DS4 Rools with multiple dice and min/max 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]); + const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), @@ -207,9 +181,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), @@ -217,9 +189,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 15, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]), @@ -227,9 +197,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 2, 20]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]), @@ -237,9 +205,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); 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]); + const rollProvider = mockMultipleThrows([15, 2, 19]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]), @@ -249,9 +215,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { 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]); + const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), @@ -261,9 +225,7 @@ describe("DS4 Rools with multiple dice and fail modifiers.", () => { 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]); + const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), From 75301b2c568457e127058297521c0b8887f559d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 13:16:08 +0100 Subject: [PATCH 02/10] Bound to fail: Add slaying dice options and tests, no impl. --- spec/support/ds4rolls.spec.ts | 56 +++++++++++++++++++++++++++---- src/module/rolls/roll-executor.ts | 2 ++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls.spec.ts index 054f6818..ec899376 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls.spec.ts @@ -71,11 +71,55 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); }); +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 } as RollOptions, 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 } as RollOptions, 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 } as RollOptions, 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 } as RollOptions, rollProvider), + ).toEqual(new RollResult(0, RollResultStatus.FAILURE, [20])); + }); + + it("Should do a regular success on `20` with a test value of 20", () => { + const rollProvider = mockSingleThrow(20); + + expect( + rollCheckSingleDie(20, { useSlayingDice: true, slayingDiceRepetition: true } as RollOptions, 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( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), ); }); @@ -83,7 +127,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit success on `maxCritSucc`.", () => { const rollProvider = mockSingleThrow(2); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), ); }); @@ -91,7 +135,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a success on lower edge case `3`.", () => { const rollProvider = mockSingleThrow(3); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(3, RollResultStatus.SUCCESS, [3]), ); }); @@ -99,7 +143,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a success on upper edge case `18`.", () => { const rollProvider = mockSingleThrow(18); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [18]), ); }); @@ -107,7 +151,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit fail on `minCritFail`.", () => { const rollProvider = mockSingleThrow(19); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), ); }); @@ -115,7 +159,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit fail on `20`", () => { const rollProvider = mockSingleThrow(20); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), ); }); diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index 204f5edc..102be1db 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -85,6 +85,8 @@ export function rollCheckMultipleDice( export class RollOptions { public maxCritSucc = 1; public minCritFail = 20; + public useSlayingDice = false; + public slayingDiceRepetition = false; } export class RollResult { From 00f7e30b4115448e028cfcc941ac96bacc94ce73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 13:54:59 +0100 Subject: [PATCH 03/10] Fix option parsing for rolls. --- spec/support/ds4rolls.spec.ts | 32 +++++++++++---------------- src/module/rolls/roll-executor.ts | 36 ++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 30 deletions(-) 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 { From 3f6f9f795f680b850da206fdae085b656f818102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 13:55:18 +0100 Subject: [PATCH 04/10] Additional tests for dice selection. --- spec/support/ds4rolls.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls.spec.ts index 282855a8..b8768e80 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls.spec.ts @@ -197,6 +197,22 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), ); }); + + 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]), + ); + }); }); describe("DS4 Rools with multiple dice and min/max modifiers.", () => { From 55beeb92c994ff2ee5a26a42710bc236cca60732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 16:12:16 +0100 Subject: [PATCH 05/10] Implement swapping edge case, restructure sources. --- .../executor.spec.ts} | 124 ++++++++++++------ spec/support/ds4rolls/utils.spec.ts | 24 ++++ src/module/rolls/roll-data.ts | 28 ++++ src/module/rolls/roll-executor.ts | 51 +++---- src/module/rolls/roll-utils.ts | 22 ++++ 5 files changed, 173 insertions(+), 76 deletions(-) rename spec/support/{ds4rolls.spec.ts => ds4rolls/executor.spec.ts} (68%) create mode 100644 spec/support/ds4rolls/utils.spec.ts create mode 100644 src/module/rolls/roll-data.ts create mode 100644 src/module/rolls/roll-utils.ts diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls/executor.spec.ts similarity index 68% rename from spec/support/ds4rolls.spec.ts rename to spec/support/ds4rolls/executor.spec.ts index b8768e80..e28447c9 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls/executor.spec.ts @@ -1,13 +1,8 @@ -import { - rollCheckMultipleDice, - rollCheckSingleDie, - RollOptions, - RollResult, - RollResultStatus, -} from "../../src/module/rolls/roll-executor"; -import { RollProvider } from "../../src/module/rolls/roll-provider"; +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"]); @@ -67,7 +62,7 @@ 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 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), ); }); @@ -75,7 +70,7 @@ describe("DS4 Rolls with one die and slaying dice, first throw.", () => { it("Should do a crit fail on `20`", () => { const rollProvider = mockSingleThrow(20); - expect(rollCheckSingleDie(4, { useSlayingDice: true } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { useSlayingDice: true }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), ); }); @@ -85,25 +80,25 @@ 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 } as RollOptions, rollProvider), - ).toEqual(new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [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 } as RollOptions, rollProvider), - ).toEqual(new RollResult(0, RollResultStatus.FAILURE, [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 test value of 20", () => { const rollProvider = mockSingleThrow(20); - expect( - rollCheckSingleDie(20, { useSlayingDice: true, slayingDiceRepetition: true } as RollOptions, rollProvider), - ).toEqual(new RollResult(20, RollResultStatus.SUCCESS, [20])); + expect(rollCheckSingleDie(20, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual( + new RollResult(20, RollResultStatus.SUCCESS, [20]), + ); }); }); @@ -111,7 +106,7 @@ 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 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), ); }); @@ -119,7 +114,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit success on `maxCritSucc`.", () => { const rollProvider = mockSingleThrow(2); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), ); }); @@ -127,7 +122,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a success on lower edge case `3`.", () => { const rollProvider = mockSingleThrow(3); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(3, RollResultStatus.SUCCESS, [3]), ); }); @@ -135,7 +130,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a success on upper edge case `18`.", () => { const rollProvider = mockSingleThrow(18); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [18]), ); }); @@ -143,7 +138,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit fail on `minCritFail`.", () => { const rollProvider = mockSingleThrow(19); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), ); }); @@ -151,13 +146,13 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit fail on `20`", () => { const rollProvider = mockSingleThrow(20); - expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + 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.", () => { +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]); @@ -186,7 +181,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { const rollProvider = mockMultipleThrows([15, 15, 1]); expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( - new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]), + new RollResult(38, RollResultStatus.SUCCESS, [15, 15, 1]), ); }); @@ -194,7 +189,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { const rollProvider = mockMultipleThrows([15, 1, 20]); expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( - new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), + new RollResult(43, RollResultStatus.SUCCESS, [20, 15, 1]), ); }); @@ -213,13 +208,37 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { new RollResult(35, RollResultStatus.SUCCESS, [20, 15, 8]), ); }); + + it("Should maximize on 'lowest dice higher than last test 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 test 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 test 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]), + ); + }); }); -describe("DS4 Rools 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.", () => { const rollProvider = mockMultipleThrows([19, 15, 6]); - expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), ); }); @@ -227,7 +246,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => { const rollProvider = mockMultipleThrows([2, 1, 2]); - expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), ); }); @@ -235,7 +254,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { it("Should succeed with the last roll not being suficient.", () => { const rollProvider = mockMultipleThrows([15, 15, 15]); - expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), ); }); @@ -243,44 +262,65 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { it("Should succeed with the last roll a crit success `2`.", () => { const rollProvider = mockMultipleThrows([15, 15, 2]); - expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( - new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]), + 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 } as RollOptions, rollProvider)).toEqual( - new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]), + 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 } as RollOptions, rollProvider)).toEqual( - new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]), + expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( + new RollResult(42, RollResultStatus.SUCCESS, [19, 15, 2]), ); }); }); -describe("DS4 Rools 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.", () => { const rollProvider = mockMultipleThrows([19, 15, 6]); - expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual( + expect(rollCheckMultipleDice(48, { minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), ); }); }); -describe("DS4 Rools 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).", () => { const rollProvider = mockMultipleThrows([2, 1, 2]); - expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual( + 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, {}, rollProvider)).toEqual( + new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 2, 19]), + ); + }); +}); + +// TODO: Implement & reactivate +xdescribe("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, {}, rollProvider)).toEqual( + new RollResult(40, RollResultStatus.CRITICAL_FAILURE, [20, 2, 19]), + ); + }); +}); diff --git a/spec/support/ds4rolls/utils.spec.ts b/spec/support/ds4rolls/utils.spec.ts new file mode 100644 index 00000000..f556af2b --- /dev/null +++ b/spec/support/ds4rolls/utils.spec.ts @@ -0,0 +1,24 @@ +import "jasmine"; +import { isDiceSwapNecessary } from "../../../src/module/rolls/roll-utils"; + +describe("Utility function testing if dice swap is necessery", () => { + 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(); + }); +}); diff --git a/src/module/rolls/roll-data.ts b/src/module/rolls/roll-data.ts new file mode 100644 index 00000000..e36fd6cd --- /dev/null +++ b/src/module/rolls/roll-data.ts @@ -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 { + return { ...this, ...other } as RollOptions; + } +} + +export class RollResult { + constructor(public value: number, public status: RollResultStatus, public dice: Array) {} +} + +export enum RollResultStatus { + FAILURE, + SUCCESS, + CRITICAL_FAILURE, + CRITICAL_SUCCESS, +} diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index 3cea5206..e9df9a9b 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -1,4 +1,6 @@ +import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data"; import { DS4RollProvider, RollProvider } from "./roll-provider"; +import { isDiceSwapNecessary, isSlayingDiceRepetition } from "./roll-utils"; export function ds4test(testValue: number, rollOptions: Partial = {}): RollResult { const finalRollValue = testValue; @@ -20,7 +22,7 @@ export function rollCheckSingleDie( if (roll <= usedOptions.maxCritSucc) { return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice); - } else if (roll >= usedOptions.minCritFail) { + } else if (roll >= usedOptions.minCritFail && !isSlayingDiceRepetition(usedOptions)) { return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); } else { if (roll <= testValue) { @@ -44,7 +46,8 @@ export function rollCheckMultipleDice( const firstResult = dice[0]; - if (firstResult >= usedOptions.minCritFail) { + // Slaying Dice require a different handling. + if (firstResult >= usedOptions.minCritFail && !isSlayingDiceRepetition(usedOptions)) { return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); } @@ -61,13 +64,22 @@ export function rollCheckMultipleDice( .reduce(partitionCallback, [[], []]) .map((a) => a.sort((r1, r2) => r2 - r1)); - const sortedRollResults: Array = critSuccesses.concat(otherRolls); + const swapLastWithCrit: boolean = isDiceSwapNecessary(critSuccesses, otherRolls, finalCheck); + + let sortedRollResults: Array; + + if (swapLastWithCrit) { + const diceToMove = critSuccesses[0]; + const remainingSuccesses = critSuccesses.slice(1); + sortedRollResults = remainingSuccesses.concat(otherRolls).concat([diceToMove]); + } else { + sortedRollResults = critSuccesses.concat(otherRolls); + } const evaluationResult = sortedRollResults .map((value, index) => { if (index == numberOfDice - 1) { - console.log(`Last dice: ${value}, checking against ${finalCheck}`); - if (value == 1) { + if (value <= usedOptions.maxCritSucc) { return finalCheck; } else if (value <= finalCheck) { return value; @@ -86,32 +98,3 @@ export function rollCheckMultipleDice( return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); } - -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 { - constructor(public value: number, public status: RollResultStatus, public dice: Array) {} -} - -export enum RollResultStatus { - FAILURE, - SUCCESS, - CRITICAL_FAILURE, - CRITICAL_SUCCESS, -} diff --git a/src/module/rolls/roll-utils.ts b/src/module/rolls/roll-utils.ts new file mode 100644 index 00000000..7db8da27 --- /dev/null +++ b/src/module/rolls/roll-utils.ts @@ -0,0 +1,22 @@ +import { RollOptions } from "./roll-data"; + +export function isDiceSwapNecessary( + critSuccesses: Array, + otherRolls: Array, + finalRollValue: number, +): boolean { + if (critSuccesses.length == 0 || otherRolls.length == 0) { + return false; + } + const amountOfOtherRolls = otherRolls.length; + const lastDice = otherRolls[amountOfOtherRolls - 1]; + if (lastDice <= finalRollValue) { + return false; + } + + return lastDice + finalRollValue > 20; +} + +export function isSlayingDiceRepetition(opts: RollOptions): boolean { + return opts.useSlayingDice && opts.slayingDiceRepetition; +} From aaa10d7469c0cb03ea85619292455f1420fd311c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 16:40:30 +0100 Subject: [PATCH 06/10] Add slaying dice behaviour. --- spec/support/ds4rolls/executor.spec.ts | 7 ++-- src/module/rolls/roll-executor.ts | 49 ++++++++++++++++---------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/spec/support/ds4rolls/executor.spec.ts b/spec/support/ds4rolls/executor.spec.ts index e28447c9..1ab1bda4 100644 --- a/spec/support/ds4rolls/executor.spec.ts +++ b/spec/support/ds4rolls/executor.spec.ts @@ -314,13 +314,12 @@ describe("DS4 Rolls with multiple and slaying dice, first throw", () => { }); }); -// TODO: Implement & reactivate -xdescribe("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`", () => { const rollProvider = mockMultipleThrows([20, 2, 19]); - expect(rollCheckMultipleDice(48, {}, rollProvider)).toEqual( - new RollResult(40, RollResultStatus.CRITICAL_FAILURE, [20, 2, 19]), + expect(rollCheckMultipleDice(48, { useSlayingDice: true, slayingDiceRepetition: true }, rollProvider)).toEqual( + new RollResult(41, RollResultStatus.SUCCESS, [20, 19, 2]), ); }); }); diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index e9df9a9b..233950ea 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -33,24 +33,7 @@ export function rollCheckSingleDie( } } -export function rollCheckMultipleDice( - testValue: number, - rollOptions: Partial, - provider: RollProvider = new DS4RollProvider(), -): RollResult { - const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); - const finalCheck = testValue % 20; - const numberOfDice = Math.ceil(testValue / 20); - - const dice = provider.getNextRolls(numberOfDice); - - const firstResult = dice[0]; - - // Slaying Dice require a different handling. - if (firstResult >= usedOptions.minCritFail && !isSlayingDiceRepetition(usedOptions)) { - return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); - } - +function separateCriticalHits(dice: Array, usedOptions: RollOptions): [Array, Array] { const partitionCallback = (prev: [Array, Array], cur: number) => { if (cur <= usedOptions.maxCritSucc) { prev[0].push(cur); @@ -64,6 +47,30 @@ export function rollCheckMultipleDice( .reduce(partitionCallback, [[], []]) .map((a) => a.sort((r1, r2) => r2 - r1)); + return [critSuccesses, otherRolls]; +} + +export function rollCheckMultipleDice( + testValue: number, + rollOptions: Partial, + provider: RollProvider = new DS4RollProvider(), +): RollResult { + const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); + const finalCheck = testValue % 20; + const numberOfDice = Math.ceil(testValue / 20); + + const dice = provider.getNextRolls(numberOfDice); + + const firstResult = dice[0]; + const slayingDiceRepetition = isSlayingDiceRepetition(usedOptions); + + // Slaying Dice require a different handling. + if (firstResult >= usedOptions.minCritFail && !slayingDiceRepetition) { + return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); + } + + const [critSuccesses, otherRolls] = separateCriticalHits(dice, usedOptions); + const swapLastWithCrit: boolean = isDiceSwapNecessary(critSuccesses, otherRolls, finalCheck); let sortedRollResults: Array; @@ -96,5 +103,9 @@ export function rollCheckMultipleDice( }) .reduce((a, b) => a + b); - return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); + if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) { + return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, sortedRollResults); + } else { + return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); + } } From f98da0cd995f77f700cec56b8c4df9874b09ffee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sun, 3 Jan 2021 14:49:21 +0100 Subject: [PATCH 07/10] Apply 2 suggestion(s) to 2 file(s) --- spec/support/ds4rolls/executor.spec.ts | 2 +- spec/support/ds4rolls/utils.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/ds4rolls/executor.spec.ts b/spec/support/ds4rolls/executor.spec.ts index 1ab1bda4..f70d874a 100644 --- a/spec/support/ds4rolls/executor.spec.ts +++ b/spec/support/ds4rolls/executor.spec.ts @@ -251,7 +251,7 @@ describe("DS4 Rolls with multiple dice and min/max modifiers.", () => { ); }); - it("Should succeed with the last roll not being suficient.", () => { + 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( diff --git a/spec/support/ds4rolls/utils.spec.ts b/spec/support/ds4rolls/utils.spec.ts index f556af2b..29d73b88 100644 --- a/spec/support/ds4rolls/utils.spec.ts +++ b/spec/support/ds4rolls/utils.spec.ts @@ -1,7 +1,7 @@ import "jasmine"; import { isDiceSwapNecessary } from "../../../src/module/rolls/roll-utils"; -describe("Utility function testing if dice swap is necessery", () => { +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(); }); From c26e4bab9f71a01e2f1544f05d64479b30e264af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sun, 3 Jan 2021 15:08:38 +0100 Subject: [PATCH 08/10] Changes as per review. Missing: - Open discussion on "test value" renames - docs - open discussion on last-die-handling --- spec/support/ds4rolls/executor.spec.ts | 18 +++++++++++++++++- src/module/rolls/roll-utils.ts | 6 +++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spec/support/ds4rolls/executor.spec.ts b/spec/support/ds4rolls/executor.spec.ts index f70d874a..e141b215 100644 --- a/spec/support/ds4rolls/executor.spec.ts +++ b/spec/support/ds4rolls/executor.spec.ts @@ -232,6 +232,14 @@ describe("DS4 Rolls with multiple dice and no modifiers.", () => { 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.", () => { @@ -308,10 +316,18 @@ 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, {}, rollProvider)).toEqual( + 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", () => { diff --git a/src/module/rolls/roll-utils.ts b/src/module/rolls/roll-utils.ts index 7db8da27..14e95f01 100644 --- a/src/module/rolls/roll-utils.ts +++ b/src/module/rolls/roll-utils.ts @@ -3,18 +3,18 @@ import { RollOptions } from "./roll-data"; export function isDiceSwapNecessary( critSuccesses: Array, otherRolls: Array, - finalRollValue: number, + lastTestValue: number, ): boolean { if (critSuccesses.length == 0 || otherRolls.length == 0) { return false; } const amountOfOtherRolls = otherRolls.length; const lastDice = otherRolls[amountOfOtherRolls - 1]; - if (lastDice <= finalRollValue) { + if (lastDice <= lastTestValue) { return false; } - return lastDice + finalRollValue > 20; + return lastDice + lastTestValue > 20; } export function isSlayingDiceRepetition(opts: RollOptions): boolean { From fc88ce6c527bb2c58dc3188a07dd1dc6f3fcbd3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Mon, 4 Jan 2021 19:38:26 +0100 Subject: [PATCH 09/10] Rename `Test` to `check`, etc., restructure calculation code. --- spec/support/ds4rolls/executor.spec.ts | 8 ++--- src/module/rolls/roll-executor.ts | 47 ++++++++------------------ src/module/rolls/roll-utils.ts | 36 ++++++++++++++++++-- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/spec/support/ds4rolls/executor.spec.ts b/spec/support/ds4rolls/executor.spec.ts index e141b215..3417e6e6 100644 --- a/spec/support/ds4rolls/executor.spec.ts +++ b/spec/support/ds4rolls/executor.spec.ts @@ -93,7 +93,7 @@ describe("DS4 Rolls with one die and slaying dice, followup throw.", () => { ); }); - it("Should do a regular success on `20` with a test value 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 }, rollProvider)).toEqual( @@ -209,7 +209,7 @@ describe("DS4 Rolls with multiple dice and no modifiers.", () => { ); }); - it("Should maximize on 'lowest dice higher than last test 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, {}, rollProvider)).toEqual( @@ -217,7 +217,7 @@ describe("DS4 Rolls with multiple dice and no modifiers.", () => { ); }); - it("Should maximize on 2-dice 'lowest dice higher than last test 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, {}, rollProvider)).toEqual( @@ -225,7 +225,7 @@ describe("DS4 Rolls with multiple dice and no modifiers.", () => { ); }); - it("Should maximize on 2-dice 'lowest dice higher than last test 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, {}, rollProvider)).toEqual( diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index 233950ea..896629a2 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -1,18 +1,19 @@ import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data"; import { DS4RollProvider, RollProvider } from "./roll-provider"; -import { isDiceSwapNecessary, isSlayingDiceRepetition } from "./roll-utils"; +import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition } from "./roll-utils"; -export function ds4test(testValue: number, rollOptions: Partial = {}): RollResult { - const finalRollValue = testValue; - if (finalRollValue <= 20) { - return rollCheckSingleDie(finalRollValue, rollOptions); +export function ds4roll(checkTargetValue: number, rollOptions: Partial = {}): RollResult { + // TODO: Add CTN modifiers from options. + const finalTargetValue = checkTargetValue; + if (finalTargetValue <= 20) { + return rollCheckSingleDie(finalTargetValue, rollOptions); } else { - return rollCheckMultipleDice(finalRollValue, rollOptions); + return rollCheckMultipleDice(finalTargetValue, rollOptions); } } export function rollCheckSingleDie( - testValue: number, + checkTargetValue: number, rollOptions: Partial, provider: RollProvider = new DS4RollProvider(), ): RollResult { @@ -21,11 +22,11 @@ export function rollCheckSingleDie( const dice = [roll]; if (roll <= usedOptions.maxCritSucc) { - return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice); + 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); @@ -51,13 +52,13 @@ function separateCriticalHits(dice: Array, usedOptions: RollOptions): [A } export function rollCheckMultipleDice( - testValue: number, + targetValue: number, rollOptions: Partial, provider: RollProvider = new DS4RollProvider(), ): RollResult { const usedOptions = new DefaultRollOptions().mergeWith(rollOptions); - const finalCheck = testValue % 20; - const numberOfDice = Math.ceil(testValue / 20); + const remainderTargetValue = targetValue % 20; + const numberOfDice = Math.ceil(targetValue / 20); const dice = provider.getNextRolls(numberOfDice); @@ -71,7 +72,7 @@ export function rollCheckMultipleDice( const [critSuccesses, otherRolls] = separateCriticalHits(dice, usedOptions); - const swapLastWithCrit: boolean = isDiceSwapNecessary(critSuccesses, otherRolls, finalCheck); + const swapLastWithCrit: boolean = isDiceSwapNecessary(critSuccesses, otherRolls, remainderTargetValue); let sortedRollResults: Array; @@ -83,25 +84,7 @@ export function rollCheckMultipleDice( sortedRollResults = critSuccesses.concat(otherRolls); } - const evaluationResult = sortedRollResults - .map((value, index) => { - if (index == numberOfDice - 1) { - if (value <= usedOptions.maxCritSucc) { - return finalCheck; - } else if (value <= finalCheck) { - return value; - } else { - return 0; - } - } else { - if (value <= usedOptions.maxCritSucc) { - return 20; - } else { - return value; - } - } - }) - .reduce((a, b) => a + b); + const evaluationResult = calculateRollResult(sortedRollResults, remainderTargetValue, usedOptions); if (usedOptions.useSlayingDice && firstResult <= usedOptions.maxCritSucc) { return new RollResult(evaluationResult, RollResultStatus.CRITICAL_SUCCESS, sortedRollResults); diff --git a/src/module/rolls/roll-utils.ts b/src/module/rolls/roll-utils.ts index 14e95f01..5df493e2 100644 --- a/src/module/rolls/roll-utils.ts +++ b/src/module/rolls/roll-utils.ts @@ -3,20 +3,50 @@ import { RollOptions } from "./roll-data"; export function isDiceSwapNecessary( critSuccesses: Array, otherRolls: Array, - lastTestValue: number, + remainingTargetValue: number, ): boolean { if (critSuccesses.length == 0 || otherRolls.length == 0) { return false; } const amountOfOtherRolls = otherRolls.length; const lastDice = otherRolls[amountOfOtherRolls - 1]; - if (lastDice <= lastTestValue) { + if (lastDice <= remainingTargetValue) { return false; } - return lastDice + lastTestValue > 20; + return lastDice + remainingTargetValue > 20; } export function isSlayingDiceRepetition(opts: RollOptions): boolean { return opts.useSlayingDice && opts.slayingDiceRepetition; } + +export function calculateRollResult( + assignedRollResults: Array, + remainderTargetValue: number, + rollOptions: RollOptions, +): number { + const numberOfDice = assignedRollResults.length; + + const maxResultPerDie: Array = 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? +function zip(a1: Array, a2: Array): 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]); + } +} From e2a42aee4b6144c17f3b60aad8988b82a617b47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Mon, 4 Jan 2021 21:21:06 +0100 Subject: [PATCH 10/10] Add docs, type clarifications. --- spec/support/ds4rolls/utils.spec.ts | 10 ++-- src/module/rolls/roll-executor.ts | 66 +++++++++++++-------- src/module/rolls/roll-provider.ts | 13 ++++- src/module/rolls/roll-utils.ts | 91 ++++++++++++++++++++++++++++- 4 files changed, 147 insertions(+), 33 deletions(-) diff --git a/spec/support/ds4rolls/utils.spec.ts b/spec/support/ds4rolls/utils.spec.ts index 29d73b88..3d88e5d2 100644 --- a/spec/support/ds4rolls/utils.spec.ts +++ b/spec/support/ds4rolls/utils.spec.ts @@ -3,22 +3,22 @@ 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(); + expect(isDiceSwapNecessary([[1, 1, 1], []], 9)).toBeFalse(); }); it("Should not swap if no die is crit success.", () => { - expect(isDiceSwapNecessary([], [2, 2, 2], 9)).toBeFalse(); + 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(); + expect(isDiceSwapNecessary([[1], [9, 8]], 10)).toBeFalse(); }); it("Should not swap if result does not get any better", () => { - expect(isDiceSwapNecessary([1], [8], 4)).toBeFalse(); + expect(isDiceSwapNecessary([[1], [8]], 4)).toBeFalse(); }); it("Should swap if result does get better", () => { - expect(isDiceSwapNecessary([1], [19], 18)).toBeTrue(); + expect(isDiceSwapNecessary([[1], [19]], 18)).toBeTrue(); }); }); diff --git a/src/module/rolls/roll-executor.ts b/src/module/rolls/roll-executor.ts index 896629a2..0a7f1694 100644 --- a/src/module/rolls/roll-executor.ts +++ b/src/module/rolls/roll-executor.ts @@ -1,17 +1,36 @@ import { DefaultRollOptions, RollOptions, RollResult, RollResultStatus } from "./roll-data"; import { DS4RollProvider, RollProvider } from "./roll-provider"; -import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition } from "./roll-utils"; +import { calculateRollResult, isDiceSwapNecessary, isSlayingDiceRepetition, separateCriticalHits } from "./roll-utils"; +/** + * 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 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 = {}): RollResult { - // TODO: Add CTN modifiers from options. - const finalTargetValue = checkTargetValue; - if (finalTargetValue <= 20) { - return rollCheckSingleDie(finalTargetValue, rollOptions); + if (checkTargetValue <= 20) { + return rollCheckSingleDie(checkTargetValue, rollOptions); } else { - return rollCheckMultipleDice(finalTargetValue, 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( checkTargetValue: number, rollOptions: Partial, @@ -34,23 +53,22 @@ export function rollCheckSingleDie( } } -function separateCriticalHits(dice: Array, usedOptions: RollOptions): [Array, Array] { - const partitionCallback = (prev: [Array, Array], cur: number) => { - if (cur <= usedOptions.maxCritSucc) { - prev[0].push(cur); - } else { - prev[1].push(cur); - } - return prev; - }; - - const [critSuccesses, otherRolls] = dice - .reduce(partitionCallback, [[], []]) - .map((a) => a.sort((r1, r2) => r2 - r1)); - - return [critSuccesses, otherRolls]; -} - +/** + * 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( targetValue: number, rollOptions: Partial, @@ -72,7 +90,7 @@ export function rollCheckMultipleDice( const [critSuccesses, otherRolls] = separateCriticalHits(dice, usedOptions); - const swapLastWithCrit: boolean = isDiceSwapNecessary(critSuccesses, otherRolls, remainderTargetValue); + const swapLastWithCrit: boolean = isDiceSwapNecessary([critSuccesses, otherRolls], remainderTargetValue); let sortedRollResults: Array; diff --git a/src/module/rolls/roll-provider.ts b/src/module/rolls/roll-provider.ts index 2e4982e0..0781e5b0 100644 --- a/src/module/rolls/roll-provider.ts +++ b/src/module/rolls/roll-provider.ts @@ -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; + getNextRolls(amount: number): Array; } diff --git a/src/module/rolls/roll-utils.ts b/src/module/rolls/roll-utils.ts index 5df493e2..09d618c3 100644 --- a/src/module/rolls/roll-utils.ts +++ b/src/module/rolls/roll-utils.ts @@ -1,8 +1,67 @@ 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} dice - The dice values. + * @param {RollOptions} usedOptions - Options that affect the check's behaviour. + * @returns {[Array, Array]} 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, 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, Array]; + +/** + * Partition an array into two, following a predicate. + * @param {Array} 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(input: Array, predicate: (v: T) => boolean) { + return input.reduce( + (p: [Array, Array], 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, Array]} 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: Array, - otherRolls: Array, + [critSuccesses, otherRolls]: CritsAndNonCrits, remainingTargetValue: number, ): boolean { if (critSuccesses.length == 0 || otherRolls.length == 0) { @@ -17,10 +76,28 @@ export function isDiceSwapNecessary( 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, remainderTargetValue: number, @@ -43,6 +120,16 @@ export function calculateRollResult( } // 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} a1 First array to zip. + * @param {Array} 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(a1: Array, a2: Array): Array<[T, U]> { if (a1.length <= a2.length) { return a1.map((e1, i) => [e1, a2[i]]);