Mendenbarr

Hit Dice Script

Apr 22nd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. on('chat:message', function(msg) {
  2. // ROLL LISTENER
  3.     if(msg.playerid.toLowerCase() != "api" && msg.rolltemplate) {
  4.         var cnamebase = msg.content.split("charname=")[1];
  5.         var cname = cnamebase ? cnamebase.replace('}}','').trim() : (msg.content.split("{{name=")[1]||'').split("}}")[0].trim();
  6.         var character = cname ? findObjs({name: cname, type: 'character'})[0] : undefined;
  7.         if(["simple"].indexOf(msg.rolltemplate) > -1) {
  8.             if(_.has(msg,'inlinerolls') && msg.content.indexOf("^{hit-dice-u}") > -1 && character) {
  9.                 handlehd(msg,character);
  10.             }
  11.         }
  12.     }
  13. });
  14. // CHECK CURRENT HD, DECREMENT HD, THEN APPLY HP
  15. var handlehd = function (msg,character) {
  16.     var hd = findObjs({type: 'attribute', characterid: character.id, name: "hit_dice"}, {caseInsensitive: true})[0];
  17.     var hp = findObjs({type: 'attribute', characterid: character.id, name: "hp"}, {caseInsensitive: true})[0];
  18.     if(!hd || hd.get("current")==="" || hd.get("max")==="") {
  19.         log("CHARACTER HAS NO HIT_DICE ATTRIBUTE OR HD CURRENT/MAX IS NULL");
  20.         sendChat(msg.who, "<div class='sheet-rolltemplate-simple' style='margin-top:-7px;'><div class='sheet-container'><div class='sheet-label' style='margin-top:5px;'><span>" + "HD attribute on " + character.get("name") + " is missing or current/max values are not filled out, Hit Points were not applied." + "</span></div></div></div>");
  21.         return;
  22.     }
  23.     else if(!hp || hp.get("current")==="" || hp.get("max")==="") {
  24.         log("CHARACTER HAS NO HP ATTRIBUTE OR HP CURRENT/MAX IS NULL");
  25.         sendChat(msg.who, "<div class='sheet-rolltemplate-simple' style='margin-top:-7px;'><div class='sheet-container'><div class='sheet-label' style='margin-top:5px;'><span>" + "HP attribute on " + character.get("name") + " is missing or current/max values are not filled out, Hit Points were not applied." + "</span></div></div></div>");
  26.         return;
  27.     }
  28.     else {
  29.         var curhd = parseInt(hd.get("current"));
  30.         var newhd = curhd - 1;
  31.     }
  32.     if (curhd === 0) {
  33.         sendChat(msg.who, "<div class='sheet-rolltemplate-simple' style='margin-top:-7px;'><div class='sheet-container'><div class='sheet-label' style='margin-top:5px;'><span>" + character.get("name") + " has no HD remaining, HP were not applied." + "</span></div></div></div>");
  34.     }
  35.     else {
  36.         hd.set({current:newhd});
  37.         var maxhp = parseInt(hp.get("max"));
  38.         var curhp = parseInt(hp.get("current"));
  39.         var result = msg.inlinerolls[2].results.total ? msg.inlinerolls[2].results.total : false;
  40.         var newhp = curhp + result;
  41.         if(result === false) {
  42.             log("FAILED TO GET HD RESULT");
  43.         }
  44.         else if (newhp > maxhp) {
  45.             hp.set({current:maxhp});
  46.         }
  47.         else {
  48.             hp.set({current:newhp});
  49.         }
  50.     }
  51. };
Advertisement
Add Comment
Please, Sign In to add comment