index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "load":
  13. case "reload":
  14. switch(arg2) {
  15. case 'hp': hpPot = getHP(); msg(`HP.json has been reloademod.`); break;
  16. case 'mp': mpPot = getMP(); msg(`MP.json has been reloademod.`); break;
  17. case 'config': config = getConfig(); msg(`Config.json has been reloademod.`); break;
  18. }
  19. break;
  20. case "notice":
  21. config.notice = !config.notice;
  22. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  23. break;
  24. case "slay":
  25. case "slaying":
  26. isSlaying = !isSlaying;
  27. if (!config.hp) config.hp = true;
  28. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}.`);
  29. break;
  30. case "hp":
  31. config.hp = !config.hp;
  32. if (isSlaying) isSlaying = false;
  33. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}.`);
  34. break;
  35. case "mp":
  36. case "mana":
  37. config.mp = !config.mp;
  38. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  39. break;
  40. default:
  41. msg(`Wrong command :v`);
  42. break;
  43. }
  44. });
  45. mod.hook('S_MOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) isMount = true;});
  46. mod.hook('S_UNMOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) {isMount = false; isCon = false;}});
  47. mod.hook('S_REQUEST_CONTRACT', 'raw', () => {isCon = true;});
  48. mod.hook('S_ACCEPT_CONTRACT', 'raw', () => {isCon = false;});
  49. mod.hook('S_REJECT_CONTRACT', 'raw', () => {isCon = false;});
  50. mod.hook('S_CANCEL_CONTRACT', 'raw', () => {isCon = false;});
  51. mod.hook('S_GACHA_END', 'raw', () => {isCon = false;});
  52. mod.hook('C_BIND_ITEM_EXECUTE', 'raw', () => {isCon = false;});
  53. mod.hook("S_LOGIN", 10, e => {({gameId} = e);});
  54. mod.hook("S_SPAWN_ME", 3, e => {isAlive = e.alive;});
  55. mod.hook("S_CREATURE_LIFE", 2, e => {if (isMe(e.gameId)) {isAlive = e.alive;}});
  56. mod.hook("S_MOUNT_VEHICLE_EX", 1, e => {if (e.target.equals(gameId)) VehicleEx = e.vehicle;});
  57. mod.hook("S_UNMOUNT_VEHICLE_EX", 1, e => {if (e.target.equals(gameId)) VehicleEx = null;});
  58. mod.hook('S_USER_STATUS', 1, e => {if (e.target.equals(gameId)) isCombat = (e.status === 1);});
  59. mod.hook('S_BATTLE_FIELD_ENTRANCE_INFO', 1, e => {zoneBG = e.zone;});
  60. mod.hook('S_LOAD_TOPO', 3, e => {isBG = (e.zone === zoneBG); VehicleEx = null; isCon = false; isMount = false;});
  61. mod.hook('S_INVEN', 16, e => {
  62. if (config.enabled) {
  63. let gHP = null, gMP = null;
  64. for(i = 0; i < hpPot.length; i++) {
  65. gHP = e.items.find(item => item.id === Number(hpPot[i][0]));
  66. if (gHP) hpPot[i][1].amount = gHP.amount;
  67. }
  68. for (i = 0; i < mpPot.length; i++) {
  69. gMP = e.items.find(item => item.id === Number(mpPot[i][0]));
  70. if (gMP) mpPot[i][1].amount = gMP.amount;
  71. }
  72. }
  73. });
  74. mod.hook('S_CREATURE_CHANGE_HP', 6, e => {
  75. if (config.enabled && config.hp) {
  76. if (e.target.equals(gameId)) {
  77. nowHP = Math.round(e.curHp / e.maxHp * 100);
  78. for (let hp = 0; hp < hpPot.length; hp++) {
  79. 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) {
  80. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  81. if (config.notice) msg(`Used ${hpPot[hp][1].name}, still have ${(hpPot[hp][1].amount)} left.`);
  82. }
  83. }
  84. }
  85. }
  86. });
  87. mod.hook('S_PLAYER_CHANGE_MP', 1, e => {
  88. if (config.enabled && config.mp) {
  89. if (e.target.equals(gameId)) {
  90. nowMP = Math.round(e.currentMp / e.maxMp * 100);
  91. for (let mp = 0; mp < mpPot.length; mp++) {
  92. if (!mpPot[mp][1].inCd && nowMP <= mpPot[mp][1].use_at && mpPot[mp][1].amount > 0 && isAlive && !isBG && !isCon && !isMount) {
  93. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  94. if (config.notice) msg(`Used ${mpPot[mp][1].name}, still have ${(mpPot[mp][1].amount)} left.`);
  95. }
  96. }
  97. }
  98. }
  99. });
  100. function useItem(itemId) {
  101. mod.send('C_USE_ITEM', 3, {
  102. gameId: gameId,
  103. id: Number(itemId[0]),
  104. amount: 1,
  105. unk4: true
  106. });
  107. }
  108. function getConfig() {
  109. let data = {};
  110. try {
  111. data = require('./config.json');
  112. } catch (e) {
  113. data = {
  114. enabled: true,
  115. hp: false,
  116. mp: true,
  117. notice: false
  118. }
  119. jsonSave('config.json', data);
  120. }
  121. return data;
  122. }
  123. function getHP() {
  124. let data = {};
  125. try {
  126. data = require('./hp.json');
  127. } catch (e) {
  128. data[6552] = {
  129. name: 'Prime Recovery Potable',
  130. use_at: 80,
  131. slay_at: 30,
  132. cd: 10
  133. }
  134. jsonSave('hp.json', data);
  135. }
  136. return jsonSort(data, 'use_at');
  137. }
  138. function getMP() {
  139. let data = {};
  140. try {
  141. data = require('./mp.json');
  142. } catch (e) {
  143. data[6562] = {
  144. name: 'Prime Replenishment Potable',
  145. use_at: 50,
  146. cd: 10
  147. }
  148. jsonSave('mp.json', data);
  149. }
  150. return jsonSort(data, 'use_at');
  151. }
  152. function jsonSort(data, sortby){
  153. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  154. let s2a = []; for(i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  155. return s2a;
  156. }
  157. function msg(msg) {cmd.message(msg);}
  158. function isMe(id) {return gameId.equals(id) || VehicleEx && VehicleEx.equals(id);}
  159. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  160. }