index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if(!useDelay)
  113. {
  114. dispatch.clearTimeout(timer);
  115. process.nextTick(openBox);
  116. }
  117. }
  118. });
  119. hook('S_SYSTEM_MESSAGE', 1, event => {
  120. const msg = dispatch.parseSystemMessage(event.message).id;
  121. 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))
  122. {
  123. command.message("Box can not be opened anymore, stopping");
  124. stop();
  125. }
  126. });
  127. hook('S_REQUEST_CONTRACT', 1, event => {
  128. if(event.type !== 53) return;
  129. dispatch.hookOnce('S_GACHA_START', 'raw', () => {
  130. gacha_detected = true;
  131. gacha_contract = event.id;
  132. openBox();
  133. return false;
  134. })
  135. return false;
  136. });
  137. }
  138. function openBox()
  139. {
  140. if(dispatch.game.inventory.getTotalAmount(boxEvent.id) > 0)
  141. {
  142. if(gacha_detected) dispatch.toServer('C_GACHA_TRY', 1, { id: gacha_contract });
  143. else {
  144. boxEvent.loc = location.loc;
  145. boxEvent.w = location.w;
  146. dispatch.toServer('C_USE_ITEM', 3, boxEvent);
  147. if(useDelay)
  148. {
  149. statUsed++; // counter for used items other than boxes
  150. }
  151. timer = dispatch.setTimeout(openBox,delay);
  152. }
  153. statOpened++;
  154. }
  155. else
  156. {
  157. command.message("You ran out of boxes, stopping");
  158. stop();
  159. }
  160. }
  161. function addZero(i)
  162. {
  163. if (i < 10) {
  164. i = "0" + i;
  165. }
  166. return i;
  167. }
  168. function stop()
  169. {
  170. unload();
  171. if(scanning)
  172. {
  173. scanning = false;
  174. command.message('Scanning for a box is aborted');
  175. }
  176. else
  177. {
  178. if(gacha_detected) dispatch.toServer('C_GACHA_CANCEL', 1, { id: gacha_contract });
  179. if(useDelay && statOpened === 0) statOpened = statUsed;
  180. if(!gacha_detected) statOpened += 1; // Add the box we used at start if not gacha.
  181. dispatch.clearTimeout(timer);
  182. enabled = false;
  183. gacha_detected = false;
  184. gacha_contract = 0;
  185. boxEvent = null;
  186. let d = new Date();
  187. let t = d.getTime();
  188. let timeElapsedMSec = t-statStarted;
  189. d = new Date(1970, 0, 1); // Epoch
  190. d.setMilliseconds(timeElapsedMSec);
  191. let h = addZero(d.getHours());
  192. let m = addZero(d.getMinutes());
  193. let s = addZero(d.getSeconds());
  194. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  195. statOpened = 0;
  196. statUsed = 0;
  197. }
  198. }
  199. function unload() {
  200. if(hooks.length) {
  201. for(let h of hooks) dispatch.unhook(h)
  202. hooks = []
  203. }
  204. }
  205. function hook() {
  206. hooks.push(dispatch.hook(...arguments))
  207. }
  208. }