index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 'check':
  39. let data = '===== HP Potion =====\n';
  40. for (let hp = 0; hp < hpPot.length; hp++)
  41. if (hpPot[hp][1].amount > 0)
  42. data += `[${hp}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount}\n`;
  43. data += '===== MP Potion =====\n';
  44. for (let mp = 0; mp < mpPot.length; mp++)
  45. if (mpPot[mp][1].amount > 0)
  46. data += `[${mp}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount}\n`;
  47. data += '===== Status =====\n';
  48. data += 'inGame: ${(mod.game.isIngame)}\n';
  49. data += 'inLoading: ${(mod.game.isInLoadingScreen)}\n';
  50. data += 'isAlive: ${(mod.game.me.alive)}\n';
  51. data += 'onMount: ${(mod.game.me.mounted)}\n';
  52. data += 'inCombat: ${(mod.game.me.inCombat)}\n';
  53. data += 'inContract: ${(mod.game.me.active)}\n';
  54. data += 'inBattleground: ${(mod.game.me.inBattleground)}\n';
  55. data += 'inCivilUnrest: ${(mod.game.me.zone === 152)}';
  56. msg(data);
  57. data = '';
  58. break;
  59. default:
  60. msg(`Wrong command :v`);
  61. break;
  62. }
  63. });
  64. mod.hook('S_INVEN', 17, e => {
  65. if (!inUpdate) {
  66. inUpdate = true;
  67. for(let i = 0; i < hpPot.length; i++) {
  68. gPot = e.items.find(item => item.id === Number(hpPot[i][0]));
  69. if (gPot) hpPot[i][1].amount = gPot.amount;
  70. }
  71. for(let i = 0; i < mpPot.length; i++) {
  72. gPot = e.items.find(item => item.id === Number(mpPot[i][0]));
  73. if (gPot) mpPot[i][1].amount = gPot.amount;
  74. }
  75. inUpdate = false;
  76. }
  77. });
  78. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  79. if (config.enabled) {
  80. isReady = mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active;
  81. if (config.hp && isReady) {
  82. nowHP = Math.round(parseInt(e.hp) / parseInt(e.maxHp) * 100);
  83. for (let hp = 0; hp < hpPot.length; hp++) {
  84. 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)) {
  85. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  86. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount)} left.`);
  87. }
  88. }
  89. }
  90. if (config.mp && isReady) {
  91. nowMP = Math.round(parseInt(e.mp) / parseInt(e.maxMp) * 100);
  92. for (let mp = 0; mp < mpPot.length; mp++) {
  93. 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)) {
  94. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  95. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount)} left.`);
  96. }
  97. }
  98. }
  99. }
  100. });
  101. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  102. inUpdate = false;
  103. for (let hp = 0; hp < hpPot.length; hp++)
  104. hpPot[hp][1].amount = 0;
  105. for (let mp = 0; mp < mpPot.length; mp++)
  106. mpPot[mp][1].amount = 0;
  107. });
  108. function useItem(itemId) {
  109. mod.send('C_USE_ITEM', 3, {
  110. gameId: mod.game.me.gameId,
  111. id: Number(itemId[0]),
  112. amount: 1,
  113. unk4: true
  114. });
  115. }
  116. function getConfig() {
  117. let data = {};
  118. try {
  119. data = require('./config.json');
  120. } catch (e) {
  121. data = {
  122. enabled: true,
  123. hp: false,
  124. mp: true,
  125. notice: false
  126. }
  127. jsonSave('config.json', data);
  128. }
  129. return data;
  130. }
  131. function getHP() {
  132. let data = {};
  133. try {
  134. data = require('./hp.json');
  135. } catch (e) {
  136. data[6552] = {
  137. name: 'Prime Recovery Potable',
  138. inBattleground: false,
  139. inCombat: true,
  140. use_at: 80,
  141. slay_at: 30,
  142. cd: 10
  143. }
  144. jsonSave('hp.json', data);
  145. }
  146. return jsonSort(data, 'use_at');
  147. }
  148. function getMP() {
  149. let data = {};
  150. try {
  151. data = require('./mp.json');
  152. } catch (e) {
  153. data[6562] = {
  154. name: 'Prime Replenishment Potable',
  155. inBattleground: false,
  156. inCombat: false,
  157. use_at: 50,
  158. cd: 10
  159. }
  160. jsonSave('mp.json', data);
  161. }
  162. return jsonSort(data, 'use_at');
  163. }
  164. function msg(msg) {
  165. cmd.message(msg);
  166. }
  167. function jsonSort(data, sortby){
  168. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  169. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  170. return s2a;
  171. }
  172. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  173. }