TheTural

FoundryVTT Gold Distribution Macro

Feb 19th, 2021
896
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let actors = canvas.tokens.controlled.map(({ actor }) => actor);
  2. let actorCount = actors.length;
  3. let actorIncrement = 0;
  4. let randomActor = Math.floor(Math.random() * actorCount);
  5. let winningActor = '';
  6.  
  7. let permissionCheck = false;
  8. if (game.user.isGM == true || game.user.isTrusted == true) { permissionCheck = true; }
  9.  
  10. function awardCurrency(totalPP, totalGP, totalEP, totalSP, totalCP)
  11. {
  12.     let splitPP = Math.floor(totalPP / actorCount);
  13.     let splitGP = Math.floor(totalGP / actorCount);
  14.     let splitEP = Math.floor(totalEP / actorCount);
  15.     let splitSP = Math.floor(totalSP / actorCount);
  16.     let splitCP = Math.floor(totalCP / actorCount);
  17.    
  18.     let leftoverPP = totalPP % actorCount;
  19.     let leftoverGP = totalGP % actorCount;
  20.     let leftoverEP = totalEP % actorCount;
  21.     let leftoverSP = totalSP % actorCount;
  22.     let leftoverCP = totalCP % actorCount;
  23.        
  24.     actors.forEach(actor =>
  25.     {
  26.         if(actorIncrement == randomActor)
  27.         {
  28.             winningActor = actor.data.name;
  29.             actor.data.data.currency.pp += splitPP + leftoverPP;
  30.             actor.data.data.currency.gp += splitGP + leftoverGP;
  31.             actor.data.data.currency.ep += splitEP + leftoverEP;
  32.             actor.data.data.currency.sp += splitSP + leftoverSP;
  33.             actor.data.data.currency.cp += splitCP + leftoverCP;
  34.         }
  35.         else
  36.         {
  37.             actor.data.data.currency.pp += splitPP;
  38.             actor.data.data.currency.gp += splitGP;
  39.             actor.data.data.currency.ep += splitEP;
  40.             actor.data.data.currency.sp += splitSP;
  41.             actor.data.data.currency.cp += splitCP;
  42.         }
  43.         actorIncrement++;
  44.     });
  45.    
  46.     let strOutput = "<b>Gave " + actorCount + " players each</b>: ";
  47.     strOutput = strOutput + splitPP + "pp, ";
  48.     strOutput = strOutput + splitGP + "gp, ";
  49.     strOutput = strOutput + splitEP + "ep, ";
  50.     strOutput = strOutput + splitSP + "sp, ";
  51.     strOutput = strOutput + splitCP + "cp";
  52.    
  53.     if (leftoverPP != 0 || leftoverGP != 0 || leftoverEP != 0 || leftoverSP != 0 || leftoverCP != 0)
  54.     {
  55.         strOutput = strOutput + "<br /><b>" + winningActor + "</b> gets the excess (";
  56.         strOutput = strOutput + leftoverPP + "pp, ";
  57.         strOutput = strOutput + leftoverGP + "gp, ";
  58.         strOutput = strOutput + leftoverEP + "ep, ";
  59.         strOutput = strOutput + leftoverSP + "sp, ";
  60.         strOutput = strOutput + leftoverCP + "cp";
  61.         strOutput = strOutput + ")";
  62.     }
  63.    
  64.     ChatMessage.create({content: strOutput});
  65. };
  66.  
  67. let currencyTotals = permissionCheck ? `
  68. <b>Currency Totals:</b><br />
  69. <div style="display: flex; width: 100%; margin: 10px 0px 10px 0px">
  70.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">PP:</label>
  71.     <input type="number" id="pp" name="pp" />
  72.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">GP:</label>
  73.     <input type="number" id="gp" name="gp" />
  74.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">EP:</label>
  75.     <input type="number" id="ep" name="ep" />
  76.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">SP:</label>
  77.     <input type="number" id="sp" name="sp"/ >
  78.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">CP:</label>
  79.     <input type="number" id="cp" name="cp"/ >
  80. </div>
  81. ` : '';
  82.  
  83. new Dialog({
  84.     title: `Distribute Currency`,
  85.     content: `
  86.         <form>
  87.             ${currencyTotals}
  88.         </form>
  89.     `,
  90.     buttons: {
  91.         yes: {
  92.             icon: "<i class='fas fa-check'></i>",
  93.             label: `Distribute`,
  94.             callback: (html) => {
  95.                 let totalPP = html.find('#pp').val();
  96.                 let totalGP = html.find('#gp').val();
  97.                 let totalEP = html.find('#ep').val();
  98.                 let totalSP = html.find('#sp').val();
  99.                 let totalCP = html.find('#cp').val();
  100.                
  101.                 if (totalPP == null || Number.isInteger(+totalPP) == false) { totalPP = 0; }
  102.                 if (totalGP == null || Number.isInteger(+totalGP) == false) { totalGP = 0; }
  103.                 if (totalEP == null || Number.isInteger(+totalEP) == false) { totalEP = 0; }
  104.                 if (totalSP == null || Number.isInteger(+totalSP) == false) { totalSP = 0; }
  105.                 if (totalCP == null || Number.isInteger(+totalCP) == false) { totalCP = 0; }
  106.                
  107.                 if (permissionCheck) {
  108.                     awardCurrency(totalPP, totalGP, totalEP, totalSP, totalCP);
  109.                 }
  110.             }
  111.         },
  112.         no: {
  113.             icon: "<i class='fas fa-times'></i>",
  114.             label: `Cancel`
  115.         },
  116.     },
  117.     default: "yes"
  118. }).render(true)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×