index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. const path = require('path'); const fs = require('fs');
  2. module.exports = function AutoPOT(mod) {
  3. const cmd = mod.command || mod.require.command;
  4. let config = getConfig(), hpPot = getHP(), mpPot = getMP();
  5. let gPot = null, isSlaying = false, inUpdate = false;
  6. mod.game.initialize(['me', 'contract']);
  7. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  8. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  9. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  10. switch (arg1) {
  11. case 'id':
  12. case 'getid':
  13. case 'itemid':
  14. let getId = arg2.match(/#(\d*)@/);
  15. getId = getId ? Number(getId[1]) : 0;
  16. msg(`itemId: ${getId}.`);
  17. break;
  18. case 'notice':
  19. config.notice = !config.notice;
  20. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  21. break;
  22. case 'slay':
  23. case 'slaying':
  24. isSlaying = !isSlaying;
  25. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}. ${config.hp ? '' : 'HP pot has Enable.'}`);
  26. if (!config.hp) config.hp = true;
  27. break;
  28. case 'hp':
  29. config.hp = !config.hp;
  30. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}. ${isSlaying ? 'Slaying mode has Disable.' : ''}`);
  31. if (isSlaying) isSlaying = false;
  32. break;
  33. case 'mp':
  34. case 'mana':
  35. config.mp = !config.mp;
  36. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  37. break;
  38. case 'option':
  39. case 'status':
  40. case 'debug':
  41. case 'check':
  42. let data = '\n===== Option =====\n';
  43. data += `Module: ${config.enabled}\n`;
  44. data += `HP: ${config.hp}\n`;
  45. data += `MP: ${config.mp}\n`;
  46. data += `Slaying: ${isSlaying}\n`;
  47. data += `Notice: ${config.notice}\n`;
  48. data += '===== Status =====\n';
  49. data += `inGame: ${mod.game.isIngame}\n`;
  50. data += `inLoading: ${mod.game.isInLoadingScreen}\n`;
  51. data += `isAlive: ${mod.game.me.alive}\n`;
  52. data += `onMount: ${mod.game.me.mounted}\n`;
  53. data += `inCombat: ${mod.game.me.inCombat}\n`;
  54. data += `inContract: ${mod.game.me.active}\n`;
  55. data += `inBattleground: ${mod.game.me.inBattleground}\n`;
  56. data += `inCivilUnrest: ${mod.game.me.zone === 152}\n`;
  57. data += '===== HP Potion =====\n';
  58. for (let hp = 0; hp < hpPot.length; hp++)
  59. if (hpPot[hp][1].amount > 0)
  60. data += `[${hp}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}\n`;
  61. data += '===== MP Potion =====\n';
  62. for (let mp = 0; mp < mpPot.length; mp++)
  63. if (mpPot[mp][1].amount > 0)
  64. data += `[${mp}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}\n`;
  65. msg(data);
  66. data = '';
  67. break;
  68. default:
  69. msg(`Wrong command :v`);
  70. break;
  71. }
  72. });
  73. mod.hook('S_INVEN', 17, e => {
  74. if (!inUpdate) {
  75. inUpdate = true;
  76. for(let hp = 0; hp < hpPot.length; hp++) {
  77. gPot = e.items.filter(item => item.id === s2n(hpPot[hp][0]));
  78. if (gPot.length > 0) hpPot[hp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  79. }
  80. for(let mp = 0; mp < mpPot.length; mp++) {
  81. gPot = e.items.filter(item => item.id === s2n(mpPot[mp][0]));
  82. if (gPot.length > 0) mpPot[mp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  83. }
  84. inUpdate = false;
  85. }
  86. });
  87. mod.hook('S_CREATURE_CHANGE_HP', 6, e => {
  88. if (config.enabled && config.hp) useHP(Math.round(s2n(e.curHp) / s2n(e.maxHp) * 100));
  89. });
  90. mod.hook('S_PLAYER_CHANGE_MP', 1, e => {
  91. if (config.enabled && config.mp) useMP(Math.round(s2n(e.currentMp) / s2n(e.maxMp) * 100));
  92. });
  93. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  94. if (config.enabled) {
  95. if (config.hp) useHP(Math.round(s2n(e.hp) / s2n(e.maxHp) * 100));
  96. if (config.mp) useMP(Math.round(s2n(e.mp) / s2n(e.maxMp) * 100));
  97. }
  98. });
  99. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  100. inUpdate = false;
  101. for (let hp = 0; hp < hpPot.length; hp++)
  102. hpPot[hp][1].amount = 0;
  103. for (let mp = 0; mp < mpPot.length; mp++)
  104. mpPot[mp][1].amount = 0;
  105. });
  106. function useHP(nowHP) {
  107. if (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active) {
  108. for (let hp = 0; hp < hpPot.length; hp++) {
  109. if (!hpPot[hp][1].inCd && hpPot[hp][1].amount > 0 && ((!isSlaying && nowHP <= hpPot[hp][1].use_at && (hpPot[hp][1].inCombat ? mod.game.me.inCombat : true)) || (isSlaying && nowHP <= hpPot[hp][1].slay_at && mod.game.me.inCombat)) && (hpPot[hp][1].inBattleground ? (mod.game.me.inBattleground || mod.game.me.zone === 152) : !mod.game.me.inBattleground)) {
  110. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  111. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  112. }
  113. }
  114. }
  115. }
  116. function useMP(nowMP) {
  117. if (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active) {
  118. for (let mp = 0; mp < mpPot.length; mp++) {
  119. if (!mpPot[mp][1].inCd && mpPot[mp][1].amount > 0 && nowMP <= mpPot[mp][1].use_at && (mpPot[mp][1].inCombat ? mod.game.me.inCombat : true) && (mpPot[mp][1].inBattleground ? (mod.game.me.inBattleground || mod.game.me.zone === 152) : !mod.game.me.inBattleground)) {
  120. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  121. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  122. }
  123. }
  124. }
  125. }
  126. function useItem(itemId) {
  127. mod.send('C_USE_ITEM', 3, {
  128. gameId: mod.game.me.gameId,
  129. id: s2n(itemId[0]),
  130. amount: 1,
  131. unk4: true
  132. });
  133. }
  134. function getConfig() {
  135. let data = {};
  136. try {
  137. data = jsonRequire('./config.json');
  138. } catch (e) {
  139. data = {
  140. enabled: true,
  141. hp: false,
  142. mp: true,
  143. notice: false
  144. }
  145. jsonSave('config.json', data);
  146. }
  147. return data;
  148. }
  149. function getHP() {
  150. let data = {};
  151. try {
  152. data = jsonRequire('./hp.json');
  153. } catch (e) {
  154. data[6552] = {
  155. name: 'Prime Recovery Potable',
  156. inBattleground: false,
  157. inCombat: true,
  158. use_at: 80,
  159. slay_at: 30,
  160. cd: 10
  161. }
  162. jsonSave('hp.json', data);
  163. }
  164. return jsonSort(data, 'use_at');
  165. }
  166. function getMP() {
  167. let data = {};
  168. try {
  169. data = jsonRequire('./mp.json');
  170. } catch (e) {
  171. data[6562] = {
  172. name: 'Prime Replenishment Potable',
  173. inBattleground: false,
  174. inCombat: false,
  175. use_at: 50,
  176. cd: 10
  177. }
  178. jsonSave('mp.json', data);
  179. }
  180. return jsonSort(data, 'use_at');
  181. }
  182. function s2n(n) {
  183. return Number(n);
  184. }
  185. function msg(msg) {
  186. cmd.message(msg);
  187. }
  188. function jsonRequire(data) {
  189. delete require.cache[require.resolve(data)];
  190. return require(data);
  191. }
  192. function jsonSort(data, sortby){
  193. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  194. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  195. return s2a;
  196. }
  197. function jsonSave(name, data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  198. }