Skip to content

Commit

Permalink
Add limiter to limit the number of rolls to sane values
Browse files Browse the repository at this point in the history
  • Loading branch information
edloidas committed Sep 13, 2017
1 parent c625549 commit 2fd6acb
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/limiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { Roll, WodRoll } = require('roll-parser');

const MAX_DICE = 99999;
const MAX_COUNT = 12;

const MIN_MOD = -9999;
const MAX_MOD = 9999;

const MAX_SUCCESS = 99999;
const MAX_FAIL = 99998;

function limit(roll) {
if (roll instanceof Roll) {
const dice = Math.min(roll.dice, MAX_DICE);
const count = Math.min(roll.count, MAX_COUNT);
const modifier = Math.max(Math.min(roll.modifier, MAX_MOD), MIN_MOD);
return new Roll(dice, count, modifier);
} else if (roll instanceof WodRoll) {
const dice = Math.min(roll.dice, MAX_DICE);
const count = Math.min(roll.count, MAX_COUNT);
const success = Math.min(roll.success, MAX_SUCCESS);
const fail = Math.min(roll.fail, MAX_FAIL);
return new WodRoll(dice, count, roll.again, success, fail);
}

return null;
}

module.exports = {
limit
};
5 changes: 3 additions & 2 deletions src/query/full.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { parseAndRoll } = require('roll-parser');
const { parse, roll } = require('roll-parser');
const { createFullResultMessage } = require('../text');
const { limit } = require('../limiter');

/*
Matches `full` command:
Expand All @@ -9,7 +10,7 @@ Matches `full` command:
const regexp = /^\/(full)(@rollrobot)?(\s[\s\S]*)*$/;

function reply(notation) {
const result = parseAndRoll(notation);
const result = roll(limit(parse(notation)));
return createFullResultMessage(result);
}

Expand Down
5 changes: 3 additions & 2 deletions src/query/roll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { parseAndRoll } = require('roll-parser');
const { parse, roll } = require('roll-parser');
const { createResultMessage } = require('../text');
const { limit } = require('../limiter');

/*
Matches `roll` command:
Expand All @@ -9,7 +10,7 @@ Matches `roll` command:
const regexp = /^\/(roll)(@rollrobot)?(\s[\s\S]*)*$/;

function reply(notation) {
const result = parseAndRoll(notation);
const result = roll(limit(parse(notation)));
return createResultMessage(result);
}

Expand Down
22 changes: 22 additions & 0 deletions test/query/full.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ describe('/full', () => {
await expect(server.send('/full 4d20-1')).resolves.toEqual(matching);
await expect(server.send('/full 3d10!>6f3')).resolves.toEqual(matching);
});

test('should limit roll values', async () => {
expect.assertions(3);

const classicMatching = expect.stringMatching(
/`\(12d99999(\+|-)9999\)` \*\d+\* .+/
);
const wodMatching = expect.stringMatching(
/`\(12d99999!>99999f99998\)` \*\d+\* .+/
);

let result;

result = server.send('/full 100d123456+12345');
await expect(result).resolves.toEqual(classicMatching);

result = server.send('/full 77d777777-77777');
await expect(result).resolves.toEqual(classicMatching);

result = server.send('/full 999d123456!>777777f111111');
await expect(result).resolves.toEqual(wodMatching);
});
});
22 changes: 22 additions & 0 deletions test/query/roll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,26 @@ describe('/roll', () => {
await expect(server.send('/roll 4d20-1')).resolves.toEqual(matching);
await expect(server.send('/roll 3d10!>6f3')).resolves.toEqual(matching);
});

test('should limit roll values', async () => {
expect.assertions(3);

const classicMatching = expect.stringMatching(
/`\(12d99999(\+|-)9999\)` \*\d+\*/
);
const wodMatching = expect.stringMatching(
/`\(12d99999!>99999f99998\)` \*\d+\*/
);

let result;

result = server.send('/roll 100d123456+12345');
await expect(result).resolves.toEqual(classicMatching);

result = server.send('/roll 77d777777-77777');
await expect(result).resolves.toEqual(classicMatching);

result = server.send('/roll 999d123456!>777777f111111');
await expect(result).resolves.toEqual(wodMatching);
});
});

0 comments on commit 2fd6acb

Please sign in to comment.