Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multiply modifier #204

Merged
merged 1 commit into from
Jan 31, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/modifiers/CriticalFailureModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CriticalFailureModifier extends ComparisonModifier {
super(comparePoint);

// set the modifier's sort order
this.order = 9;
this.order = 10;
}

/* eslint-disable class-methods-use-this */
Expand Down
2 changes: 1 addition & 1 deletion src/modifiers/CriticalSuccessModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CriticalSuccessModifier extends ComparisonModifier {
super(comparePoint);

// set the modifier's sort order
this.order = 8;
this.order = 9;
}

/* eslint-disable class-methods-use-this */
Expand Down
106 changes: 106 additions & 0 deletions src/modifiers/MultiplyModifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { isNumeric } from '../utilities/math.js';
import ComparisonModifier from './ComparisonModifier.js';

const factorSymbol = Symbol('factor');

class MultiplyModifier extends ComparisonModifier {
constructor(factor, comparePoint = null) {
super(comparePoint);

this.factor = factor;

// set the modifier's sort order
this.order = 8;
}

/**
* The multiplication factor.
*
* @return {Number}
*/
get factor() {
return this[factorSymbol];
}

/**
* Set the multiplication factor.
*
* @param {number} value
*/
set factor(value) {
if (!isNumeric(value)) {
throw new TypeError('factor must be a positive, non-zero number');
}
this[factorSymbol] = value;
}

/* eslint-disable class-methods-use-this */
/**
* The name of the modifier.
*
* @returns {string} 'max'
*/
get name() {
return 'multiply';
}
/* eslint-enable class-methods-use-this */

/**
* The modifier's notation.
*
* @returns {string}
*/
get notation() {
return `mul${this.factor}${super.notation}`;
}

/**
* Run the modifier on the results.
*
* @param {RollResults} results The results to run the modifier against
* @param {StandardDice|RollGroup} _context The object that the modifier is attached to
*
* @returns {RollResults} The modified results
*/
run(results, _context) {
const { rolls } = results;

/* eslint-disable no-param-reassign */
rolls.forEach((roll) => {
// if there's no compare point, or the compare point matches then multiply the value
if (!this.comparePoint || this.isComparePoint(roll.value)) {
roll.modifiers.add('multiply');
roll.calculationValue *= this.factor;
}
});
/* eslint-enable */

return results;
}

/**
* Return an object for JSON serialising.
*
* This is called automatically when JSON encoding the object.
*
* @return {{
* notation: string,
* name: string,
* type: string,
* comparePoint: (ComparePoint|undefined),
* factor: Number
* }}
*/
toJSON() {
const { factor } = this;

return Object.assign(
super.toJSON(),
{
factor,
},
);
}
}

export default MultiplyModifier;
2 changes: 1 addition & 1 deletion src/modifiers/SortingModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SortingModifier extends Modifier {
this.direction = direction;

// set the modifier's sort order
this.order = 10;
this.order = 11;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/modifiers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KeepModifier from './KeepModifier.js';
import MaxModifier from './MaxModifier.js';
import MinModifier from './MinModifier.js';
import Modifier from './Modifier.js';
import MultiplyModifier from './MultiplyModifier.js';
import ReRollModifier from './ReRollModifier.js';
import SortingModifier from './SortingModifier.js';
import TargetModifier from './TargetModifier.js';
Expand All @@ -21,6 +22,7 @@ export {
MaxModifier,
MinModifier,
Modifier,
MultiplyModifier,
ReRollModifier,
SortingModifier,
TargetModifier,
Expand Down
5 changes: 3 additions & 2 deletions src/modifiers/modifier-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const flags = {
drop: 'd',
max: 'v',
min: '^',
multiply: '*',
penetrate: 'p',
're-roll': 'r',
're-roll-once': 'ro',
'target-failure': '_',
'target-success': '*',
'target-failure': '-',
'target-success': '+',
};

/**
Expand Down
7 changes: 7 additions & 0 deletions src/parser/grammars/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Modifier
/ SortingModifier
/ MaxModifier
/ MinModifier
/ MultiplyModifier

// Explode, Penetrate, Compound modifier
ExplodeModifier
Expand Down Expand Up @@ -96,6 +97,12 @@ MinModifier
return new Modifiers.MinModifier(min);
}

// Multiply roll values
MultiplyModifier
= "mul" factor:FloatNumber comparePoint:ComparePoint? {
return new Modifiers.MultiplyModifier(factor, comparePoint);
}

// Re-rolling Dice (Including Re-roll Once)
ReRollModifier
= "r" once:"o"? comparePoint:ComparePoint? {
Expand Down
4 changes: 2 additions & 2 deletions tests/DiceRoll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ describe('DiceRoll', () => {
const notation = '4d6>5f<=2';
const diceRoller = new DiceRoll(notation);

expect(diceRoller.output).toEqual(`${notation}: [6*, 2_, 5, 8*] = 1`);
expect(diceRoller.output).toEqual(`${notation}: [6+, 2-, 5, 8+] = 1`);

jest.restoreAllMocks();
});
Expand All @@ -639,7 +639,7 @@ describe('DiceRoll', () => {
const notation = '4d6>5f<=2+2d6';
const diceRoller = new DiceRoll(notation);

expect(diceRoller.output).toEqual(`${notation}: [6*, 2_, 5, 8*]+[3, 5] = 9`);
expect(diceRoller.output).toEqual(`${notation}: [6+, 2-, 5, 8+]+[3, 5] = 9`);

jest.restoreAllMocks();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/modifiers/MaxModifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('MaxModifier', () => {
}));
});

test('constructor requires max', () => {
test('constructor requires `max`', () => {
expect(() => {
new MaxModifier();
}).toThrow(TypeError);
Expand Down
Loading