You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.2 KiB

  1. var generateUUID = (function() {
  2. "use strict";
  3. var a = 0, b = [];
  4. return function() {
  5. var c = (new Date()).getTime() + 0, d = c === a;
  6. a = c;
  7. for (var e = new Array(8), f = 7; 0 <= f; f--) {
  8. e[f] = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(c % 64);
  9. c = Math.floor(c / 64);
  10. }
  11. c = e.join("");
  12. if (d) {
  13. for (f = 11; 0 <= f && 63 === b[f]; f--) {
  14. b[f] = 0;
  15. }
  16. b[f]++;
  17. } else {
  18. for (f = 0; 12 > f; f++) {
  19. b[f] = Math.floor(64 * Math.random());
  20. }
  21. }
  22. for (f = 0; 12 > f; f++){
  23. c += "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(b[f]);
  24. }
  25. return c;
  26. };
  27. }()),
  28. generateRowID = function () {
  29. "use strict";
  30. return generateUUID().replace(/_/g, "Z");
  31. };
  32. var addAttr = function(char_id, name, value, max) {
  33. let obj = {
  34. name: name,
  35. current: value,
  36. characterid: char_id
  37. };
  38. if(max) {
  39. obj.max = max;
  40. }
  41. return createObj("attribute", obj);
  42. };
  43. on("chat:message", function(msg) {
  44. if(msg.type == "api" && msg.content.indexOf("!acksimport ") !== -1) {
  45. var default_token_images = {
  46. "arcane": "",
  47. "divine": "https://s3.amazonaws.com/files.d20.io/images/137282084/8KCYW2UxnjP83Frhl0rLyg/thumb.png?1590201189",
  48. "physical": ""
  49. };
  50. var data = msg.content.replace("!acksimport ", "");
  51. var npcs = JSON.parse(data);
  52. for(let c of npcs) {
  53. sendChat(msg.who, "Creating NPC: " + c.name);
  54. // Create the character sheet for this NPC
  55. let sheet = createObj("character", {
  56. name: c.name
  57. });
  58. sheet.set('bio', "A generated npc character.");
  59. sheet.set('gmnotes', "Generated by Atr0phyACKS");
  60. // Add all of their attributes and mods
  61. addAttr(sheet.id, "str", c.attributes.str, null);
  62. addAttr(sheet.id, "int", c.attributes.int, null);
  63. addAttr(sheet.id, "wis", c.attributes.wis, null);
  64. addAttr(sheet.id, "dex", c.attributes.dex, null);
  65. addAttr(sheet.id, "con", c.attributes.con, null);
  66. addAttr(sheet.id, "chr", c.attributes.chr, null);
  67. addAttr(sheet.id, "str_mod", c.attribute_mods.str, null);
  68. addAttr(sheet.id, "int_mod", c.attribute_mods.int, null);
  69. addAttr(sheet.id, "wis_mod", c.attribute_mods.wis, null);
  70. addAttr(sheet.id, "dex_mod", c.attribute_mods.dex, null);
  71. addAttr(sheet.id, "con_mod", c.attribute_mods.con, null);
  72. addAttr(sheet.id, "chr_mod", c.attribute_mods.chr, null);
  73. // Add their Hit Points
  74. addAttr(sheet.id, "hp", c.hp, c.hp);
  75. // Set the guild and level
  76. addAttr(sheet.id, "class", c.guild, null);
  77. addAttr(sheet.id, "level", c.level, null);
  78. addAttr(sheet.id, "align", c.alignment, null);
  79. // Set the AC and attack throw
  80. addAttr(sheet.id, "ac", c.armour.ac_mod, null);
  81. addAttr(sheet.id, "attack_throw", c.attack_throw, null);
  82. addAttr(sheet.id, "attack_throw_vis", c.attack_throw, null);
  83. // Set the saves
  84. addAttr(sheet.id, "save_pp", c.saves.pp, null);
  85. addAttr(sheet.id, "save_pd", c.saves.pd, null);
  86. addAttr(sheet.id, "save_bb", c.saves.bb, null);
  87. addAttr(sheet.id, "save_sw", c.saves.sw, null);
  88. addAttr(sheet.id, "save_sp", c.saves.sp, null);
  89. /* Add our Repeating Sections */
  90. // for armour
  91. let armour_name = "repeating_equipment_" + generateRowID();
  92. addAttr(sheet.id, armour_name + "_equipment_name", c.armour.name, null);
  93. // for weapons
  94. let melee_name = "repeating_attacks_" + generateRowID();
  95. addAttr(sheet.id, melee_name + "_attack_name", c.melee.name, null);
  96. addAttr(sheet.id, melee_name + "_attack_throw_mod", c.melee.throw_mod, null);
  97. addAttr(sheet.id, melee_name + "_attack_dmg", c.melee.damage, null);
  98. // for spells
  99. if(c.spells) {
  100. for(spell of c.spells) {
  101. let spell_name = "repeating_spells_" + generateRowID();
  102. addAttr(sheet.id, spell_name + "_spell_name", spell.name, null);
  103. addAttr(sheet.id, spell_name + "_spell_effect", Base64.decode(spell.description), null);
  104. addAttr(sheet.id, spell_name + "_spell_range", spell.range, null);
  105. addAttr(sheet.id, spell_name + "_spell_roll", spell.roll || 0, null);
  106. if(spell.is_arcane) {
  107. addAttr(sheet.id, spell_name + "_spell_school", "Arcane", null);
  108. addAttr(sheet.id, spell_name + "_spell_level", spell.arcane, null);
  109. }
  110. if(spell.is_divine) {
  111. addAttr(sheet.id, spell_name + "_spell_school", "Divine", null);
  112. addAttr(sheet.id, spell_name + "_spell_level", spell.divine, null);
  113. }
  114. }
  115. }
  116. // populate the default token
  117. let default_token = createObj("graphic", {
  118. "imgsrc": default_token_images["divine"],
  119. represents: sheet.id,
  120. "showname": true,
  121. "layer": "objects",
  122. "pageid": Campaign().get('playerpageid')
  123. });
  124. setDefaultTokenForCharacter(sheet, default_token);
  125. }
  126. }
  127. if(msg.type == "api" && msg.content.indexOf("!acksdeleteall") !== -1) {
  128. let characters = findObjs({type: 'character'});
  129. for(let character of characters) {
  130. character.get('gmnotes', function(val) {
  131. if(val === "Generated by Atr0phyACKS") {
  132. sendChat(msg.who, "Deleting generated NPC: " + character.get('name'));
  133. character.remove();
  134. }
  135. });
  136. }
  137. }
  138. });