index.js 4.6 KB

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