Guest User

Untitled

a guest
Oct 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. var handleammo = function (msg,character,player) {
  2. if(msg.content.indexOf("ammo=") === -1) {
  3. // UNABLE TO FIND AMMO
  4. return;
  5. }
  6. var ammostr;
  7. if(msg.content.indexOf("{{charname=") > -1) {
  8. ammostr = (msg.content.split("ammo=")[1]||'').split(" {{charname=")[0];
  9. }
  10. else {
  11. ammostr = (msg.content.split("ammo=")[1]||'').split(" charname")[0];
  12. }
  13. _.each(ammostr.split(","), function(ammofull) {
  14. ammofull = ammofull.trim();
  15. var ammoid = "";
  16. var ammoname = "";
  17. // check to see if more than one ammo is used
  18. var ammouseregex = /[[].*]/;
  19. var ammousestr = ammouseregex.exec(ammofull) ? ammofull.match(ammouseregex)[0] : null;
  20. ammofull = ammofull.replace(ammousestr,"");
  21. var ammouses = ammousestr ? parseInt(ammousestr.replace("[","").replace("]")) : 1;
  22. // get the name and id for the ammo resource
  23. if(ammofull.substring(0,1) === "-") {
  24. // ammofull was just the id for the resource
  25. ammoid = ammofull;
  26. var ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0];
  27. if(ammoresource) {
  28. ammoname = getAttrByName(character.id, ammoresource.get("name") + "_name");
  29. }
  30. }
  31. else if(ammofull.indexOf("|") > -1) {
  32. // ammofull contained the id for the resource
  33. var temp = ammofull.split("|");
  34. ammoid = temp[1];
  35. ammoname = temp[0];
  36. }
  37. else {
  38. // no id was found for the resource
  39. ammoname = ammofull;
  40. var resources = filterObjs(function(obj) {
  41. return obj.get("type") === "attribute"
  42. && obj.get("characterid") === character.id
  43. && (obj.get("current") + "").toLowerCase() === ammofull.toLowerCase()
  44. && obj.get("name").indexOf("resource") > -1;
  45. });
  46. if(!resources[0]) {
  47. log("UNABLE TO FIND RESOURCE");
  48. return;
  49. }
  50. var resname = resources[0].get("name").replace("_name","");
  51. ammoid = findObjs({type: 'attribute', characterid: character.id, name: resname}, {caseInsensitive: true})[0].id;
  52. var atkid = null;
  53. var reg = new RegExp("repeating_attack_(.+)_attack_dmg");
  54. var match = reg.exec(msg.content);
  55. if(match) {
  56. atkid = match[1];
  57. }
  58. else {
  59. var atkname = (msg.content.split("{{rname=")[1]||'').split("}}")[0];
  60. var attacks = filterObjs(function(obj) {
  61. return obj.get("type") === "attribute"
  62. && obj.get("characterid") === character.id
  63. && obj.get("current") === atkname;
  64. });
  65. reg = new RegExp("repeating_attack_(.+)_atkname");
  66. _.each(attacks, function(atk) {
  67. match = reg.exec(atk.get("name"));
  68. if(match) {
  69. atkid = match[1];
  70. }
  71. });
  72. }
  73. if(!atkid) {
  74. log("UNABLE TO FIND ATTACK ID");
  75. return;
  76. }
  77. var atkammo = findObjs({type: 'attribute', characterid: character.id, name: "repeating_attack_" + atkid + "_ammo"}, {caseInsensitive: true})[0];
  78. var value = ammousestr
  79. ? atkammo.get("current").replace(ammoname+ammousestr, ammoname+ammousestr+"|"+ammoid)
  80. : atkammo.get("current").replace(ammoname, ammoname + "|" + ammoid);
  81. atkammo.set({current: value});
  82. }
  83. ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0];
  84. if(ammoresource) {
  85. ammoresource.set({current: ammoresource.get("current") - ammouses});
  86. var ammoitemid = getAttrByName(character.id, ammoresource.get("name") + "_itemid");
  87. if(ammoitemid) {
  88. var ammoitem = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemcount"}, {caseInsensitive: true})[0];
  89. if(ammoitem) {
  90. ammoitem.set({current: ammoitem.get("current") - 1});
  91. }
  92. var ammoweight = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemweight"}, {caseInsensitive: true})[0];
  93. var totalweight = findObjs({type: 'attribute', characterid: character.id, name: "weighttotal"}, {caseInsensitive: true})[0];
  94. if(ammoweight && totalweight) {
  95. totalweight.set({current: totalweight.get("current") - ammoweight.get("current")});
  96. }
  97. }
  98. var output = ammoname + ":\n" + ((ammoresource.get("current") >= 0) ? ammoresource.get("current") + " LEFT" : "OUT OF AMMO");
  99. var formattedOutput = (getAttrByName(character.id, "wtype") === "" ? "" : "/w gm ") + "&{template:desc} {{desc=" + output + "}}";
  100. if(ammoresource.get("current") <= 0 || state.FifthEditionOGLbyRoll20.ammotracking !== "quiet") {
  101. sendChat(msg.who, formattedOutput);
  102. }
  103. }
  104. });
  105. };
Add Comment
Please, Sign In to add comment