index.js 6.2 KB

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