Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/dice/FudgeDice.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class FudgeDice extends StandardDice {
}
}

return new RollResult(total);
const rollResult = new RollResult(total);
rollResult.dice = this;
return rollResult;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/dice/StandardDice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/results/RollResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 13 additions & 2 deletions tests/dice/FudgeDice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
14 changes: 11 additions & 3 deletions tests/dice/PercentileDice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down