unitTracker.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 userMounted = (type, event) => {
  74. if (this.users.has(event.gameId)) {
  75. let data = undefined;
  76. switch (type) {
  77. case (ENTITY_HOOK_TYPE.DESPAWN):
  78. data = this.users.get(event.gameId);
  79. data.mount = 0;
  80. this.users.set(event.gameId, data);
  81. break;
  82. case (ENTITY_HOOK_TYPE.SPAWN):
  83. data = this.users.get(event.gameId);
  84. data.mount = event.id;
  85. this.users.set(event.gameId, data);
  86. break;
  87. }
  88. }
  89. };
  90. const updateAngle = (data, w) => data.loc.w = w;
  91. const updateLoc = (data, loc) => data.loc = loc;
  92. const updateAngleHandler = (event) => {
  93. let gameId = event.gameId;
  94. if (this.users.has(gameId)) this.users.set(gameId, updateAngle(this.users.get(gameId), event.w));
  95. else if (this.npcs.has(gameId)) this.npcs.set(gameId, updateAngle(this.npcs.get(gameId), event.w));
  96. else if (this.summons.has(gameId)) this.summons.set(gameId, updateAngle(this.summons.get(gameId), event.w));
  97. };
  98. const userLocationHandler = (event) => {
  99. if (this.users.has(event.gameId)) {
  100. this.users.set(event.gameId, updateLoc(this.users.get(event.gameId), event.loc));
  101. }
  102. };
  103. const npcLocationHandler = (event) => {
  104. if (this.npcs.has(event.gameId)) {
  105. this.npcs.set(event.gameId, updateLoc(this.npcs.get(event.gameId), event.loc));
  106. }
  107. else if (this.summons.has(event.gameId)) {
  108. this.summons.set(event.gameId, updateLoc(this.summons.get(event.gameId), event.loc));
  109. }
  110. else if (this.servants.has(event.gameId)) {
  111. this.servants.set(event.gameId, updateLoc(this.servants.get(event.gameId), event.loc));
  112. }
  113. };
  114. const creatureLifeHandler = (event) => {
  115. if (this.users.has(event.gameId)) {
  116. let data = this.users.get(event.gameId);
  117. data.alive = event.alive;
  118. this.users.set(event.gameId, data);
  119. }
  120. };
  121. const cleanup = () => {
  122. console.log("DEBUG: unitTracker -> cleanup");
  123. this.npcs.clear();
  124. this.summons.clear();
  125. this.users.clear();
  126. this.servants.clear();
  127. this.collections.clear();
  128. this.drop.clear();
  129. };
  130. mod.hook(...proto.getData("S_CREATURE_ROTATE"), HOOK_SETTINGS.FIRST, updateAngleHandler);
  131. mod.hook(...proto.getData("S_CREATURE_LIFE"), HOOK_SETTINGS.FIRST, creatureLifeHandler);
  132. mod.hook(...proto.getData("S_NPC_LOCATION"), HOOK_SETTINGS.FIRST, npcLocationHandler);
  133. mod.hook(...proto.getData("S_USER_LOCATION"), HOOK_SETTINGS.FIRST, userLocationHandler);
  134. mod.hook(...proto.getData("S_USER_LOCATION_IN_ACTION"), HOOK_SETTINGS.FIRST, userLocationHandler);
  135. mod.hook(...proto.getData("S_MOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  136. mod.hook(...proto.getData("S_UNMOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  137. mod.hook(...proto.getData("S_SPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  138. mod.hook(...proto.getData("S_DESPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  139. mod.hook(...proto.getData("S_SPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  140. mod.hook(...proto.getData("S_DESPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  141. mod.hook(...proto.getData("S_REQUEST_SPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  142. mod.hook(...proto.getData("S_REQUEST_DESPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  143. mod.hook(...proto.getData("S_SPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  144. mod.hook(...proto.getData("S_DESPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  145. mod.hook(...proto.getData("S_SPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  146. mod.hook(...proto.getData("S_DESPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  147. mod.hook("S_LOAD_TOPO", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, cleanup);
  148. }
  149. }
  150. module.exports = unitTracker;