drexl93 / roll-tracker

MIT License
6 stars 5 forks source link

Explanation of how DsN roll works #11

Closed jacobwojoski closed 1 year ago

jacobwojoski commented 1 year ago

A mediocre attempt at how the DsN fn works. Hope this help

//Fn Makes a hook to wait for the diceSoNiceRoll to finish //It does endlessly loop "kinda" basically makes a hook to look for dice so nice roll complete msg //Maybe this will help explain it static async waitFor3DDiceMessage(targetMessageId) {

  //Build hook hook that waits for A DsN msg then return
  //This funtion returns after Hook is made so this code returns and now were 
  //waiting at the end of promise fn to get our resolve
  //code below hook is called by something else (async part)
  //would have liked to pull this code out of waitFor3DDiceMessage fn but then it looses access to targetMessageId 
  //var Could make it global but figured id just keep it the way it is
  function buildHook(resolve) {

      Hooks.once('diceSoNiceRollComplete', (messageId) => {
          //This code ONLY gets called if a DsN roll completed (async code)

          //Is the DsNmsg the one we were waiting for
          if (targetMessageId === messageId){
              //got msg we wanted so we can return now
              resolve(true);
          } else {
              //We got a completed msg but was wrong one, Need to build a new hook to wait for 
              //a new msg
              buildHook(resolve);
          }
      });
  }

  //Promise means the following is async dont return untill we get a result
  return new Promise((resolve, reject) => {

      //If 3d Dice is installed build hook (The async thing)
      if(game.dice3d){
          buildHook(resolve);
      } else {
          resolve(true);
      }
  });

}

drexl93 commented 1 year ago

Thank you for this!