index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 gameId = null, VehicleEx = null, getInv = false;
  6. let isAlive = false, isCombat = false, isBG = false, isSlaying = false;
  7. let zoneBG = 0, nowHP = 0, nowMP = 0, isCon = false, isMount = false;
  8. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  9. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  10. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  11. switch (arg1) {
  12. case 'id':
  13. case 'getid':
  14. case 'itemid':
  15. let id = arg2.match(/#(\d*)@/);
  16. id = id ? Number(id[1]) : 0;
  17. msg(`itemId: ${id}.`);
  18. break;
  19. case 'load':
  20. case 'reload':
  21. switch(arg2) {
  22. case 'hp': hpPot = getHP(); msg(`HP.json has been reloaded.`); break;
  23. case 'mp': mpPot = getMP(); msg(`MP.json has been reloaded.`); break;
  24. case 'config': config = getConfig(); msg(`Config.json has been reloaded.`); break;
  25. }
  26. break;
  27. case 'notice':
  28. config.notice = !config.notice;
  29. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  30. break;
  31. case 'slay':
  32. case 'slaying':
  33. isSlaying = !isSlaying;
  34. if (!config.hp) config.hp = true;
  35. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}.`);
  36. break;
  37. case 'hp':
  38. config.hp = !config.hp;
  39. if (isSlaying) isSlaying = false;
  40. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}.`);
  41. break;
  42. case 'mp':
  43. case 'mana':
  44. config.mp = !config.mp;
  45. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  46. break;
  47. default:
  48. msg(`Wrong command :v`);
  49. break;
  50. }
  51. });
  52. mod.hook('S_MOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) isMount = true;});
  53. mod.hook('S_UNMOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) {isMount = false; isCon = false;}});
  54. mod.hook('S_REQUEST_CONTRACT', 'raw', () => {isCon = true;});
  55. mod.hook('S_ACCEPT_CONTRACT', 'raw', () => {isCon = false;});
  56. mod.hook('S_REJECT_CONTRACT', 'raw', () => {isCon = false;});
  57. mod.hook('S_CANCEL_CONTRACT', 'raw', () => {isCon = false;});
  58. mod.hook('S_GACHA_END', 'raw', () => {isCon = false;});
  59. mod.hook('C_BIND_ITEM_EXECUTE', 'raw', () => {isCon = false;});
  60. mod.hook('S_LOGIN', 10, e => {({gameId} = e);});
  61. mod.hook('S_SPAWN_ME', 3, e => {isAlive = e.alive;});
  62. mod.hook('S_CREATURE_LIFE', 2, e => {if (isMe(e.gameId)) {isAlive = e.alive;}});
  63. mod.hook('S_MOUNT_VEHICLE_EX', 1, e => {if (e.target.equals(gameId)) VehicleEx = e.vehicle;});
  64. mod.hook('S_UNMOUNT_VEHICLE_EX', 1, e => {if (e.target.equals(gameId)) VehicleEx = null;});
  65. mod.hook('S_USER_STATUS', 1, e => {if (e.target.equals(gameId)) isCombat = (e.status === 1);});
  66. mod.hook('S_BATTLE_FIELD_ENTRANCE_INFO', 1, e => {zoneBG = e.zone;});
  67. mod.hook('S_LOAD_TOPO', 3, e => {isBG = (e.zone === zoneBG); VehicleEx = null; isCon = false; isMount = false;});
  68. mod.hook('S_INVEN', 16, e => {
  69. if (config.enabled) {
  70. let gHP = null, gMP = null;
  71. for(i = 0; i < hpPot.length; i++) {
  72. gHP = e.items.find(item => item.id === Number(hpPot[i][0]));
  73. if (gHP) hpPot[i][1].amount = gHP.amount;
  74. }
  75. for (i = 0; i < mpPot.length; i++) {
  76. gMP = e.items.find(item => item.id === Number(mpPot[i][0]));
  77. if (gMP) mpPot[i][1].amount = gMP.amount;
  78. }
  79. }
  80. });
  81. mod.hook('S_CREATURE_CHANGE_HP', 6, e => {
  82. if (config.enabled && config.hp) {
  83. if (e.target.equals(gameId)) {
  84. nowHP = Math.round(e.curHp / e.maxHp * 100);
  85. for (let hp = 0; hp < hpPot.length; hp++) {
  86. if (!hpPot[hp][1].inCd && ((!isSlaying && nowHP <= hpPot[hp][1].use_at) || (isSlaying && nowHP <= hpPot[hp][1].slaying)) && hpPot[hp][1].amount > 0 && isCombat && isAlive && !isBG && !isCon && !isMount) {
  87. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  88. if (config.notice) msg(`Used ${hpPot[hp][1].name}, still have ${(hpPot[hp][1].amount)} left.`);
  89. }
  90. }
  91. }
  92. }
  93. });
  94. mod.hook('S_PLAYER_CHANGE_MP', 1, e => {
  95. if (config.enabled && config.mp) {
  96. if (e.target.equals(gameId)) {
  97. nowMP = Math.round(e.currentMp / e.maxMp * 100);
  98. for (let mp = 0; mp < mpPot.length; mp++) {
  99. if (!mpPot[mp][1].inCd && nowMP <= mpPot[mp][1].use_at && mpPot[mp][1].amount > 0 && isAlive && !isBG && !isCon && !isMount) {
  100. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  101. if (config.notice) msg(`Used ${mpPot[mp][1].name}, still have ${(mpPot[mp][1].amount)} left.`);
  102. }
  103. }
  104. }
  105. }
  106. });
  107. function useItem(itemId) {
  108. mod.send('C_USE_ITEM', 3, {
  109. gameId: 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. use_at: 80,
  138. slay_at: 30,
  139. cd: 10
  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. 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(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 isMe(id) {return gameId.equals(id) || VehicleEx && VehicleEx.equals(id);}
  166. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  167. }