demondownload

Never Stop Blowing Up

Jun 27th, 2024 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const clamp = (value, max, min = 0) =>
  2.   Math.min(max, Math.max(min, value))
  3.  
  4. const sizes = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20']
  5.  
  6. const rollDie = async (size) => {
  7.   let total = 0
  8.   let rolls = []
  9.   let maxDie = size
  10.   // do the roll
  11.   const roll = await new Roll(size).roll()
  12.   const result = parseInt(roll.total)
  13.   total += result
  14.   const max = roll.dice[0].faces
  15.   const explodes = result === max
  16.   rolls.push({roll, explodes})
  17.  
  18.   // post the result
  19.   const content = explodes ? 'EXPLODES' : '--'
  20.  
  21.   // roll the next die, if explode
  22.   if(explodes){
  23.     const currentIndex = sizes.indexOf(size)
  24.     const nextIndex = clamp(currentIndex + 1, (sizes.length-1))
  25.     const explosion = await rollDie(sizes[nextIndex])
  26.     total += explosion.total
  27.     maxDie = explosion.maxDie
  28.     rolls = [...rolls, ...explosion.rolls]
  29.     // TODO: Update actor skill??
  30.   }
  31.  
  32.   return { total, maxDie, rolls }
  33. }
  34.  
  35. const {total, maxDie, rolls} = await rollDie('d4')
  36.  
  37. let enrichedRolls = ''
  38. for(const roll of rolls) {
  39.   enrichedRolls += await roll.roll.render()
  40.   if(roll.explodes) enrichedRolls += '<h3>EXPLODES</h3>'
  41. }
  42.  
  43. ChatMessage.create({
  44.   user: game.user._id,
  45.   speaker: ChatMessage.getSpeaker(),
  46.   content: `<h1>RESULT: ${total}</h1><p>New die size: ${maxDie}</p>${enrichedRolls}`,
  47.   sound: CONFIG.sounds.dice,
  48.   rolls: rolls.map((r) => r.roll),
  49.   type: CONST.CHAT_MESSAGE_TYPES.ROLL,
  50. })
  51.  
  52. /* TODO:
  53.   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.
  54. */
Advertisement
Add Comment
Please, Sign In to add comment