unitTracker.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. /*
  3. * Unit for Fps Manager. Tracking original data from server
  4. * Author: SaltyMonkey
  5. * Contributors:
  6. */
  7. /**
  8. * @typedef {import("../fpsManager.js").deps} deps
  9. */
  10. const { ENTITY_HOOK_TYPE, HOOK_SETTINGS, RAW_HOOK_MODE, SPECIAL_ZONES } = require("./helpers/enums");
  11. class unitTracker {
  12. /**
  13. *Creates an instance of unitState.
  14. * @param {deps} deps dependencies
  15. * @memberof unitTracker
  16. */
  17. constructor(deps) {
  18. const mod = deps.mod;
  19. const proto = deps.proto;
  20. console.log("DEBUG: unitTracker -> constructor");
  21. // portals "shapeId": 652992,
  22. // ninja log "shapeId": 303030
  23. // sentinel "shapeId": 730500
  24. Object.assign(this, {
  25. "collections": new Map(),
  26. "drop": new Map(),
  27. "servants": new Map(),
  28. "users": new Map(),
  29. "npcs": new Map(),
  30. "summons": new Map()
  31. });
  32. const dropHandler = (type, event) => {
  33. switch (type) {
  34. case (ENTITY_HOOK_TYPE.DESPAWN): this.drop.delete(event.gameId); break;
  35. case (ENTITY_HOOK_TYPE.SPAWN): this.drop.set(event.gameId, event); break;
  36. }
  37. };
  38. const collectionHandler = (type, event) => {
  39. switch (type) {
  40. case (ENTITY_HOOK_TYPE.DESPAWN): this.collections.delete(event.gameId); break;
  41. case (ENTITY_HOOK_TYPE.SPAWN): this.collections.set(event.gameId, event); break;
  42. }
  43. };
  44. const servantsHandler = (type, event) => {
  45. switch (type) {
  46. case (ENTITY_HOOK_TYPE.DESPAWN): this.servants.delete(event.gameId); break;
  47. case (ENTITY_HOOK_TYPE.SPAWN): this.servants.set(event.gameId, event); break;
  48. }
  49. };
  50. const userHandler = (type, event) => {
  51. switch (type) {
  52. case (ENTITY_HOOK_TYPE.DESPAWN): this.users.delete(event.gameId); break;
  53. case (ENTITY_HOOK_TYPE.SPAWN): this.users.set(event.gameId, event); break;
  54. }
  55. };
  56. const npcHandler = (type, event) => {
  57. switch (type) {
  58. case (ENTITY_HOOK_TYPE.DESPAWN):
  59. this.npcs.delete(event.gameId);
  60. this.summons.delete(event.gameId);
  61. break;
  62. case (ENTITY_HOOK_TYPE.SPAWN):
  63. // eslint-disable-next-line no-magic-numbers
  64. if (event.huntingZoneId === SPECIAL_ZONES.HELPERS && this.users.has(event.owner)) {
  65. this.summons.set(event.gameId, event);
  66. }
  67. else {
  68. this.npcs.set(event.gameId, event);
  69. }
  70. break;
  71. }
  72. };
  73. const updateAngle = (data, w) => data.loc.w = w;
  74. const updateAngleHandler = (event) => {
  75. let gameId = event.gameId;
  76. if (this.users.has(gameId)) this.users.set(gameId, updateAngle(this.users.get(gameId), event.w));
  77. else if (this.npcs.has(gameId)) this.npcs.set(gameId, updateAngle(this.npcs.get(gameId), event.w));
  78. else if (this.summons.has(gameId)) this.summons.set(gameId, updateAngle(this.summons.get(gameId), event.w));
  79. };
  80. const userLocationHandler = (event) => {
  81. if (this.users.has(event.gameId)) {
  82. let data = this.users.get(event.gameId);
  83. data.loc = event.loc;
  84. this.users.set(event.gameId, data);
  85. }
  86. };
  87. const npcLocationHandler = (event) => {
  88. if (this.npcs.has(event.gameId)) {
  89. let data = this.npcs.get(event.gameId);
  90. data.loc = event.loc;
  91. this.npcs.set(event.gameId, data);
  92. }
  93. else if (this.summons.has(event.gameId)) {
  94. let data = this.summons.get(event.gameId);
  95. data.loc = event.loc;
  96. this.summons.set(event.gameId, data);
  97. }
  98. };
  99. const creatureLifeHandler = (event) => {
  100. if (this.users.has(event.gameId)) {
  101. let data = this.users.get(event.gameId);
  102. data.alive = event.alive;
  103. this.users.set(event.gameId, data);
  104. }
  105. };
  106. const cleanup = () => {
  107. console.log("DEBUG: unitTracker -> cleanup");
  108. this.npcs.clear();
  109. this.summons.clear();
  110. this.users.clear();
  111. this.servants.clear();
  112. this.collections.clear();
  113. this.drop.clear();
  114. };
  115. mod.hook(...proto.getData("S_CREATURE_ROTATE"), HOOK_SETTINGS.FIRST, updateAngleHandler);
  116. mod.hook(...proto.getData("S_CREATURE_LIFE"), HOOK_SETTINGS.FIRST, creatureLifeHandler);
  117. mod.hook(...proto.getData("S_NPC_LOCATION"), HOOK_SETTINGS.FIRST, npcLocationHandler);
  118. mod.hook(...proto.getData("S_USER_LOCATION"), HOOK_SETTINGS.FIRST, userLocationHandler);
  119. mod.hook(...proto.getData("S_USER_LOCATION_IN_ACTION"), HOOK_SETTINGS.LAST, userLocationHandler);
  120. //mod.hook("S_ACTION_STAGE", HOOK_SETTINGS.FIRST, locFromAction);
  121. //mod.hook("S_ACTION_END", HOOK_SETTINGS.FIRST, locFromAction);
  122. //mod.hook(...def.getVersion("S_EACH_SKILL_RESULT"), HOOK_SETTINGS.FIRST, eachResultUpdate);
  123. mod.hook(...proto.getData("S_SPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  124. mod.hook(...proto.getData("S_DESPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  125. mod.hook(...proto.getData("S_SPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  126. mod.hook(...proto.getData("S_DESPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  127. mod.hook(...proto.getData("S_REQUEST_SPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  128. mod.hook(...proto.getData("S_REQUEST_DESPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  129. mod.hook(...proto.getData("S_SPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  130. mod.hook(...proto.getData("S_DESPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  131. mod.hook(...proto.getData("S_SPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  132. mod.hook(...proto.getData("S_DESPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  133. mod.hook("S_LOAD_TOPO", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, cleanup);
  134. }
  135. }
  136. module.exports = unitTracker;