index.js 4.9 KB

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