Allow adding a value to dice rolls in #roll.

This commit is contained in:
raeleus 2024-10-06 09:39:13 -07:00
parent 09b404cbdc
commit ca76856c90
4 changed files with 24 additions and 11 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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"

View file

@ -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