TheTural

FoundryVTT Gold Distribution Macro

Feb 19th, 2021 (edited)
2,104
1
Never
1
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.         let newTotalPP = actor.data.data.currency.pp + splitPP;
  27.         let newTotalGP = actor.data.data.currency.gp + splitGP;
  28.         let newTotalEP = actor.data.data.currency.ep + splitEP;
  29.         let newTotalSP = actor.data.data.currency.sp + splitSP;
  30.         let newTotalCP = actor.data.data.currency.cp + splitCP;
  31.        
  32.         if(actorIncrement == randomActor)
  33.         {
  34.             newTotalPP += leftoverPP;
  35.             newTotalGP += leftoverGP;
  36.             newTotalEP += leftoverEP;
  37.             newTotalSP += leftoverSP;
  38.             newTotalCP += leftoverCP;
  39.            
  40.             winningActor = actor.data.name;
  41.             actor.update({"data.currency.pp" : newTotalPP, "data.currency.gp" : newTotalGP, "data.currency.ep" : newTotalEP, "data.currency.sp" : newTotalSP, "data.currency.cp" : newTotalCP});
  42.         }
  43.         else
  44.         {
  45.             actor.update({"data.currency.pp" : newTotalPP, "data.currency.gp" : newTotalGP, "data.currency.ep" : newTotalEP, "data.currency.sp" : newTotalSP, "data.currency.cp" : newTotalCP});
  46.         }
  47.         actorIncrement++;
  48.     });
  49.    
  50.     let strOutput = "<b>Gave " + actorCount + " players each</b>:<br />";
  51.     if (splitPP > 0) { strOutput += "<span style='color:#90A2B6'>" + splitPP + "pp</span>"; if (splitGP > 0 || splitEP > 0 || splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
  52.     if (splitGP > 0) { strOutput += "<span style='color:#B08C34'>" + splitGP + "gp</span>"; if (splitEP > 0 || splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
  53.     if (splitEP > 0) { strOutput += "<span style='color:#617480'>" + splitEP + "ep</span>"; if (splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
  54.     if (splitSP > 0) { strOutput += "<span style='color:#717773'>" + splitSP + "sp</span>"; if (splitCP > 0) { strOutput += ", "; } }
  55.     if (splitCP > 0) { strOutput += "<span style='color:#9D5934'>" + splitCP + "cp</span>"; }
  56.    
  57.     if (leftoverPP > 0 || leftoverGP > 0 || leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0)
  58.     {
  59.         strOutput = strOutput + "<hr /><b>" + winningActor + " gets the remainder</b>:<br />";
  60.        
  61.         if (leftoverPP > 0) { strOutput += "<span style='color:#90A2B6'>" + leftoverPP + "pp</span>"; if (leftoverGP > 0 || leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }       
  62.         if (leftoverGP > 0) { strOutput += "<span style='color:#B08C34'>" + leftoverGP + "gp</span>"; if (leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }
  63.         if (leftoverEP > 0) { strOutput += "<span style='color:#617480'>" + leftoverEP + "ep</span>"; if (leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }
  64.         if (leftoverSP > 0) { strOutput += "<span style='color:#717773'>" + leftoverSP + "sp</span>"; if (leftoverCP > 0) { strOutput += ", "; } }
  65.         if (leftoverCP > 0) { strOutput += "<span style='color:#9D5934'>" + leftoverCP + "cp</span>"; }
  66.     }
  67.    
  68.     ChatMessage.create({content: strOutput});
  69. };
  70.  
  71. let currencyTotals = permissionCheck ? `
  72. <b>Currency Totals:</b><br />
  73. <div style="display: flex; width: 100%; margin: 10px 0px 10px 0px">
  74.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">PP:</label>
  75.     <input type="number" id="pp" name="pp" />
  76.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">GP:</label>
  77.     <input type="number" id="gp" name="gp" />
  78.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">EP:</label>
  79.     <input type="number" id="ep" name="ep" />
  80.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">SP:</label>
  81.     <input type="number" id="sp" name="sp"/ >
  82.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">CP:</label>
  83.     <input type="number" id="cp" name="cp"/ >
  84. </div>
  85. ` : '';
  86.  
  87. new Dialog({
  88.     title: `Distribute Currency`,
  89.     content: `
  90.         <form>
  91.             ${currencyTotals}
  92.         </form>
  93.     `,
  94.     buttons: {
  95.         yes: {
  96.             icon: "<i class='fas fa-check'></i>",
  97.             label: `Distribute`,
  98.             callback: (html) => {
  99.                 let totalPP = html.find('#pp').val();
  100.                 let totalGP = html.find('#gp').val();
  101.                 let totalEP = html.find('#ep').val();
  102.                 let totalSP = html.find('#sp').val();
  103.                 let totalCP = html.find('#cp').val();
  104.                
  105.                 if (totalPP == null || Number.isInteger(+totalPP) == false) { totalPP = 0; }
  106.                 if (totalGP == null || Number.isInteger(+totalGP) == false) { totalGP = 0; }
  107.                 if (totalEP == null || Number.isInteger(+totalEP) == false) { totalEP = 0; }
  108.                 if (totalSP == null || Number.isInteger(+totalSP) == false) { totalSP = 0; }
  109.                 if (totalCP == null || Number.isInteger(+totalCP) == false) { totalCP = 0; }
  110.                
  111.                 if (permissionCheck) {
  112.                     awardCurrency(totalPP, totalGP, totalEP, totalSP, totalCP);
  113.                 }
  114.             }
  115.         },
  116.         no: {
  117.             icon: "<i class='fas fa-times'></i>",
  118.             label: `Cancel`
  119.         },
  120.     },
  121.     default: "yes"
  122. }).render(true)
Comments
  • justAnotherIdea
    269 days (edited)
    1. //This was super helpful, thank you! I added another button to my version that evenly distributes their already acquired currency as well.
    2.  
    3. let actors = canvas.tokens.controlled.map(({ actor }) => actor);
    4. let actorCount = actors.length;
    5. let actorIncrement = 0;
    6. let randomActor = Math.floor(Math.random() * actorCount);
    7. let winningActor = '';
    8.  
    9. let permissionCheck = false;
    10. if (game.user.isGM == true || game.user.isTrusted == true) { permissionCheck = true; }
    11.  
    12. function awardCurrency(totalPP, totalGP, totalEP, totalSP, totalCP)
    13. {
    14.     let splitPP = Math.floor(totalPP / actorCount);
    15.     let splitGP = Math.floor(totalGP / actorCount);
    16.     let splitEP = Math.floor(totalEP / actorCount);
    17.     let splitSP = Math.floor(totalSP / actorCount);
    18.     let splitCP = Math.floor(totalCP / actorCount);
    19.    
    20.     let leftoverPP = totalPP % actorCount;
    21.     let leftoverGP = totalGP % actorCount;
    22.     let leftoverEP = totalEP % actorCount;
    23.     let leftoverSP = totalSP % actorCount;
    24.     let leftoverCP = totalCP % actorCount;
    25.        
    26.     actors.forEach(actor =>
    27.     {
    28.         let newTotalPP = actor.data.data.currency.pp + splitPP;
    29.         let newTotalGP = actor.data.data.currency.gp + splitGP;
    30.         let newTotalEP = actor.data.data.currency.ep + splitEP;
    31.         let newTotalSP = actor.data.data.currency.sp + splitSP;
    32.         let newTotalCP = actor.data.data.currency.cp + splitCP;
    33.        
    34.         if(actorIncrement == randomActor)
    35.         {
    36.             newTotalPP += leftoverPP;
    37.             newTotalGP += leftoverGP;
    38.             newTotalEP += leftoverEP;
    39.             newTotalSP += leftoverSP;
    40.             newTotalCP += leftoverCP;
    41.            
    42.             winningActor = actor.data.name;
    43.             actor.update({"data.currency.pp" : newTotalPP, "data.currency.gp" : newTotalGP, "data.currency.ep" : newTotalEP, "data.currency.sp" : newTotalSP, "data.currency.cp" : newTotalCP});
    44.         }
    45.         else
    46.         {
    47.             actor.update({"data.currency.pp" : newTotalPP, "data.currency.gp" : newTotalGP, "data.currency.ep" : newTotalEP, "data.currency.sp" : newTotalSP, "data.currency.cp" : newTotalCP});
    48.         }
    49.         actorIncrement++;
    50.     });
    51.    
    52.     let strOutput = "<b>Gave " + actorCount + " players each</b>:<br />";
    53.     if (splitPP > 0) { strOutput += "<span style='color:#90A2B6'>" + splitPP + "pp</span>"; if (splitGP > 0 || splitEP > 0 || splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
    54.     if (splitGP > 0) { strOutput += "<span style='color:#B08C34'>" + splitGP + "gp</span>"; if (splitEP > 0 || splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
    55.     if (splitEP > 0) { strOutput += "<span style='color:#617480'>" + splitEP + "ep</span>"; if (splitSP > 0 || splitCP > 0) { strOutput += ", "; } }
    56.     if (splitSP > 0) { strOutput += "<span style='color:#717773'>" + splitSP + "sp</span>"; if (splitCP > 0) { strOutput += ", "; } }
    57.     if (splitCP > 0) { strOutput += "<span style='color:#9D5934'>" + splitCP + "cp</span>"; }
    58.    
    59.     if (leftoverPP > 0 || leftoverGP > 0 || leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0)
    60.     {
    61.         strOutput = strOutput + "<hr /><b>" + winningActor + " gets the remainder</b>:<br />";
    62.        
    63.         if (leftoverPP > 0) { strOutput += "<span style='color:#90A2B6'>" + leftoverPP + "pp</span>"; if (leftoverGP > 0 || leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }       
    64.         if (leftoverGP > 0) { strOutput += "<span style='color:#B08C34'>" + leftoverGP + "gp</span>"; if (leftoverEP > 0 || leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }
    65.         if (leftoverEP > 0) { strOutput += "<span style='color:#617480'>" + leftoverEP + "ep</span>"; if (leftoverSP > 0 || leftoverCP > 0) { strOutput += ", "; } }
    66.         if (leftoverSP > 0) { strOutput += "<span style='color:#717773'>" + leftoverSP + "sp</span>"; if (leftoverCP > 0) { strOutput += ", "; } }
    67.         if (leftoverCP > 0) { strOutput += "<span style='color:#9D5934'>" + leftoverCP + "cp</span>"; }
    68.     }
    69.    
    70.     ChatMessage.create({content: strOutput});
    71. };
    72.  
    73. let currencyTotals = permissionCheck ? `
    74. <b>Currency Totals:</b><br />
    75. <div style="display: flex; width: 100%; margin: 10px 0px 10px 0px">
    76.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">PP:</label>
    77.     <input type="number" id="pp" name="pp" />
    78.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">GP:</label>
    79.     <input type="number" id="gp" name="gp" />
    80.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">EP:</label>
    81.     <input type="number" id="ep" name="ep" />
    82.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">SP:</label>
    83.     <input type="number" id="sp" name="sp"/ >
    84.     <label for="pp" style="white-space: nowrap; margin: 4px 10px 0px 10px;">CP:</label>
    85.     <input type="number" id="cp" name="cp"/ >
    86. </div>
    87. ` : '';
    88.  
    89. new Dialog({
    90.     title: `Distribute Currency`,
    91.     content: `
    92.         <form>
    93.             ${currencyTotals}
    94.         </form>
    95.     `,
    96.     buttons: {
    97.         yes: {
    98.             icon: "<i class='fas fa-check'></i>",
    99.             label: `Distribute`,
    100.             callback: (html) => {
    101.                 let totalPP = html.find('#pp').val();
    102.                 let totalGP = html.find('#gp').val();
    103.                 let totalEP = html.find('#ep').val();
    104.                 let totalSP = html.find('#sp').val();
    105.                 let totalCP = html.find('#cp').val();
    106.                
    107.                 if (totalPP == null || Number.isInteger(+totalPP) == false) { totalPP = 0; }
    108.                 if (totalGP == null || Number.isInteger(+totalGP) == false) { totalGP = 0; }
    109.                 if (totalEP == null || Number.isInteger(+totalEP) == false) { totalEP = 0; }
    110.                 if (totalSP == null || Number.isInteger(+totalSP) == false) { totalSP = 0; }
    111.                 if (totalCP == null || Number.isInteger(+totalCP) == false) { totalCP = 0; }
    112.                
    113.                 if (permissionCheck) {
    114.                     awardCurrency(totalPP, totalGP, totalEP, totalSP, totalCP);
    115.                 }
    116.             }
    117.         },
    118.         no: {
    119.             icon: "<i class='fas fa-times'></i>",
    120.             label: `Cancel`
    121.         },
    122.         get: {
    123.             icon: "<i class='fas fa-rotate-right'></i>",
    124.             label: `Even Out`,
    125.             callback: (html) => {
    126.                 let totalPP = html.find('#pp');
    127.                 let totalGP = html.find('#gp');
    128.                 let totalEP = html.find('#ep');
    129.                 let totalSP = html.find('#sp');
    130.                 let totalCP = html.find('#cp');
    131.                 let sumPP = 0;
    132.                 let sumGP = 0;
    133.                 let sumEP = 0;
    134.                 let sumSP = 0;
    135.                 let sumCP = 0;
    136.  
    137.                 actors.forEach(actor =>
    138.                     {
    139.                         sumPP = sumPP + actor.data.data.currency.pp;
    140.                         sumGP = sumGP + actor.data.data.currency.gp;
    141.                         sumEP = sumEP + actor.data.data.currency.ep;
    142.                         sumSP = sumSP + actor.data.data.currency.sp;
    143.                         sumCP = sumCP + actor.data.data.currency.cp;
    144.                        
    145.                         actor.data.data.currency.pp = 0;
    146.                         actor.data.data.currency.gp = 0;
    147.                         actor.data.data.currency.ep = 0;
    148.                         actor.data.data.currency.sp = 0;
    149.                         actor.data.data.currency.cp = 0;
    150.                 });
    151.                 totalPP.val(sumPP);
    152.                 totalGP.val(sumGP);
    153.                 totalEP.val(sumEP);
    154.                 totalSP.val(sumSP);
    155.                 totalCP.val(sumCP);
    156.  
    157.                 awardCurrency(sumPP, sumGP, sumEP, sumSP, sumCP);
    158.             }
    159.         },
    160.     },
    161.     default: "yes"
    162. }).render(true)
Add Comment
Please, Sign In to add comment