index.js 4.9 KB

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