index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* eslint-disable no-param-reassign */
  2. module.exports = function boxOpener(dispatch) {
  3. const command = dispatch.command || dispatch.require.command;
  4. let hooks = [],
  5. enabled = false,
  6. boxEvent = null,
  7. gacha_detected = false,
  8. gacha_contract = 0,
  9. isLooting = false,
  10. location = null,
  11. timer = null,
  12. delay = 5500,
  13. useDelay = false,
  14. statOpened = 0,
  15. statUsed = 0,
  16. statStarted = null,
  17. scanning = false;
  18. command.add("box", () => {
  19. if (!enabled && !scanning) {
  20. scanning = true;
  21. load();
  22. command.message("Please normally open a box now and the script will continue opening it");
  23. } else {
  24. stop();
  25. }
  26. });
  27. command.add("boxdelay", (arg) => {
  28. if (arg === "0") {
  29. useDelay = false;
  30. delay = 5500;
  31. command.message("Turning OFF minimum box opening delay, enjoy the speed");
  32. } else if (!isNaN(arg)) {
  33. useDelay = true;
  34. delay = parseInt(arg);
  35. command.message(`Minimum box opening delay is set to: ${ delay / 1000 } sec`);
  36. } else {
  37. command.message(`Minimum box opening delay is set to: ${ useDelay ? `${delay / 1000 } sec` : "no delay"}`);
  38. }
  39. });
  40. dispatch.hook("C_PLAYER_LOCATION", 5, event => {location = event;});
  41. dispatch.game.initialize("inventory");
  42. dispatch.game.inventory.on("update", () => {
  43. if (!enabled) return;
  44. isLooting = false;
  45. });
  46. function load() {
  47. hook("C_USE_ITEM", 3, event => {
  48. if (gacha_detected) return false;
  49. if (!scanning) return;
  50. if (scanning) {
  51. boxEvent = event;
  52. boxEvent.dbid = 0n; // to open all inv slots
  53. command.message(`Box set to: ${event.id}, proceeding to auto-open it with ${ useDelay ? `a minimum ${ delay / 1000 } sec delay` : "no delay"}`);
  54. scanning = false;
  55. const d = new Date();
  56. statStarted = d.getTime();
  57. enabled = true;
  58. timer = dispatch.setTimeout(openBox, delay);
  59. }
  60. });
  61. hook("S_SYSTEM_MESSAGE_LOOT_ITEM", "raw", () => {
  62. if (!gacha_detected && !isLooting && boxEvent) {
  63. isLooting = true;
  64. if (!useDelay) {
  65. dispatch.clearTimeout(timer);
  66. openBox();
  67. }
  68. }
  69. });
  70. hook("S_GACHA_END", "raw", () => {
  71. if (boxEvent && gacha_detected) {
  72. dispatch.clearTimeout(timer);
  73. if (useDelay) timer = dispatch.setTimeout(openBox, delay);
  74. else process.nextTick(openBox);
  75. }
  76. });
  77. hook("S_SYSTEM_MESSAGE", 1, event => {
  78. const msg = dispatch.parseSystemMessage(event.message).id;
  79. if (["SMT_ITEM_MIX_NEED_METERIAL", "SMT_CANT_CONVERT_NOW", "SMT_GACHA_NO_MORE_ITEM_SHORT", "SMT_NOTI_LEFT_LIMITED_GACHA_ITEM", "SMT_GACHA_CANCEL", "SMT_COMMON_NO_MORE_ITEM_TO_USE"].includes(msg)) {
  80. command.message("Box can not be opened anymore, stopping");
  81. stop();
  82. }
  83. });
  84. if (dispatch.majorPatchVersion >= 93) {
  85. hook("S_REQUEST_CONTRACT", dispatch.majorPatchVersion > 107 ? 2 : 1, event => {
  86. if (event.type !== 53) return;
  87. dispatch.hookOnce("S_GACHA_START", "raw", () => {
  88. gacha_detected = true;
  89. gacha_contract = event.id;
  90. openBox();
  91. return false;
  92. });
  93. return false;
  94. });
  95. hook("S_CANCEL_CONTRACT", 1, event => {
  96. if (!gacha_detected || event.type !== 53) return;
  97. gacha_contract = 0;
  98. stop();
  99. });
  100. } else {
  101. hook("S_GACHA_START", 1, event => {
  102. gacha_detected = true;
  103. dispatch.send("C_GACHA_TRY", 1, {
  104. "id": event.id
  105. });
  106. });
  107. }
  108. }
  109. function openBox() {
  110. if (dispatch.game.inventory.getTotalAmount(boxEvent.id) > 0) {
  111. if (dispatch.majorPatchVersion >= 93) {
  112. if (gacha_detected) {
  113. if (dispatch.majorPatchVersion >= 99) {
  114. dispatch.toServer("C_GACHA_TRY", 2, {
  115. id: gacha_contract,
  116. amount: 1
  117. });
  118. } else {
  119. dispatch.toServer("C_GACHA_TRY", 1, {
  120. id: gacha_contract
  121. });
  122. }
  123. } else {
  124. boxEvent.loc = location.loc;
  125. boxEvent.w = location.w;
  126. dispatch.toServer("C_USE_ITEM", 3, boxEvent);
  127. if (useDelay) {
  128. statUsed++; // counter for used items other than boxes
  129. }
  130. timer = dispatch.setTimeout(openBox, delay);
  131. }
  132. } else {
  133. boxEvent.loc = location.loc;
  134. boxEvent.w = location.w;
  135. dispatch.send("C_USE_ITEM", 3, boxEvent);
  136. if (useDelay) {
  137. statUsed++; // counter for used items other than boxes
  138. }
  139. }
  140. statOpened++;
  141. } else {
  142. command.message("You ran out of boxes, stopping");
  143. stop();
  144. }
  145. }
  146. function addZero(i) {
  147. if (i < 10) {
  148. i = `0${ i}`;
  149. }
  150. return i;
  151. }
  152. function stop() {
  153. unload();
  154. if (scanning) {
  155. scanning = false;
  156. command.message("Scanning for a box is aborted");
  157. } else {
  158. if (gacha_detected && gacha_contract) dispatch.toServer("C_GACHA_CANCEL", 1, { id: gacha_contract });
  159. if (useDelay && statOpened === 0) statOpened = statUsed;
  160. if (!gacha_detected) statOpened += 1; // Add the box we used at start if not gacha.
  161. dispatch.clearTimeout(timer);
  162. enabled = false;
  163. gacha_detected = false;
  164. gacha_contract = 0;
  165. boxEvent = null;
  166. let d = new Date();
  167. const t = d.getTime();
  168. const timeElapsedMSec = t - statStarted;
  169. d = new Date(1970, 0, 1); // Epoch
  170. d.setMilliseconds(timeElapsedMSec);
  171. const h = addZero(d.getHours());
  172. const m = addZero(d.getMinutes());
  173. const s = addZero(d.getSeconds());
  174. command.message(`Box opener stopped. Opened: ${ statOpened } boxes. Time elapsed: ${ h }:${ m }:${ s }. Per box: ${ ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) } sec`);
  175. statOpened = 0;
  176. statUsed = 0;
  177. }
  178. }
  179. function unload() {
  180. if (hooks.length) {
  181. for (const h of hooks) dispatch.unhook(h);
  182. hooks = [];
  183. }
  184. }
  185. function hook() {
  186. hooks.push(dispatch.hook(...arguments));
  187. }
  188. };