Fix option parsing for rolls.

This commit is contained in:
Oliver Rümpelein 2021-01-02 13:54:59 +01:00
parent 75301b2c56
commit 00f7e30b41
2 changed files with 38 additions and 30 deletions

View file

@ -25,39 +25,31 @@ describe("DS4 Rolls with one die and no modifications.", () => {
it("Should do a regular success roll.", () => { it("Should do a regular success roll.", () => {
const rollProvider = mockSingleThrow(4); const rollProvider = mockSingleThrow(4);
expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(12, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
}); });
it("Should do a single success roll on success upper edge case.", () => { it("Should do a single success roll on success upper edge case.", () => {
const rollProvider = mockSingleThrow(4); const rollProvider = mockSingleThrow(4);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(4, RollResultStatus.SUCCESS, [4]));
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
}); });
it("Should do a single failure roll on lower edge case.", () => { it("Should do a single failure roll on lower edge case.", () => {
const rollProvider = mockSingleThrow(5); const rollProvider = mockSingleThrow(5);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [5]));
new RollResult(0, RollResultStatus.FAILURE, [5]),
);
}); });
it("Should do a single failure roll on upper edge case '19'.", () => { it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider = mockSingleThrow(19); const rollProvider = mockSingleThrow(19);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(new RollResult(0, RollResultStatus.FAILURE, [19]));
new RollResult(0, RollResultStatus.FAILURE, [19]),
);
}); });
it("Should do a single crit success roll on '1'.", () => { it("Should do a single crit success roll on '1'.", () => {
const rollProvider = mockSingleThrow(1); const rollProvider = mockSingleThrow(1);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), 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'.", () => { it("Should do a single crit failure roll on '20'.", () => {
const rollProvider = mockSingleThrow(20); const rollProvider = mockSingleThrow(20);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, {}, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), 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.", () => { it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider = mockMultipleThrows([20, 15, 6]); 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]), 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]); 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]), 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.", () => { it("Should succeed with the last roll not being suficient.", () => {
const rollProvider = mockMultipleThrows([15, 15, 15]); 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]), 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.", () => { it("Should succeed with the last roll a crit success.", () => {
const rollProvider = mockMultipleThrows([15, 15, 1]); 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]), 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.", () => { it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider = mockMultipleThrows([15, 1, 20]); 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]), new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]),
); );
}); });

View file

@ -1,6 +1,6 @@
import { DS4RollProvider, RollProvider } from "./roll-provider"; import { DS4RollProvider, RollProvider } from "./roll-provider";
export function ds4test(testValue: number, rollOptions: RollOptions = new RollOptions()): RollResult { export function ds4test(testValue: number, rollOptions: Partial<RollOptions> = {}): RollResult {
const finalRollValue = testValue; const finalRollValue = testValue;
if (finalRollValue <= 20) { if (finalRollValue <= 20) {
return rollCheckSingleDie(finalRollValue, rollOptions); return rollCheckSingleDie(finalRollValue, rollOptions);
@ -11,15 +11,16 @@ export function ds4test(testValue: number, rollOptions: RollOptions = new RollOp
export function rollCheckSingleDie( export function rollCheckSingleDie(
testValue: number, testValue: number,
rollOptions: RollOptions, rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(), provider: RollProvider = new DS4RollProvider(),
): RollResult { ): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const roll = provider.getNextRoll(); const roll = provider.getNextRoll();
const dice = [roll]; const dice = [roll];
if (roll <= rollOptions.maxCritSucc) { if (roll <= usedOptions.maxCritSucc) {
return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice); 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); return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
} else { } else {
if (roll <= testValue) { if (roll <= testValue) {
@ -32,9 +33,10 @@ export function rollCheckSingleDie(
export function rollCheckMultipleDice( export function rollCheckMultipleDice(
testValue: number, testValue: number,
rollOptions: RollOptions, rollOptions: Partial<RollOptions>,
provider: RollProvider = new DS4RollProvider(), provider: RollProvider = new DS4RollProvider(),
): RollResult { ): RollResult {
const usedOptions = new DefaultRollOptions().mergeWith(rollOptions);
const finalCheck = testValue % 20; const finalCheck = testValue % 20;
const numberOfDice = Math.ceil(testValue / 20); const numberOfDice = Math.ceil(testValue / 20);
@ -42,12 +44,12 @@ export function rollCheckMultipleDice(
const firstResult = dice[0]; const firstResult = dice[0];
if (firstResult >= rollOptions.minCritFail) { if (firstResult >= usedOptions.minCritFail) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice); return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
} }
const partitionCallback = (prev: [Array<number>, Array<number>], cur: number) => { const partitionCallback = (prev: [Array<number>, Array<number>], cur: number) => {
if (cur <= rollOptions.maxCritSucc) { if (cur <= usedOptions.maxCritSucc) {
prev[0].push(cur); prev[0].push(cur);
} else { } else {
prev[1].push(cur); prev[1].push(cur);
@ -64,13 +66,16 @@ export function rollCheckMultipleDice(
const evaluationResult = sortedRollResults const evaluationResult = sortedRollResults
.map((value, index) => { .map((value, index) => {
if (index == numberOfDice - 1) { if (index == numberOfDice - 1) {
console.log(`Last dice: ${value}, checking against ${finalCheck}`);
if (value == 1) { if (value == 1) {
return finalCheck; return finalCheck;
} else if (value <= finalCheck) {
return value;
} else { } else {
return value <= finalCheck ? value : 0; return 0;
} }
} else { } else {
if (value <= rollOptions.maxCritSucc) { if (value <= usedOptions.maxCritSucc) {
return 20; return 20;
} else { } else {
return value; return value;
@ -82,11 +87,22 @@ export function rollCheckMultipleDice(
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults); 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 maxCritSucc = 1;
public minCritFail = 20; public minCritFail = 20;
public useSlayingDice = false; public useSlayingDice = false;
public slayingDiceRepetition = false; public slayingDiceRepetition = false;
mergeWith(other: Partial<RollOptions>): RollOptions {
return { ...this, ...other } as RollOptions;
}
} }
export class RollResult { export class RollResult {