-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Inline rolls #206
Labels
Comments
I think you could achieve it by RegExp string replacement: const regex = /\[([^[\]]+)\]/
function parseExpression(expression, roller) {
if (regex.test(expression)) {
expression = expression.replace(regex, (_, notation) => {
const singleRoll = roller.roll(notation.trim())
return String(singleRoll.total)
})
return parseExpression(expression, roller)
} else {
return expression
}
}
const diceRoller = new DiceRoller()
const expression = /* your expression */
const finalExpression = parseExpression(expression, diceRoller)
console.log('firstly:', diceRoller.output)
console.log('finally:', new DiceRoll(finalExpression).output) examples:
You can also introduce variables to use the results of previous rolls, or any other datasource. function parseExpression(expression, roller) {
if (regex.test(expression)) {
expression = expression.replace(regex, (_, notation) => {
notation = notation.replace(/\$(\d+)/, (_, variable) => {
return String(roller.log[Number(variable) - 1].total) // $1 will refer to roller.log[0]
})
const singleRoll = roller.roll(notation.trim())
return String(singleRoll.total)
})
return parseExpression(expression, roller)
} else {
return expression
}
} and then:
A complex example: how to roll run: if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature request
Provide the ability to treat roll results as dice count or number of dice sides.
A similar feature is implemented in Roll20: https://wiki.roll20.net/Dice_Reference#Inline_Dice_Rolls
Current behaviour
Input
4d(1d6)
producesInvalid notation; Expected "%", "*", "**", "+", "-", "/", "^", [.], or [0-9] but "d" found.
error.Input
(1d6)d6
producesInvalid notation; Expected "%", "*", "**", "+", "-", "/", "^", or end of input but "d" found.
error.etc.
Inputs such as
1d4*((3d10)*(4d6/2))
produce desired results.Desired behaviour
I would like to treat dice roll results in a way similar to equations, so they would be computed and used in notation as literal numbers.
Examples:
(1d6)d6
would be the same as rolling a d6, then using the result as number of d6 to roll on the next step. E.g. inline roll of1d6
results in 4, then the overall result would be equal to rolling4d6
. If a 2 is rolled on1d6
, then overall result will be determined on roll of2d6
. And so on(10d6>3)d6
would be the same as rolling a 10d6, then roll d6 equal to the amount of successes (> 3) in the first roll3d(1d6)
would be the same as rolling a d6, and then roll3d1
,3d2
,3d3
,3d4
,3d5
or3d6
, depending on result of first roll((((4d3)d6>=4)d6>=3)d6<5)d6: ( ( ( ( ([1, 1, 2, 1] = 5) -> [3, 4*, 4*, 1, 6*] = 3) -> [4*, 1, 3*] = 2 ) -> [2*, 3*] = 2 ) -> [5, 6] = 11
or a multi-lined, step-by-step format:
Benefits
I play Warhammer 40,000. A common case of dice roll for Warhammer players (and probably in other wargames too) is to roll attacks in several steps ("To Hit" -> "To Wound" -> "Attempt saves against attacks" -> "Calculate damage" -> "Atttempt to ignore damage", etc.), using number of successes sum of rolled dice as dice pool for new rolls. A description of the rules can be found here.
Obviously, just saving simple dice rolls such as
24d6
does not help much, as number of dice rolled on each step is randomized. Saving the whole chain, like(((24d6>=4)d6>=3)d6<3
, or at least typing it in a single roll instead of rolling each step separately, is something I have been looking for in a dice rolling app for a long time.Concerns
The text was updated successfully, but these errors were encountered: