index.js 5.3 KB

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