Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Condensing die roll templates?

Has anyone worked out a bit of Javascript to take die rolls in text form from multiple sources and "condense" them into a more readable format? An example of what I'm trying to do would look something like this: Base hand-to-hand damage is calculated from strength by looking it up in a table that determines it's 'd6+1'. The character has a blade that causes extra damage, which the player has entered as 'd6+1'. Elsewhere on the page I want a set of shortcuts for common attacks where I display the total roll, which I can get from concatenating to 'd6+1+d6+1'... but that's rather ugly to display.  I'd like to be able to turn it into a more concise "2d6+2".  Is there any existing work on this? If not, I suppose I'll try to figure something out by looking at some example dice parser code.  Is regex available inside sheet workers?
I should add that for this particular exercise it wouldn't need to support multiplication/division, nesting, etc. - just +, -, basic die rolls, and whole numbers.
1592563204
GiGs
Pro
Sheet Author
API Scripter
This would be possible. Is there a reason though you need to allow players to enter these as strings. You could instead have three inputs, asking for number dice, type of dice, and bonus. You could set it up something like <input type="number" class="dice-display" name="attr_dice_number" value="1"> <span class="dice-display">d</span> <input type="number" class="dice-display" name="attr_dice_sides" value="6"> <select class="dice-display" name="attr_dice_direction">     <option value="1" selected>+</option>     <option value="-1">-</option> </select> <input type="number" class="dice-display" name="attr_dice_modifier" value="0"> <!-- note: multiply the modifier by the dice_direction to get the true modifier. --> put it in a div, maybe, and then use css to set up so it displays properly. Then you simple collect all the properties of each dice roll and handle them easily. The advantage of this approach (and it is huge - non-programmers often underestimate just how important this is) is that users cannot mess up the input. Allowing for a simple text string like "1d6+1" is prone to error, and often needs a lot of programming to handle the script not breaking when unexpected text is entered (which will  happen - even if just form typos). 
1592563528
GiGs
Pro
Sheet Author
API Scripter
To answer your last question, yes, regex is available inside sheet workers.
1592564661
Finderski
Plus
Sheet Author
Compendium Curator
The other option would be to place comments after each portion of the formula. You could still have drop downs and things like that to avoid the typos, etc. but as you construct the die roll formula insert the comments after each section: d6+1 [Strength] + d6+1 [Weapon] That way you'd know what all went into the die roll.
For this particular application there's not a good fixed format to apply since (for instance) the damage roll format for a power can change based on the number of points put into it.  For example, 10 points in a power might do 2d6 damage, while 15 might be d6+d8, 20 might be 2d8, etc. I've scraped a few of the barnacles off my javascript skills and started on implementing something on a plain html page for starters; it doesn't look it will be too hard to do.  Just need to better understand the results I'm getting from regex exec and match functions, but I'll figure it out. GiGs said: This would be possible. Is there a reason though you need to allow players to enter these as strings. You could instead have three inputs, asking for number dice, type of dice, and bonus. You could set it up something like <input type="number" class="dice-display" name="attr_dice_number" value="1"> <span class="dice-display">d</span> <input type="number" class="dice-display" name="attr_dice_sides" value="6"> <select class="dice-display" name="attr_dice_direction">     <option value="1" selected>+</option>     <option value="-1">-</option> </select> <input type="number" class="dice-display" name="attr_dice_modifier" value="0"> <!-- note: multiply the modifier by the dice_direction to get the true modifier. --> put it in a div, maybe, and then use css to set up so it displays properly. Then you simple collect all the properties of each dice roll and handle them easily. The advantage of this approach (and it is huge - non-programmers often underestimate just how important this is) is that users cannot mess up the input. Allowing for a simple text string like "1d6+1" is prone to error, and often needs a lot of programming to handle the script not breaking when unexpected text is entered (which will  happen - even if just form typos). 
That's a possibility as well - I want to display it on the character sheet, though, and I'd have to rethink some of my layout to account for the increased screen space the comments would occupy.  Will keep it in mind if my other attempt falls through :) Finderski said: The other option would be to place comments after each portion of the formula. You could still have drop downs and things like that to avoid the typos, etc. but as you construct the die roll formula insert the comments after each section: d6+1 [Strength] + d6+1 [Weapon] That way you'd know what all went into the die roll.
1592596976
GiGs
Pro
Sheet Author
API Scripter
If there is a relationship between the power points and the damage done, you can program that into a sheet worker, and let players input the power and have the damage roll filled in automatically.
GiGs said: The advantage of this approach (and it is huge - non-programmers often underestimate just how important this is) is that users cannot mess up the input.  You underestimate my power! But this looks nice.  I have to see about adding this to my tool box
1592607309
GiGs
Pro
Sheet Author
API Scripter
Coal Powered Puppet said: GiGs said: The advantage of this approach (and it is huge - non-programmers often underestimate just how important this is) is that users cannot mess up the input.  You underestimate my power! But this looks nice.  I have to see about adding this to my tool box I'm happy you find it useful :)