diff --git a/src/diceRoller.ts b/src/diceRoller.ts index 856e387..dee2606 100644 --- a/src/diceRoller.ts +++ b/src/diceRoller.ts @@ -514,6 +514,7 @@ export class DiceRoller { while (i < rolls.length && dropped < toDrop) { if (rolls[i].valid) { rolls[i].valid = false; + rolls[i].drop = true dropped++; } @@ -539,6 +540,7 @@ export class DiceRoller { while (i < rolls.length && dropped < toDrop) { if (rolls[i].valid) { rolls[i].valid = false; + rolls[i].drop = true dropped++; } @@ -573,6 +575,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { + roll.explode = true const newRoll = this.reRoll(roll, ++i); rolls.splice(i, 0, newRoll); roll = newRoll; @@ -607,13 +610,14 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { - const newRoll = this.reRoll(rolls[i], ++i); + roll.explode = true + 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; @@ -644,6 +648,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { + roll.explode = true const newRoll = this.reRoll(roll, ++i); newRoll.value -= 1; newRoll.roll -= 1; @@ -668,6 +673,7 @@ export class DiceRoller { for (let i = 0; i < rolls.length; i++) { while (targetMethod(rolls[i].roll)) { + rolls[i].reroll = true rolls[i].valid = false; const newRoll = this.reRoll(rolls[i], i + 1); rolls.splice(++i, 0, newRoll); @@ -690,6 +696,7 @@ export class DiceRoller { for (let i = 0; i < rolls.length; i++) { if (targetMethod(rolls[i].roll)) { + rolls[i].reroll = true rolls[i].valid = false; const newRoll = this.reRoll(rolls[i], i + 1); rolls.splice(++i, 0, newRoll); diff --git a/src/rollTypes.ts b/src/rollTypes.ts index 3c7d3b4..6e23fd1 100644 --- a/src/rollTypes.ts +++ b/src/rollTypes.ts @@ -28,6 +28,12 @@ export interface RollBase { label?: string; /** A property used to maintain ordering of dice rolls within groups */ order: number; + /** Has this die been dropped */ + drop?: boolean; + /** Has this die exploded */ + explode?: boolean; + /** Has this die been rerolled */ + reroll?: boolean; } /** An intermediate interface extended for groups of dice */ diff --git a/test/rollerTest.test.ts b/test/rollerTest.test.ts index 4d3f778..065f053 100644 --- a/test/rollerTest.test.ts +++ b/test/rollerTest.test.ts @@ -62,4 +62,29 @@ 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] + ['4d6dl1', 15, [.84, .67, .5, .17]] // value = [6,5,4,2] +] + +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