index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const Command = require('command');
  2. const sysmsg = require('tera-data-parser').sysmsg;
  3. module.exports = function boxOpener(dispatch){
  4. const command = Command(dispatch)
  5. let hooks = [],
  6. cid = null,
  7. enabled = false,
  8. gacha_detected = false,
  9. location = null,
  10. timer = null,
  11. delay = 5000,
  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 open the box you wish to set and it will auto-open all of it. Then use the same command to stop.');
  24. }
  25. else
  26. {
  27. stop();
  28. }
  29. });
  30. command.add('boxdelay', (arg) => {
  31. if(arg === "0")
  32. {
  33. useDelay = false;
  34. delay = 5000;
  35. command.message("turning OFF minimum box opening delay, enjoy the speed");
  36. }
  37. else
  38. {
  39. useDelay = true;
  40. delay = parseInt(arg);
  41. command.message("turning ON minimum box opening delay and setting it to " + (delay / 1000) + " sec");
  42. }
  43. });
  44. dispatch.hook('S_LOGIN', 1, event =>{cid = event.cid});
  45. dispatch.hook('C_PLAYER_LOCATION', 1, event =>{location = event});
  46. function load()
  47. {
  48. hook('S_INVEN', 5, event =>{
  49. if(!enabled) return
  50. isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM were received
  51. if(event.first) inventory = []
  52. else if(!inventory) return
  53. for(let item of event.items) inventory.push(item)
  54. if(!event.more)
  55. {
  56. let box = false
  57. for(let item of inventory)
  58. {
  59. if(item.slot < 40) continue
  60. if(item.item == boxId)
  61. {
  62. box = true
  63. }
  64. }
  65. if(!box)
  66. {
  67. stop();
  68. }
  69. inventory.splice(0,inventory.length)
  70. inventory = [];
  71. inventory = null
  72. }
  73. });
  74. hook('C_USE_ITEM', 1, event =>{
  75. if(!scanning) return
  76. if(scanning){
  77. boxId = event.item;
  78. command.message('Box set to: '+boxId+', proceeding to mass-open it with a minimum ' + (delay / 1000) + ' sec delay');
  79. scanning = false;
  80. let d = new Date();
  81. statStarted = d.getTime();
  82. enabled = true;
  83. load();
  84. openBox();
  85. }
  86. });
  87. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  88. if(!useDelay && !gacha_detected && !isLooting)
  89. {
  90. isLooting = true;
  91. statOpened++;
  92. clearTimeout(timer);
  93. openBox();
  94. }
  95. });
  96. hook('S_GACHA_END', 1, event => {
  97. if(!useDelay)
  98. {
  99. statOpened++;
  100. clearTimeout(timer);
  101. openBox();
  102. }
  103. });
  104. hook('S_SYSTEM_MESSAGE', 1, event => {
  105. if((event.message == "@" + sysmsg.maps.get(dispatch.base.protocolVersion).name.get('SMT_ITEM_MIX_NEED_METERIAL')) || (event.message == "@" + sysmsg.maps.get(dispatch.base.protocolVersion).name.get('SMT_CANT_CONVERT_NOW'))) // 1242 || 1776
  106. {
  107. stop();
  108. }
  109. });
  110. hook('S_GACHA_START', 1, event => {
  111. gacha_detected = true;
  112. dispatch.toServer('C_GACHA_TRY', 1,{
  113. id:event.id
  114. })
  115. });
  116. }
  117. function openBox()
  118. {
  119. dispatch.toServer('C_USE_ITEM', 1, {
  120. ownerId: cid,
  121. item: boxId,
  122. id: 0,
  123. unk1: 0,
  124. unk2: 0,
  125. unk3: 0,
  126. unk4: 1,
  127. unk5: 0,
  128. unk6: 0,
  129. unk7: 0,
  130. x: location.x1,
  131. y: location.y1,
  132. z: location.z1,
  133. w: location.w,
  134. unk8: 0,
  135. unk9: 0,
  136. unk10: 0,
  137. unk11: 1
  138. });
  139. timer = setTimeout(openBox,delay);
  140. if(useDelay)
  141. {
  142. statOpened++;
  143. }
  144. }
  145. function addZero(i)
  146. {
  147. if (i < 10) {
  148. i = "0" + i;
  149. }
  150. return i;
  151. }
  152. function stop()
  153. {
  154. unload();
  155. if(scanning)
  156. {
  157. scanning = false;
  158. command.message('Scanning aborted');
  159. }
  160. else
  161. {
  162. clearTimeout(timer);
  163. enabled = false;
  164. gacha_detected = false;
  165. let d = new Date();
  166. let t = d.getTime();
  167. let timeElapsedMSec = t-statStarted;
  168. d = new Date(1970, 0, 1); // Epoch
  169. d.setMilliseconds(timeElapsedMSec);
  170. let h = addZero(d.getHours());
  171. let m = addZero(d.getMinutes());
  172. let s = addZero(d.getSeconds());
  173. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  174. statOpened = 0;
  175. }
  176. }
  177. function unload() {
  178. if(hooks.length) {
  179. for(let h of hooks) dispatch.unhook(h)
  180. hooks = []
  181. }
  182. }
  183. function hook() {
  184. hooks.push(dispatch.hook(...arguments))
  185. }
  186. }