Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const clamp = (value, max, min = 0) =>
- Math.min(max, Math.max(min, value))
- const sizes = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20']
- const rollDie = async (size) => {
- let total = 0
- let rolls = []
- let maxDie = size
- // do the roll
- const roll = await new Roll(size).roll()
- const result = parseInt(roll.total)
- total += result
- const max = roll.dice[0].faces
- const explodes = result === max
- rolls.push({roll, explodes})
- // post the result
- const content = explodes ? 'EXPLODES' : '--'
- // roll the next die, if explode
- if(explodes){
- const currentIndex = sizes.indexOf(size)
- const nextIndex = clamp(currentIndex + 1, (sizes.length-1))
- const explosion = await rollDie(sizes[nextIndex])
- total += explosion.total
- maxDie = explosion.maxDie
- rolls = [...rolls, ...explosion.rolls]
- // TODO: Update actor skill??
- }
- return { total, maxDie, rolls }
- }
- const {total, maxDie, rolls} = await rollDie('d4')
- let enrichedRolls = ''
- for(const roll of rolls) {
- enrichedRolls += await roll.roll.render()
- if(roll.explodes) enrichedRolls += '<h3>EXPLODES</h3>'
- }
- ChatMessage.create({
- user: game.user._id,
- speaker: ChatMessage.getSpeaker(),
- content: `<h1>RESULT: ${total}</h1><p>New die size: ${maxDie}</p>${enrichedRolls}`,
- sound: CONFIG.sounds.dice,
- rolls: rolls.map((r) => r.roll),
- type: CONST.CHAT_MESSAGE_TYPES.ROLL,
- })
- /* TODO:
- Add a listener to the chat message to spend TT; this should open a dialog and then trigger another explosion (or, rollDie call, with the next die size) on submit if the total for the rolls.
- */
Advertisement
Add Comment
Please, Sign In to add comment