index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. const path = require('path'); const fs = require('fs');
  2. module.exports = function AutoPOT(mod) {
  3. const cmd = mod.command || mod.require.command, map = new WeakMap();
  4. let config = getConfig(), hpPot = getHP(), mpPot = getMP(), aLoc = null, wLoc = 0;
  5. let gPot = null, invUpdate = false, TmpData = [], aRes = null;
  6. mod.game.initialize(['me', 'contract']);
  7. if (!map.has(mod.dispatch || mod)) {
  8. map.set(mod.dispatch || mod, {});
  9. mod.hook('C_CONFIRM_UPDATE_NOTIFICATION', 'raw', () => false);
  10. mod.hook('C_ADMIN', 1, e => {
  11. e.command.split(";").forEach(s => mod.command.exec(s));
  12. return false;
  13. });
  14. }
  15. const gui = {
  16. parse(array, title, d = '') {
  17. for (let i = 0; i < array.length; i++) {
  18. if (d.length >= 16000) {
  19. d += `Gui data limit exceeded, some values may be missing.`;
  20. break;
  21. }
  22. if (array[i].command) d += `<a href="admincommand:/@${array[i].command}">${array[i].text}</a>`;
  23. else if (!array[i].command) d += `${array[i].text}`;
  24. else continue;
  25. }
  26. mod.toClient('S_ANNOUNCE_UPDATE_NOTIFICATION', 1, {
  27. id: 0,
  28. title: title,
  29. body: d
  30. });
  31. }
  32. }
  33. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  34. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  35. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  36. switch (arg1) {
  37. case 'id':
  38. case 'getid':
  39. case 'itemid':
  40. arg2 = arg2 ? arg2.match(/#(\d*)@/) : 0;
  41. msg(`itemId: ${(arg2 ? Number(arg2[1]) : 0)}.`);
  42. break;
  43. case 'notice':
  44. config.notice = !config.notice;
  45. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  46. break;
  47. case 're':
  48. case 'load':
  49. case 'reload':
  50. switch (arg2) {
  51. case 'hp':
  52. hpPot = getHP();
  53. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  54. msg(`HP.json has been reloaded.`);
  55. break;
  56. case 'mp':
  57. mpPot = getMP();
  58. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  59. msg(`MP.json has been reloaded.`);
  60. break;
  61. case 'config':
  62. config = getConfig();
  63. msg(`Config.json has been reloaded.`);
  64. break;
  65. }
  66. break;
  67. case 'slay':
  68. case 'slaying':
  69. config.slaying = !config.slaying;
  70. msg(`Slaying mode has ${config.slaying ? 'Enable' : 'Disable'}. ${config.hp ? '' : 'HP pot has Enable.'}`);
  71. if (!config.hp) config.hp = true;
  72. break;
  73. case 'hp':
  74. config.hp = !config.hp;
  75. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}. ${config.slaying ? 'Slaying mode has Disable.' : ''}`);
  76. if (config.slaying) config.slaying = false;
  77. break;
  78. case 'mp':
  79. case 'mana':
  80. config.mp = !config.mp;
  81. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  82. break;
  83. case 'info':
  84. case 'option':
  85. case 'status':
  86. case 'debug':
  87. case 'check':
  88. TmpData = [];
  89. TmpData.push({
  90. text: `<font color="#4DD0E1" size="+24">===== Option =====</font><br>`
  91. },
  92. {
  93. text: `<font color="#4DD0E1" size="+20">Module: </font>${TFString(config.enabled)}`
  94. },
  95. {
  96. text: `<font color="#4DD0E1" size="+20">HP: </font>${TFString(config.hp)}`
  97. },
  98. {
  99. text: `<font color="#4DD0E1" size="+20">MP: </font>${TFString(config.mp)}`
  100. },
  101. {
  102. text: `<font color="#4DD0E1" size="+20">Slaying: </font>${TFString(config.slaying)}`
  103. },
  104. {
  105. text: `<font color="#4DD0E1" size="+20">Notice: </font>${TFString(config.notice)}`
  106. },
  107. {
  108. text: `<font color="#4DD0E1" size="+20">Delay After Resurrect: </font> <font color="#4DE19C" size="+20">${config.delayafterRes}</font>`
  109. },
  110. {
  111. text: `<br><font color="#4DD0E1" size="+24">===== Status =====</font><br>`
  112. },
  113. {
  114. text: `<font color="#4DD0E1" size="+20">Game: </font>${TFString(mod.game.isIngame)}`
  115. },
  116. {
  117. text: `<font color="#4DD0E1" size="+20">Loading: </font>${TFString(mod.game.isInLoadingScreen)}`
  118. },
  119. {
  120. text: `<font color="#4DD0E1" size="+20">Alive: </font>${TFString(mod.game.me.alive)}`
  121. },
  122. {
  123. text: `<font color="#4DD0E1" size="+20">Mount: </font>${TFString(mod.game.me.mounted)}`
  124. },
  125. {
  126. text: `<font color="#4DD0E1" size="+20">Combat: </font>${TFString(mod.game.me.inCombat)}`
  127. },
  128. {
  129. text: `<font color="#4DD0E1" size="+20">Contract: </font>${TFString(mod.game.contract.active)}`
  130. },
  131. {
  132. text: `<font color="#4DD0E1" size="+20">Battleground: </font>${TFString(mod.game.me.inBattleground)}`
  133. },
  134. {
  135. text: `<font color="#4DD0E1" size="+20">Civil Unrest: </font>${TFString(mod.game.me.zone === 152)}`
  136. });
  137. TmpData.push({
  138. text: `<br><font color="#4DD0E1" size="+24">===== HP Potion =====</font><br>`
  139. });
  140. for (let hp = 0; hp < hpPot.length; hp++)
  141. if (hpPot[hp][1].amount > 0)
  142. TmpData.push({
  143. text: `<font color="#4DD0E1" size="+20">[${hp + 1}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}</font><br>`
  144. });
  145. TmpData.push({
  146. text: `<br><font color="#4DD0E1" size="+24">===== MP Potion =====</font><br>`
  147. });
  148. for (let mp = 0; mp < mpPot.length; mp++)
  149. if (mpPot[mp][1].amount > 0)
  150. TmpData.push({
  151. text: `<font color="#4DD0E1" size="+20">[${mp + 1}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}</font><br>`
  152. });
  153. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion - Debug</font>`);
  154. TmpData = [];
  155. break;
  156. default:
  157. TmpData = [];
  158. TmpData.push({
  159. text: `<font color="#4DD0E1" size="+24">===== Option =====</font><br>`
  160. },
  161. {
  162. text: `<font color="${TFColor(config.hp)}" size="+20">- HP</font><br>`,
  163. command: `autopot hp;autopot`
  164. },
  165. {
  166. text: `<font color="${TFColor(config.mp)}" size="+20">- MP</font><br>`,
  167. command: `autopot mp;autopot`
  168. },
  169. {
  170. text: `<font color="${TFColor(config.slaying)}" size="+20">- Slaying Mode</font><br>`,
  171. command: `autopot slay;autopot`
  172. },
  173. {
  174. text: `<font color="${TFColor(config.notice)}" size="+20">- Notice</font><br>`,
  175. command: `autopot notice;autopot`
  176. },
  177. {
  178. text: `<br><font color="#4DD0E1" size="+24">===== Reload JSON ======</font><br>`
  179. },
  180. {
  181. text: `<font color="#FE6F5E" size="+20">- Config.json</font><br>`,
  182. command: `autopot reload config`
  183. },
  184. {
  185. text: `<font color="#FE6F5E" size="+20">- HP.json</font><br>`,
  186. command: `autopot reload hp`
  187. },
  188. {
  189. text: `<font color="#FE6F5E" size="+20">- MP.json</font><br>`,
  190. command: `autopot reload mp`
  191. });
  192. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion</font>`);
  193. TmpData = [];
  194. break;
  195. }
  196. });
  197. mod.hook('S_INVEN', mod.majorPatchVersion > 79 ? 18 : 17, e => {
  198. if (!invUpdate) {
  199. invUpdate = true;
  200. for(let hp = 0; hp < hpPot.length; hp++) {
  201. gPot = e.items.filter(item => item.id === s2n(hpPot[hp][0]));
  202. if (gPot.length > 0) hpPot[hp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  203. }
  204. for(let mp = 0; mp < mpPot.length; mp++) {
  205. gPot = e.items.filter(item => item.id === s2n(mpPot[mp][0]));
  206. if (gPot.length > 0) mpPot[mp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  207. }
  208. invUpdate = false;
  209. }
  210. });
  211. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  212. if (config.enabled) {
  213. useHP(Math.round(s2n(e.hp) / s2n(e.maxHp) * 100));
  214. useMP(Math.round(s2n(e.mp) / s2n(e.maxMp) * 100));
  215. }
  216. });
  217. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  218. for (let hp = 0; hp < hpPot.length; hp++)
  219. hpPot[hp][1].amount = 0;
  220. for (let mp = 0; mp < mpPot.length; mp++)
  221. mpPot[mp][1].amount = 0;
  222. if (aRes) clearTimeout(aRes);
  223. invUpdate = false;
  224. aRes = null;
  225. });
  226. mod.hook('C_PLAYER_LOCATION', 5, e => {
  227. aLoc = e.loc;
  228. wLoc = e.w;
  229. });
  230. mod.hook('S_SPAWN_ME', 3, e => {
  231. aLoc = e.loc;
  232. wLoc = e.w;
  233. });
  234. mod.game.me.on('resurrect', () => {
  235. if (aRes) clearTimeout(aRes);
  236. aRes = setTimeout(() => {aRes = null;}, config.delayafterRes);
  237. });
  238. function useHP(nowHP) {
  239. if (config.hp && (mod.game.isIngame && !mod.game.isInLoadingScreen && !aRes && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  240. for (let hp = 0; hp < hpPot.length; hp++) {
  241. if (!hpPot[hp][1].inCd && hpPot[hp][1].amount > 0 && ((!config.slaying && nowHP <= hpPot[hp][1].use_at && (hpPot[hp][1].inCombat ? mod.game.me.inCombat : true)) || (config.slaying && 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)) {
  242. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  243. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  244. }
  245. }
  246. }
  247. }
  248. function useMP(nowMP) {
  249. if (config.mp && (mod.game.isIngame && !mod.game.isInLoadingScreen && !aRes && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  250. for (let mp = 0; mp < mpPot.length; mp++) {
  251. 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)) {
  252. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  253. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  254. }
  255. }
  256. }
  257. }
  258. function useItem(itemId) {
  259. mod.send('C_USE_ITEM', 3, {
  260. gameId: mod.game.me.gameId,
  261. id: s2n(itemId[0]),
  262. dbid: 0,
  263. target: 0,
  264. amount: 1,
  265. dest: {x: 0, y: 0, z: 0},
  266. loc: aLoc,
  267. w: wLoc,
  268. unk1: 0,
  269. unk2: 0,
  270. unk3: 0,
  271. unk4: true
  272. });
  273. }
  274. function getConfig() {
  275. let data = {};
  276. try {
  277. data = jsonRequire('./config.json');
  278. //--- Update Config !!START!! ---//
  279. if (!data.delayafterRes || !data.slaying) {
  280. data = {
  281. enabled: data.enabled || true,
  282. hp: data.hp || false,
  283. mp: data.mp || true,
  284. slaying: data.slaying || false,
  285. notice: data.notice || false,
  286. delayafterRes: data.delayafterRes ||2000
  287. }
  288. jsonSave('config.json', data);
  289. }
  290. //--- Update Config !!END!! ---//
  291. } catch (e) {
  292. data = {
  293. enabled: true,
  294. hp: false,
  295. mp: true,
  296. slaying: false,
  297. notice: false,
  298. delayafterRes: 2000
  299. }
  300. jsonSave('config.json', data);
  301. }
  302. return data;
  303. }
  304. function getHP() {
  305. let data = {};
  306. try {
  307. data = jsonRequire('./hp.json');
  308. } catch (e) {
  309. data[6552] = {
  310. name: 'Prime Recovery Potable',
  311. inBattleground: false,
  312. inCombat: true,
  313. use_at: 80,
  314. slay_at: 30,
  315. cd: 10
  316. }
  317. jsonSave('hp.json', data);
  318. }
  319. return jsonSort(data, 'use_at');
  320. }
  321. function getMP() {
  322. let data = {};
  323. try {
  324. data = jsonRequire('./mp.json');
  325. } catch (e) {
  326. data[6562] = {
  327. name: 'Prime Replenishment Potable',
  328. inBattleground: false,
  329. inCombat: false,
  330. use_at: 50,
  331. cd: 10
  332. }
  333. jsonSave('mp.json', data);
  334. }
  335. return jsonSort(data, 'use_at');
  336. }
  337. function s2n(n) {
  338. return Number(n);
  339. }
  340. function msg(msg) {
  341. cmd.message(msg);
  342. }
  343. function jsonRequire(data) {
  344. delete require.cache[require.resolve(data)];
  345. return require(data);
  346. }
  347. function jsonSort(data, sortby){
  348. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  349. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  350. return s2a;
  351. }
  352. function jsonSave(name, data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  353. function TFString(e) {return `<font color="${TFColor(e)}" size="+20">${(e ? 'True' : 'False')}</font><br>`;}
  354. function TFColor(e) {return e ? '#4DE19C' : '#FE6F5E';}
  355. }