unitTracker.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 unitState {
  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. // portals "shapeId": 652992,
  21. // ninja log "shapeId": 303030
  22. // sentinel "shapeId": 730500
  23. Object.assign(this, {
  24. "collections": new Map(),
  25. "drop": new Map(),
  26. "servants": new Map(),
  27. "users": new Map(),
  28. "npcs": new Map(),
  29. "summons": new Map()
  30. });
  31. const dropHandler = (type, event) => {
  32. switch (type) {
  33. case (ENTITY_HOOK_TYPE.DESPAWN): this.drop.delete(event.gameId); break;
  34. case (ENTITY_HOOK_TYPE.SPAWN): this.drop.set(event.gameId, event); break;
  35. }
  36. };
  37. const collectionHandler = (type, event) => {
  38. switch (type) {
  39. case (ENTITY_HOOK_TYPE.DESPAWN): this.collections.delete(event.gameId); break;
  40. case (ENTITY_HOOK_TYPE.SPAWN): this.collections.set(event.gameId, event); break;
  41. }
  42. };
  43. const servantsHandler = (type, event) => {
  44. switch (type) {
  45. case (ENTITY_HOOK_TYPE.DESPAWN): this.servants.delete(event.gameId); break;
  46. case (ENTITY_HOOK_TYPE.SPAWN): this.servants.set(event.gameId, event); break;
  47. }
  48. };
  49. const userHandler = (type, event) => {
  50. switch (type) {
  51. case (ENTITY_HOOK_TYPE.DESPAWN): this.users.delete(event.gameId); break;
  52. case (ENTITY_HOOK_TYPE.SPAWN): this.users.set(event.gameId, event); break;
  53. }
  54. };
  55. const npcHandler = (type, event) => {
  56. switch (type) {
  57. case (ENTITY_HOOK_TYPE.DESPAWN):
  58. this.npcs.delete(event.gameId);
  59. this.summons.delete(event.gameId);
  60. break;
  61. case (ENTITY_HOOK_TYPE.SPAWN):
  62. // eslint-disable-next-line no-magic-numbers
  63. if (event.huntingZoneId === SPECIAL_ZONES.HELPERS && this.users.has(event.owner)) {
  64. this.summons.set(event.gameId, event);
  65. }
  66. else {
  67. this.npcs.set(event.gameId, event);
  68. }
  69. break;
  70. }
  71. };
  72. const updateAngle = (data, w) => data.loc.w = w;
  73. const updateAngleHandler = (event) => {
  74. let gameId = event.gameId;
  75. if (this.users.has(gameId)) this.users.set(gameId, updateAngle(this.users.get(gameId), event.w));
  76. else if (this.npcs.has(gameId)) this.npcs.set(gameId, updateAngle(this.npcs.get(gameId), event.w));
  77. else if (this.summons.has(gameId)) this.summons.set(gameId, updateAngle(this.summons.get(gameId), event.w));
  78. };
  79. const userLocationHandler = (event) => {
  80. if (this.users.has(event.gameId)) {
  81. let data = this.users.get(event.gameId);
  82. data.loc = event.loc;
  83. this.users.set(event.gameId, data);
  84. }
  85. };
  86. const npcLocationHandler = (event) => {
  87. if (this.npcs.has(event.gameId)) {
  88. let data = this.npcs.get(event.gameId);
  89. data.loc = event.loc;
  90. this.npcs.set(event.gameId, data);
  91. }
  92. else if (this.summons.has(event.gameId)) {
  93. let data = this.summons.get(event.gameId);
  94. data.loc = event.loc;
  95. this.summons.set(event.gameId, data);
  96. }
  97. };
  98. const creatureLifeHandler = (event) => {
  99. if (this.users.has(event.gameId)) {
  100. let data = this.users.get(event.gameId);
  101. data.alive = event.alive;
  102. this.users.set(event.gameId, data);
  103. }
  104. };
  105. const cleanup = () => {
  106. this.npcs.clear();
  107. this.summons.clear();
  108. this.users.clear();
  109. this.servants.clear();
  110. this.collections.clear();
  111. this.drop.clear();
  112. };
  113. mod.hook(...proto.getData("S_CREATURE_ROTATE"), HOOK_SETTINGS.FIRST, updateAngleHandler);
  114. mod.hook(...proto.getData("S_CREATURE_LIFE"), HOOK_SETTINGS.FIRST, creatureLifeHandler);
  115. mod.hook(...proto.getData("S_NPC_LOCATION"), HOOK_SETTINGS.FIRST, npcLocationHandler);
  116. mod.hook(...proto.getData("S_USER_LOCATION"), HOOK_SETTINGS.FIRST, userLocationHandler);
  117. mod.hook(...proto.getData("S_USER_LOCATION_IN_ACTION"), HOOK_SETTINGS.LAST, userLocationHandler);
  118. //mod.hook("S_ACTION_STAGE", HOOK_SETTINGS.FIRST, locFromAction);
  119. //mod.hook("S_ACTION_END", HOOK_SETTINGS.FIRST, locFromAction);
  120. //mod.hook(...def.getVersion("S_EACH_SKILL_RESULT"), HOOK_SETTINGS.FIRST, eachResultUpdate);
  121. mod.hook(...proto.getData("S_SPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  122. mod.hook(...proto.getData("S_DESPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  123. mod.hook(...proto.getData("S_SPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  124. mod.hook(...proto.getData("S_DESPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  125. mod.hook(...proto.getData("S_REQUEST_SPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  126. mod.hook(...proto.getData("S_REQUEST_DESPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  127. mod.hook(...proto.getData("S_SPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  128. mod.hook(...proto.getData("S_DESPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  129. mod.hook(...proto.getData("S_SPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  130. mod.hook(...proto.getData("S_DESPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  131. mod.hook("S_LOAD_TOPO", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, cleanup);
  132. }
  133. }
  134. module.exports = unitState;