From 9a991472cf2c3e89db9460f2f1fc101985562c6a Mon Sep 17 00:00:00 2001 From: frankieali Date: Tue, 3 Aug 2021 23:34:32 -0400 Subject: [PATCH 1/3] 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 From c33d9609c3d4bb2d5593e2aef9ed9ff380b92ebd Mon Sep 17 00:00:00 2001 From: frankieali Date: Tue, 17 Aug 2021 10:24:53 -0400 Subject: [PATCH 2/3] add operator descriptor to die objects Helps to define when a die has been dropped, rerolled, or exploded --- src/diceRoller.ts | 7 +++++++ src/rollTypes.ts | 2 ++ test/rollerTest.test.ts | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/diceRoller.ts b/src/diceRoller.ts index f6703c6..d532433 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].operation = 'drop' dropped++; } @@ -539,6 +540,7 @@ export class DiceRoller { while (i < rolls.length && dropped < toDrop) { if (rolls[i].valid) { rolls[i].valid = false; + rolls[i].operation = 'drop' dropped++; } @@ -573,6 +575,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { + roll.operation = 'explode' const newRoll = this.reRoll(roll, ++i); rolls.splice(i, 0, newRoll); roll = newRoll; @@ -607,6 +610,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { + roll.operation = 'explode' const newRoll = this.reRoll(roll,i+1); rollValue += newRoll.roll; roll = newRoll; @@ -644,6 +648,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { + roll.operation = 'explode' 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].operation = 'reroll' 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].operation = 'reroll' 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..f584e03 100644 --- a/src/rollTypes.ts +++ b/src/rollTypes.ts @@ -28,6 +28,8 @@ export interface RollBase { label?: string; /** A property used to maintain ordering of dice rolls within groups */ order: number; + /** Any special operations appied to this die */ + operation?: string; } /** An intermediate interface extended for groups of dice */ diff --git a/test/rollerTest.test.ts b/test/rollerTest.test.ts index 5ef0275..065f053 100644 --- a/test/rollerTest.test.ts +++ b/test/rollerTest.test.ts @@ -66,7 +66,8 @@ testRolls.forEach(([roll, 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] + ['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 From e7c752e45b8389fd4cb31a05a40d5615621d64cc Mon Sep 17 00:00:00 2001 From: frankieali Date: Thu, 19 Aug 2021 10:25:25 -0400 Subject: [PATCH 3/3] rename RollBase props to be more specific and prevent collision --- src/diceRoller.ts | 14 +++++++------- src/rollTypes.ts | 8 ++++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/diceRoller.ts b/src/diceRoller.ts index d532433..dee2606 100644 --- a/src/diceRoller.ts +++ b/src/diceRoller.ts @@ -514,7 +514,7 @@ export class DiceRoller { while (i < rolls.length && dropped < toDrop) { if (rolls[i].valid) { rolls[i].valid = false; - rolls[i].operation = 'drop' + rolls[i].drop = true dropped++; } @@ -540,7 +540,7 @@ export class DiceRoller { while (i < rolls.length && dropped < toDrop) { if (rolls[i].valid) { rolls[i].valid = false; - rolls[i].operation = 'drop' + rolls[i].drop = true dropped++; } @@ -575,7 +575,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { - roll.operation = 'explode' + roll.explode = true const newRoll = this.reRoll(roll, ++i); rolls.splice(i, 0, newRoll); roll = newRoll; @@ -610,7 +610,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { - roll.operation = 'explode' + roll.explode = true const newRoll = this.reRoll(roll,i+1); rollValue += newRoll.roll; roll = newRoll; @@ -648,7 +648,7 @@ export class DiceRoller { let explodeCount = 0; while (targetMethod(roll) && explodeCount++ < 1000) { - roll.operation = 'explode' + roll.explode = true const newRoll = this.reRoll(roll, ++i); newRoll.value -= 1; newRoll.roll -= 1; @@ -673,7 +673,7 @@ export class DiceRoller { for (let i = 0; i < rolls.length; i++) { while (targetMethod(rolls[i].roll)) { - rolls[i].operation = 'reroll' + rolls[i].reroll = true rolls[i].valid = false; const newRoll = this.reRoll(rolls[i], i + 1); rolls.splice(++i, 0, newRoll); @@ -696,7 +696,7 @@ export class DiceRoller { for (let i = 0; i < rolls.length; i++) { if (targetMethod(rolls[i].roll)) { - rolls[i].operation = 'reroll' + 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 f584e03..6e23fd1 100644 --- a/src/rollTypes.ts +++ b/src/rollTypes.ts @@ -28,8 +28,12 @@ export interface RollBase { label?: string; /** A property used to maintain ordering of dice rolls within groups */ order: number; - /** Any special operations appied to this die */ - operation?: string; + /** 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 */