index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. location = null,
  9. timer = null,
  10. delay = 1500,
  11. statOpened = 0,
  12. statStarted = null,
  13. scanning = false;
  14. boxId = 166901, // MWA box as default.
  15. inventory = null;
  16. command.add('getbox', () => {
  17. scanning = true;
  18. load();
  19. command.message('Please open the box you wish to set.');
  20. });
  21. command.add('openbox', () => {
  22. if(!enabled)
  23. {
  24. var d = new Date();
  25. statStarted = d.getTime();
  26. enabled = true;
  27. load();
  28. command.message('Opening selected box, use the same command to stop.');
  29. openBox();
  30. }
  31. else
  32. {
  33. stop();
  34. }
  35. });
  36. command.add('stopbox', () => {
  37. stop();
  38. });
  39. command.add('boxdelay', (arg) => {
  40. delay = parseInt(arg);
  41. command.message("setting box opening delay to " + delay);
  42. });
  43. dispatch.hook('S_LOGIN', 1, event =>{cid = event.cid});
  44. dispatch.hook('C_PLAYER_LOCATION', 1, event =>{location = event});
  45. function load()
  46. {
  47. hook('S_INVEN', 5, event =>{
  48. if(!enabled) return
  49. if(event.first) inventory = []
  50. else if(!inventory) return
  51. for(let item of event.items) inventory.push(item)
  52. if(!event.more)
  53. {
  54. let box = false
  55. for(let item of inventory)
  56. {
  57. if(item.slot < 40) continue
  58. if(item.item == boxId)
  59. {
  60. box = true
  61. }
  62. }
  63. if(!box)
  64. {
  65. stop();
  66. }
  67. inventory.splice(0,inventory.length)
  68. inventory = [];
  69. inventory = null
  70. }
  71. });
  72. hook('C_USE_ITEM', 1, event =>{
  73. if(!scanning) return
  74. if(scanning){
  75. boxId = event.item;
  76. command.message('Box set to: '+boxId+'');
  77. scanning = false;
  78. unload();
  79. }
  80. });
  81. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  82. statOpened++;
  83. clearTimeout(timer);
  84. openBox();
  85. });
  86. hook('S_SYSTEM_MESSAGE', 1, event => {
  87. if(event.message == "@" + sysmsg.map.name['SMT_ITEM_MIX_NEED_METERIAL']) // SMT_ITEM_MIX_NEED_METERIAL // 1242
  88. {
  89. stop();
  90. }
  91. });
  92. }
  93. function openBox()
  94. {
  95. dispatch.toServer('C_USE_ITEM', 1, {
  96. ownerId: cid,
  97. item: boxId,
  98. id: 0,
  99. unk1: 0,
  100. unk2: 0,
  101. unk3: 0,
  102. unk4: 1,
  103. unk5: 0,
  104. unk6: 0,
  105. unk7: 0,
  106. x: location.x1,
  107. y: location.y1,
  108. z: location.z1,
  109. w: location.w,
  110. unk8: 0,
  111. unk9: 0,
  112. unk10: 0,
  113. unk11: 1
  114. });
  115. timer = setTimeout(openBox,delay);
  116. }
  117. function addZero(i)
  118. {
  119. if (i < 10) {
  120. i = "0" + i;
  121. }
  122. return i;
  123. }
  124. function stop()
  125. {
  126. clearTimeout(timer);
  127. unload();
  128. enabled = false;
  129. let d = new Date();
  130. let t = d.getTime();
  131. let timeElapsedMSec = t-statStarted;
  132. d = new Date(1970, 0, 1); // Epoch
  133. d.setMilliseconds(timeElapsedMSec);
  134. let h = addZero(d.getHours());
  135. let m = addZero(d.getMinutes());
  136. let s = addZero(d.getSeconds());
  137. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  138. statOpened = 0;
  139. }
  140. function unload() {
  141. if(hooks.length) {
  142. for(let h of hooks) dispatch.unhook(h)
  143. hooks = []
  144. }
  145. }
  146. function hook() {
  147. hooks.push(dispatch.hook(...arguments))
  148. }
  149. }