index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const Command = require('command');
  2. module.exports = function boxOpener(dispatch){
  3. const command = Command(dispatch)
  4. let hooks = [],
  5. enabled = false,
  6. boxEvent = null,
  7. gacha_detected = false,
  8. isLooting = false,
  9. location = null,
  10. timer = null,
  11. delay = 6000,
  12. useDelay = false,
  13. statOpened = 0,
  14. statStarted = null,
  15. scanning = false;
  16. boxId = 166901, // MWA box as default.
  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 = 6000;
  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', 4, event =>{location = event});
  49. function load()
  50. {
  51. hook('S_INVEN', 12, event =>{
  52. if(!enabled) return
  53. isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM
  54. if(event.first) inventory = []
  55. else if(!inventory) return
  56. for(let item of event.items) inventory.push(item)
  57. if(!event.more)
  58. {
  59. let box = false
  60. for(let item of inventory)
  61. {
  62. if(item.slot < 40) continue
  63. if(item.id == boxId)
  64. {
  65. box = true
  66. }
  67. }
  68. if(!box)
  69. {
  70. command.message("You ran out of boxes, stopping");
  71. stop();
  72. }
  73. inventory.splice(0,inventory.length)
  74. inventory = [];
  75. inventory = null
  76. }
  77. });
  78. hook('C_USE_ITEM', 3, event =>{
  79. if(!scanning) return
  80. if(scanning){
  81. boxEvent = event;
  82. boxId = event.id;
  83. command.message("Box set to: "+boxId+", proceeding to auto-open it with " + (useDelay ? "a minimum " + (delay / 1000) + " sec delay" : "no delay" ));
  84. scanning = false;
  85. let d = new Date();
  86. statStarted = d.getTime();
  87. enabled = true;
  88. timer = setTimeout(openBox,delay);
  89. }
  90. });
  91. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  92. if(!gacha_detected && !isLooting)
  93. {
  94. isLooting = true;
  95. statOpened++;
  96. if(!useDelay)
  97. {
  98. clearTimeout(timer);
  99. openBox();
  100. }
  101. }
  102. });
  103. hook('S_GACHA_END', 1, event => {
  104. statOpened++;
  105. if(!useDelay)
  106. {
  107. clearTimeout(timer);
  108. openBox();
  109. }
  110. });
  111. hook('S_SYSTEM_MESSAGE', 1, event => {
  112. const msg = dispatch.base.parseSystemMessage(event.message);
  113. if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
  114. {
  115. command.message("Box can not be opened anymore, stopping");
  116. stop();
  117. }
  118. });
  119. hook('S_GACHA_START', 1, event => {
  120. gacha_detected = true;
  121. dispatch.toServer('C_GACHA_TRY', 1,{
  122. id:event.id
  123. })
  124. });
  125. }
  126. function openBox()
  127. {
  128. boxEvent.loc = location.loc;
  129. boxEvent.w = location.w;
  130. dispatch.toServer('C_USE_ITEM', 3, boxEvent);
  131. timer = setTimeout(openBox,delay);
  132. }
  133. function addZero(i)
  134. {
  135. if (i < 10) {
  136. i = "0" + i;
  137. }
  138. return i;
  139. }
  140. function stop()
  141. {
  142. unload();
  143. if(scanning)
  144. {
  145. scanning = false;
  146. command.message('Scanning for a box is aborted');
  147. }
  148. else
  149. {
  150. clearTimeout(timer);
  151. enabled = false;
  152. gacha_detected = false;
  153. let d = new Date();
  154. let t = d.getTime();
  155. let timeElapsedMSec = t-statStarted;
  156. d = new Date(1970, 0, 1); // Epoch
  157. d.setMilliseconds(timeElapsedMSec);
  158. let h = addZero(d.getHours());
  159. let m = addZero(d.getMinutes());
  160. let s = addZero(d.getSeconds());
  161. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  162. statOpened = 0;
  163. }
  164. }
  165. function unload() {
  166. if(hooks.length) {
  167. for(let h of hooks) dispatch.unhook(h)
  168. hooks = []
  169. }
  170. }
  171. function hook() {
  172. hooks.push(dispatch.hook(...arguments))
  173. }
  174. }