index.js 5.5 KB

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