From 9a991472cf2c3e89db9460f2f1fc101985562c6a Mon Sep 17 00:00:00 2001 From: frankieali Date: Tue, 3 Aug 2021 23:34:32 -0400 Subject: [PATCH] fix for compounded rolls --- src/diceRoller.ts | 6 +++--- test/rollerTest.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/diceRoller.ts b/src/diceRoller.ts index 856e387..f6703c6 100644 --- a/src/diceRoller.ts +++ b/src/diceRoller.ts @@ -607,13 +607,13 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { - const newRoll = this.reRoll(rolls[i], ++i); + const newRoll = this.reRoll(roll,i+1); rollValue += newRoll.roll; roll = newRoll; } - roll.value = rollValue; - roll.roll = rollValue; + rolls[i].value = rollValue; + rolls[i].roll = rollValue; } return rolls; diff --git a/test/rollerTest.test.ts b/test/rollerTest.test.ts index 4d3f778..5ef0275 100644 --- a/test/rollerTest.test.ts +++ b/test/rollerTest.test.ts @@ -62,4 +62,28 @@ testRolls.forEach(([roll, expectedValue]) => { test(roll, () => { expect(roller.rollValue(roll)).toBe(expectedValue) }); +}); + +const testFixedRolls: [string, number, number[]][] = [ + ['1d6!!', 14, [.84, .84, .17]], // value = [6,6,2] + ['4d6!!', 24, [.84, .67, .5, .17, .84, 0]] // value = [6,5,4,2,6,1] +] + +let externalCount: number = 0 +let rollsAsFloats: Array = [] +const fixedRoller = new dist.DiceRoller((rolls: Array = rollsAsFloats)=>{ + if(rolls.length > 0) { + return rolls[externalCount++] + } else { + console.warn("No results passed to the dice-roller-parser. Using fallback Math.random") + return Math.random() + } +}) + +testFixedRolls.forEach(([roll, expectedValue, values]) => { + test(roll, () => { + externalCount = 0 + rollsAsFloats = values + expect(fixedRoller.rollValue(roll)).toBe(expectedValue) + }); }); \ No newline at end of file