diff --git a/src/dice/FudgeDice.js b/src/dice/FudgeDice.js index 78924504..afef7a69 100644 --- a/src/dice/FudgeDice.js +++ b/src/dice/FudgeDice.js @@ -82,7 +82,9 @@ class FudgeDice extends StandardDice { } } - return new RollResult(total); + const rollResult = new RollResult(total); + rollResult.dice = this; + return rollResult; } } diff --git a/src/dice/StandardDice.js b/src/dice/StandardDice.js index db9d5923..ef510e99 100644 --- a/src/dice/StandardDice.js +++ b/src/dice/StandardDice.js @@ -221,7 +221,9 @@ class StandardDice extends HasDescription { * @returns {RollResult} The value rolled */ rollOnce() { - return new RollResult(generator.integer(this.min, this.max)); + const rollResult = new RollResult(generator.integer(this.min, this.max)); + rollResult.dice = this; + return rollResult; } /** diff --git a/src/results/RollResult.js b/src/results/RollResult.js index 75f5f0e0..b9fb4edc 100644 --- a/src/results/RollResult.js +++ b/src/results/RollResult.js @@ -5,6 +5,7 @@ const calculationValueSymbol = Symbol('calculation-value'); const modifiersSymbol = Symbol('modifiers'); const initialValueSymbol = Symbol('initial-value'); const useInTotalSymbol = Symbol('use-in-total'); +const diceSymbol = Symbol('dice'); const valueSymbol = Symbol('value'); /** @@ -196,6 +197,18 @@ class RollResult { this[useInTotalSymbol] = !!value; } + get dice() { + return this[diceSymbol]; + } + + set dice(value) { + if (typeof value !== 'object' || !value) { + throw new TypeError('Dice value is not of instance StandardDice'); + } + + this[diceSymbol] = value; + } + /** * Value of the roll after modifiers have been applied. * diff --git a/tests/dice/FudgeDice.test.js b/tests/dice/FudgeDice.test.js index 50dacad7..295aeff4 100644 --- a/tests/dice/FudgeDice.test.js +++ b/tests/dice/FudgeDice.test.js @@ -468,7 +468,13 @@ describe('FudgeDice', () => { describe('Rolling', () => { test('rollOnce returns a RollResult object', () => { - expect((new FudgeDice(2, 1)).rollOnce()).toBeInstanceOf(RollResult); + const dice = new FudgeDice(2, 1); + const rollResult = dice.rollOnce(); + + expect(rollResult).toBeInstanceOf(RollResult); + expect(rollResult.dice).toBeInstanceOf(FudgeDice); + expect(rollResult.dice).toBe(dice); + expect(rollResult.dice.sides).toEqual('F.2'); }); test('rollOnce rolls between min and max (Inclusive)', () => { @@ -498,7 +504,12 @@ describe('FudgeDice', () => { }); test('roll returns a RollResults object', () => { - expect((new FudgeDice(null, 1)).roll()).toBeInstanceOf(RollResults); + const dice = new FudgeDice(null, 1); + const rollResults = dice.roll(); + + expect(rollResults).toBeInstanceOf(RollResults); + expect(rollResults).toHaveLength(1); + expect(rollResults.rolls[0].dice).toBe(dice); }); test('rollOnce gets called when rolling', () => { diff --git a/tests/dice/PercentileDice.test.js b/tests/dice/PercentileDice.test.js index 9dca58e9..34deda72 100644 --- a/tests/dice/PercentileDice.test.js +++ b/tests/dice/PercentileDice.test.js @@ -452,7 +452,13 @@ describe('PercentileDice', () => { describe('Rolling', () => { test('rollOnce returns a RollResult object', () => { - expect((new PercentileDice()).rollOnce()).toBeInstanceOf(RollResult); + const dice = new PercentileDice(); + const result = dice.rollOnce(); + + expect(result).toBeInstanceOf(RollResult); + expect(result.dice).toBeInstanceOf(PercentileDice); + expect(result.dice).toBe(dice); + expect(result.dice.sides).toEqual('%'); }); test('rollOnce rolls between min and max (Inclusive)', () => { @@ -487,9 +493,11 @@ describe('PercentileDice', () => { }); test('roll returns correct number of rolls', () => { - const die = new PercentileDice(4); + const dice = new PercentileDice(4); + const rollResults = dice.roll(); - expect(die.roll()).toHaveLength(4); + expect(rollResults).toHaveLength(4); + expect(rollResults.rolls[0].dice).toBe(dice); }); });