index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(), inUpdate = false;
  5. let gPot = null, isReady = false, isSlaying = false, nowHP = 0, nowMP = 0;
  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 === Number(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 === Number(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_PLAYER_STAT_UPDATE', 10, e => {
  88. if (config.enabled) {
  89. isReady = mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active;
  90. if (config.hp && isReady) {
  91. nowHP = Math.round(parseInt(e.hp) / parseInt(e.maxHp) * 100);
  92. for (let hp = 0; hp < hpPot.length; hp++) {
  93. 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)) {
  94. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  95. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  96. }
  97. }
  98. }
  99. if (config.mp && isReady) {
  100. nowMP = Math.round(parseInt(e.mp) / parseInt(e.maxMp) * 100);
  101. for (let mp = 0; mp < mpPot.length; mp++) {
  102. 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)) {
  103. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  104. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  105. }
  106. }
  107. }
  108. }
  109. });
  110. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  111. inUpdate = false;
  112. for (let hp = 0; hp < hpPot.length; hp++)
  113. hpPot[hp][1].amount = 0;
  114. for (let mp = 0; mp < mpPot.length; mp++)
  115. mpPot[mp][1].amount = 0;
  116. });
  117. function useItem(itemId) {
  118. mod.send('C_USE_ITEM', 3, {
  119. gameId: mod.game.me.gameId,
  120. id: Number(itemId[0]),
  121. amount: 1,
  122. unk4: true
  123. });
  124. }
  125. function getConfig() {
  126. let data = {};
  127. try {
  128. data = jsonRequire('./config.json');
  129. } catch (e) {
  130. data = {
  131. enabled: true,
  132. hp: false,
  133. mp: true,
  134. notice: false
  135. }
  136. jsonSave('config.json', data);
  137. }
  138. return data;
  139. }
  140. function getHP() {
  141. let data = {};
  142. try {
  143. data = jsonRequire('./hp.json');
  144. } catch (e) {
  145. data[6552] = {
  146. name: 'Prime Recovery Potable',
  147. inBattleground: false,
  148. inCombat: true,
  149. use_at: 80,
  150. slay_at: 30,
  151. cd: 10
  152. }
  153. jsonSave('hp.json', data);
  154. }
  155. return jsonSort(data, 'use_at');
  156. }
  157. function getMP() {
  158. let data = {};
  159. try {
  160. data = jsonRequire('./mp.json');
  161. } catch (e) {
  162. data[6562] = {
  163. name: 'Prime Replenishment Potable',
  164. inBattleground: false,
  165. inCombat: false,
  166. use_at: 50,
  167. cd: 10
  168. }
  169. jsonSave('mp.json', data);
  170. }
  171. return jsonSort(data, 'use_at');
  172. }
  173. function msg(msg) {
  174. cmd.message(msg);
  175. }
  176. function jsonRequire(data) {
  177. delete require.cache[require.resolve(data)];
  178. return require(data);
  179. }
  180. function jsonSort(data, sortby){
  181. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  182. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  183. return s2a;
  184. }
  185. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  186. }