index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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)
  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. if(event.first) inventory = []
  51. else if(!inventory) return
  52. for(let item of event.items) inventory.push(item)
  53. if(!event.more)
  54. {
  55. let box = false
  56. for(let item of inventory)
  57. {
  58. if(item.slot < 40) continue
  59. if(item.item == boxId)
  60. {
  61. box = true
  62. }
  63. }
  64. if(!box)
  65. {
  66. stop();
  67. }
  68. inventory.splice(0,inventory.length)
  69. inventory = [];
  70. inventory = null
  71. }
  72. });
  73. hook('C_USE_ITEM', 1, event =>{
  74. if(!scanning) return
  75. if(scanning){
  76. boxId = event.item;
  77. command.message('Box set to: '+boxId+', proceeding to mass-open it with a minimum ' + (delay / 1000) + ' sec delay');
  78. scanning = false;
  79. let d = new Date();
  80. statStarted = d.getTime();
  81. enabled = true;
  82. load();
  83. openBox();
  84. }
  85. });
  86. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  87. if(!useDelay && !gacha_detected)
  88. {
  89. statOpened++;
  90. clearTimeout(timer);
  91. openBox();
  92. }
  93. });
  94. hook('S_GACHA_END', 1, event => {
  95. if(!useDelay)
  96. {
  97. statOpened++;
  98. clearTimeout(timer);
  99. openBox();
  100. }
  101. });
  102. hook('S_SYSTEM_MESSAGE', 1, event => {
  103. 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
  104. {
  105. stop();
  106. }
  107. });
  108. hook('S_GACHA_START', 1, event => {
  109. gacha_detected = true;
  110. dispatch.toServer('C_GACHA_TRY', 1,{
  111. id:event.id
  112. })
  113. });
  114. }
  115. function openBox()
  116. {
  117. dispatch.toServer('C_USE_ITEM', 1, {
  118. ownerId: cid,
  119. item: boxId,
  120. id: 0,
  121. unk1: 0,
  122. unk2: 0,
  123. unk3: 0,
  124. unk4: 1,
  125. unk5: 0,
  126. unk6: 0,
  127. unk7: 0,
  128. x: location.x1,
  129. y: location.y1,
  130. z: location.z1,
  131. w: location.w,
  132. unk8: 0,
  133. unk9: 0,
  134. unk10: 0,
  135. unk11: 1
  136. });
  137. timer = setTimeout(openBox,delay);
  138. if(useDelay)
  139. {
  140. statOpened++;
  141. }
  142. }
  143. function addZero(i)
  144. {
  145. if (i < 10) {
  146. i = "0" + i;
  147. }
  148. return i;
  149. }
  150. function stop()
  151. {
  152. unload();
  153. if(scanning)
  154. {
  155. scanning = false;
  156. command.message('Scanning aborted');
  157. }
  158. else
  159. {
  160. clearTimeout(timer);
  161. enabled = false;
  162. gacha_detected = false;
  163. let d = new Date();
  164. let t = d.getTime();
  165. let timeElapsedMSec = t-statStarted;
  166. d = new Date(1970, 0, 1); // Epoch
  167. d.setMilliseconds(timeElapsedMSec);
  168. let h = addZero(d.getHours());
  169. let m = addZero(d.getMinutes());
  170. let s = addZero(d.getSeconds());
  171. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  172. statOpened = 0;
  173. }
  174. }
  175. function unload() {
  176. if(hooks.length) {
  177. for(let h of hooks) dispatch.unhook(h)
  178. hooks = []
  179. }
  180. }
  181. function hook() {
  182. hooks.push(dispatch.hook(...arguments))
  183. }
  184. }