-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add limiter to limit the number of rolls to sane values
- Loading branch information
Showing
5 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters