ooshbin

TokenAction - Reliable Talent test

Dec 21st, 2020 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. // Reliable Talent test for future Token Action Maker update
  2. // type !rtTest to run the script - if the selected character has any levels
  3. // in Rogue, it should create the RT version of the Ability Check drop-down.
  4. // Only one 3d die will be thrown when using the RT version.
  5.  
  6. const tokenActionUpdate = (() => { // eslint-disable-line no-unused-vars
  7.  
  8. const handleInput = (msg) => {
  9. if (msg.type === 'api' && msg.content.match(/^!rtTest/i)) {
  10. let chars = getSelectedCharacters(msg.selected);
  11. if (!chars || chars.length < 1) return sendChat('','Nothing selected!');
  12. let c = chars[0];
  13. let reliableTalent = isRogue(c.id);
  14. let npcFlag = isNpc(c.id);
  15. createAbility('AbilityCheck', createDropDown('ability', reliableTalent, npcFlag), c.id);
  16. createAbility('SavingThrow', createDropDown('save', reliableTalent, npcFlag), c.id);
  17. if (reliableTalent) {
  18. createAttribute('rtypeRT', '@{rtype}0]]}} {{r2=[[@{d20RT}', '', c.id);
  19. createAttribute('d20RT', '{[[1d20]]', '', c.id);
  20. }
  21. sendChat('', `Ability created on ${c.get('name')}. Character has Rogue levels?? **${reliableTalent}.**`)
  22. }
  23. }
  24.  
  25. const getSelectedCharacters = function(selected) {
  26. return _.chain(selected)
  27. .map(function(s) {
  28. return getObj(s._type, s._id);
  29. })
  30. .reject(_.isUndefined)
  31. .map(function(c) {
  32. return getObj('character', c.get('represents'));
  33. })
  34. .filter(_.identity)
  35. .value();
  36. }
  37.  
  38. const isNpc = function(id) {
  39. var checkNpc = findObjs({
  40. _type: 'attribute',
  41. _characterid: id,
  42. name: 'npc'
  43. });
  44. if (_.isUndefined(checkNpc[0])) {
  45. return false;
  46. } else {
  47. return checkNpc[0].get('current');
  48. }
  49. }
  50.  
  51. const createAbility = function(name, pattern, id, isTokenAction = true) {
  52. var checkAbility = findObjs({
  53. _type: 'ability',
  54. _characterid: id,
  55. name: name
  56. });
  57.  
  58. if (checkAbility[0]) {
  59. checkAbility[0].set({
  60. action: pattern
  61. });
  62. } else {
  63. createObj('ability', {
  64. name: name,
  65. action: pattern,
  66. characterid: id,
  67. istokenaction: isTokenAction
  68. });
  69. }
  70. }
  71.  
  72. const isRogue = (charId) => {
  73. let classes = [];
  74. classes = findObjs({type: 'attribute', characterid: charId})
  75. .filter((attr) => (attr.get('name').match(/multiclass\d\b/i) || attr.get('name').match(/^class\b/i)))
  76. .map((attr) => attr.get('current'));
  77. if (classes.some((attr) => attr.match(/rogue/i))) return true;
  78. else return false;
  79. }
  80.  
  81. const createAttribute = (attrName, attrCurrent = '', attrMax = '', charId) => {
  82. let attr = findObjs({characterid: charId, name: attrName});
  83. if (attr.length > 0) {
  84. attr[0].set({current: attrCurrent, max: attrMax});
  85. } else {
  86. createObj('attribute', {characterid: charId, name: attrName, current: attrCurrent, max: attrMax});
  87. log(`Attribute ${attrName} created on ${charId} . Current value: ${attrCurrent}}`);
  88. }
  89. }
  90.  
  91. const createDropDown = (checkType, reliableTalent = false, npc = false) => {
  92. const skillLabels = ['Acrobatics','Animal Handling','Arcana','Athletics','Deception','History','Insight','Intimidation','Investigation','Medicine','Nature','Perception','Performance','Persuasion','Religion','Sleight Of Hand','Stealth','Survival'];
  93. const skillArray = skillLabels.map(a => a.replace(/\s/g,'_'))
  94. const abilityArray = ['Strength','Dexterity','Constitution','Intelligence','Wisdom','Charisma'];
  95. const npcSaveArray = abilityArray.map((ab) => ab.slice(0,3).toUpperCase());
  96. let skillMacroArray = [], abilityMacroArray = [], saveMacroArray = [];
  97. let rtype = (reliableTalent) ? 'rtypeRT' : 'rtype'; // Reliable Talent variables
  98. let d20 = (reliableTalent) ? 'd20RT' : 'd20'; //
  99. let template = (npc) ? 'npc' : 'simple'; // NPC variables
  100. let npcPrefix = (npc) ? 'npc_' : ''; //
  101. let pcSuffix = (npc) ? '' : '_bonus'; //
  102. let npcName = (npc) ? '@{selected|npc_name_flag}' : ''; //
  103. let saveArray = (npc) ? npcSaveArray : abilityArray; //
  104. if (checkType === 'ability') { // Skills
  105. for (let i=0; i < skillArray.length; i++) {
  106. let RTkh1 = (reliableTalent) ? `&#44;[[@{selected|reliable_talent}*ceil(@{selected|${skillArray[i]}_prof}/100)]][RT]&#125;k1` : ''; // Reliable talent calculation
  107.  
  108. skillMacroArray.push(`|${skillLabels[i]},${RTkh1}+@{selected|${npcPrefix}${skillArray[i]}${pcSuffix}}@{selected|pbd_safe}[${(skillLabels[i]+' ').slice(0,skillLabels[i].indexOf(' '))}]]]&#125;&#125; {{rname=${skillLabels[i]}&#125;&#125; {{mod=@{selected|${npcPrefix}${skillArray[i]}${pcSuffix}}&#125;&#125; {{r1=[[@{selected|${d20}}${RTkh1} + @{selected|${npcPrefix}${skillArray[i]}${pcSuffix}}@{selected|pbd_safe}[${(skillLabels[i]+' ').slice(0,skillLabels[i].indexOf(' '))}]]]`)
  109. }
  110. for (let i=0; i < abilityArray.length; i++) { // Abilities w JOAT and Globals
  111. let RTAbilityFudge = (reliableTalent) ? '&#44;0&#125;k1' : ''; // Reliable Talent fix
  112.  
  113. abilityMacroArray.push(`|${abilityArray[i]},${RTAbilityFudge}+@{selected|${abilityArray[i]}_mod}@{selected|jack_attr}[${npcSaveArray[i]}]]]&#125;&#125; {{rname=${abilityArray[i]}&#125;&#125; {{mod=@{selected|${abilityArray[i]}_mod}@{selected|jack_bonus}&#125;&#125; {{r1=[[ @{selected|d20} + @{selected|${abilityArray[i]}_mod}@{selected|jack_attr}[${npcSaveArray[i]}]]]`);
  114. }
  115. let AbilityMacro = `@{selected|wtype}&{template:${template}} ${npcName} @{selected|${rtype}}?{Ability${skillMacroArray.join('')}${abilityMacroArray.join('')}}}} {{global=@{selected|global_skill_mod}}} {{type=Check}} {{typec=Check}} @{selected|charname_output}`;
  116. return AbilityMacro;
  117. }
  118. if (checkType === 'save') { // Saves
  119. for (let i=0; i < abilityArray.length; i++) {
  120. saveMacroArray.push(`|${abilityArray[i]}, +@{selected|${npcPrefix}${saveArray[i]}_save${pcSuffix}}@{selected|pbd_safe}[${npcSaveArray[i]} SAVE]]]&#125;&#125; {{rname=${abilityArray[i]} Save&#125;&#125 {{mod=@{selected|${abilityArray[i]}_save_bonus}&#125;&#125; {{r1=[[@{selected|d20}+@{selected|${npcPrefix}${saveArray[i]}_save${pcSuffix}}@{selected|pbd_safe}[${npcSaveArray[i]} SAVE]]]`);
  121. }
  122. let SaveMacro = `@{selected|wtype}&{template:${template}} ${npcName} @{selected|rtype}?{Saving Throw${saveMacroArray.join('')}}}} {{global=@{selected|global_save_mod}}} {{type=Save}} {{typec=Save}} @{selected|charname_output}`;
  123. return SaveMacro;
  124. }
  125. }
  126.  
  127. const registerEventHandlers = () => {
  128. on('chat:message', handleInput);
  129. }
  130.  
  131. on('ready', () => {
  132. //checkInstall();
  133. registerEventHandlers();
  134. });
  135. return;
  136. })();
Add Comment
Please, Sign In to add comment