From ca76856c904aef6a8a37d6cc94f3577ee7549137 Mon Sep 17 00:00:00 2001 From: raeleus Date: Sun, 6 Oct 2024 09:39:13 -0700 Subject: [PATCH] Allow adding a value to dice rolls in #roll. --- Input.js | 8 +++++--- Library.js | 24 +++++++++++++++++------- Output.js | 2 +- README.md | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Input.js b/Input.js index 7832e0a..480478c 100644 --- a/Input.js +++ b/Input.js @@ -591,9 +591,10 @@ function doRoll(command) { if (dice == null) dice = "d20" dice = formatRoll(dice) - var roll = calculateRoll(dice) - if (rollType == "advantage") roll = Math.max(roll, calculateRoll(dice)) - if (rollType == "disadvantage") roll = Math.min(roll, calculateRoll(dice)) + var addition = getAddition(dice) + var roll = calculateRoll(dice) - addition + if (rollType == "advantage") roll = Math.max(roll, calculateRoll(dice) - addition) + if (rollType == "disadvantage") roll = Math.min(roll, calculateRoll(dice) - addition) state.show = "none" @@ -603,6 +604,7 @@ function doRoll(command) { if (roll == 20) text += " Critical Success!" else if (roll == 1) text += " Critical Failure!" + else if (addition > 0) text += ` + ${addition} = ${roll + addition}` text += "]\n" return text diff --git a/Library.js b/Library.js index 4d5f444..1aefc19 100644 --- a/Library.js +++ b/Library.js @@ -185,7 +185,7 @@ function statsToOrPattern(stats) { function getDice(rolltext) { var matches = rolltext.match(/\d+(?=d)/) if (matches != null) { - return matches[0] + return parseInt(matches[0]) } return 1 } @@ -193,16 +193,25 @@ function getDice(rolltext) { function getSides(rolltext) { var matches = rolltext.match(/(?<=d)\d+/) if (matches != null) { - return matches[0] + return parseInt(matches[0]) } return 20 } -function formatRoll(text) { - var matches = text.match(/(?<=.*)\d*d\d+(?=.*)/) +function getAddition(rolltext) { + var matches = rolltext.match(/(?<=(\+|-)\s*)\d+/) if (matches != null) { - return matches[0] + return parseInt(matches[0]) + } + + return 0 +} + +function formatRoll(text) { + var matches = text.match(/(?<=.*)\d*d\d+(?=.*)(\s*\+\s*\d+)?/) + if (matches != null) { + return matches[0].replaceAll(/\s*\+\s*/g, "+").replaceAll(/\s*-\s*/g, "-") } matches = text.match(/\d+/) @@ -218,13 +227,14 @@ function calculateRoll(rolltext) { var dice = getDice(rolltext) var sides = getSides(rolltext) + var addition = getAddition(rolltext) - var score = 0; + var score = addition; for (i = 0; i < dice; i++) { score += getRandomInteger(1, sides) } - return score + return Math.max(0, score) } function getCharacter(characterName) { diff --git a/Output.js b/Output.js index cedeb6f..e9b621a 100644 --- a/Output.js +++ b/Output.js @@ -224,7 +224,7 @@ const modifier = (text) => { text += "--Basic Hashtags--" text += "\n#roll (advantage|disadvantage) (dice_value)" - text += "\n Rolls a die/dice and shows the result. dice_value can be in the following formats 5d20 or d20 or 20. The parameters can be listed in any order." + text += "\n Rolls a die/dice and shows the result. dice_value can be in the following formats 5d20+6 or 5d20 or d20 or 20. The parameters can be listed in any order." text += "\n#generatename (male|female) (fantasy|modern|scifi|nordic)" text += "\n Retrieves a random name from a list of names in the specified gender and genre. The parameters can be listed in any order." text += "\n#shownotes" diff --git a/README.md b/README.md index 15da336..99e2dfc 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ v. 0.2.0 * The scenario keeps track of how many days have passed since the adventure has started. See `#rest`, `#showday`, `#setday`. * Added automatic difficulty which is DC 0 * Added `#version` +* Allow adding a plus or minus number to `#roll`. ie. `#roll 5d20+6` * Minor bug fixes and improvements v. 0.1.0