unitState.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. /*
  3. * Unit for Fps Manager. Managing module state and events
  4. * Author: SaltyMonkey
  5. * Contributors:
  6. */
  7. /**
  8. * @typedef {import("../fpsManager.js").deps} deps
  9. */
  10. //using es5 compatible mode because json not supports es6 types and es6 version is slow ->(require("fast-deep-equal/es6");)
  11. const EventEmitter = require("events").EventEmitter;
  12. const rfdc = require("rfdc")({ "proto": true });
  13. const utils = require("./helpers/utils");
  14. //const watchdog = require("./helpers/watchdog");
  15. //import enums
  16. const { RAW_HOOK_MODE, HOOK_SETTINGS, ABN, EVENTS } = require("./helpers/enums");
  17. //types for abnormals to find in client data
  18. const shapeChangers = [ABN.BigHead, ABN.ShapeChanger];
  19. const onScreenEffects = [ABN.FX, ABN.PostProcessingEffect, ABN.CCB];
  20. class unitState extends EventEmitter {
  21. /**
  22. *Creates an instance of unitState.
  23. * @param {deps} deps dependencies
  24. * @memberof unitState
  25. */
  26. constructor(deps) {
  27. super();
  28. console.log("DEBUG: unitState -> constructor");
  29. this.__mod = deps.mod;
  30. this.__proto = deps.proto;
  31. this.__fps = deps.fps;
  32. Object.assign(this, {
  33. //main properties for state management
  34. "presets": {}, // all loaded per class presets
  35. "preset": {}, //current chosen preset
  36. "activeMode": undefined,
  37. "prevMode": undefined,
  38. "availableClasses": new Set(),
  39. "userTemplateToClassMap": {},
  40. "lockedPacketsData": [],
  41. "throttlePacketsData": [],
  42. //lookup structures
  43. "shapeChangersBuffs": new Set(),
  44. "onScreenEffectsBuffs": new Set(),
  45. "smtData": new Set()
  46. });
  47. deps.mod.game.data.abnormalities.forEach(abnormality => {
  48. let types = abnormality.effects.map(x => x.type);
  49. if (utils.arraysHasIntersect(types, shapeChangers))
  50. this.shapeChangersBuffs.add(abnormality.id);
  51. if (utils.arraysHasIntersect(types, onScreenEffects))
  52. this.onScreenEffectsBuffs.add(abnormality.id);
  53. });
  54. this.availableClasses.add("common");
  55. deps.mod.game.data.users.forEach(template => {
  56. this.availableClasses.add(template.class);
  57. this.userTemplateToClassMap[template.id] = template.class;
  58. });
  59. this.Init();
  60. }
  61. Init() {
  62. console.log("DEBUG: unitState -> Init");
  63. ([...this.availableClasses]).forEach(x => {
  64. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  65. });
  66. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  67. this.lockedPacketsData = staticData.blockedProto;
  68. this.throttlePacketsData = staticData.throttledProto;
  69. this.smtData = new Set(...staticData.blockedSmt);
  70. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  71. this.preset = this.__mod.settings.classBasedPresets
  72. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  73. : this.presets["common"];
  74. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  75. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  76. });
  77. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  78. if (!this.__mod.settings.enableEvents) return;
  79. this.prevMode = rfdc(this.activeMode);
  80. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  81. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  82. this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  83. }
  84. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  85. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  86. this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  87. }
  88. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  89. let mode = undefined;
  90. let name = undefined;
  91. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  92. name = this.preset.directives.triggerDungeons[event.zone];
  93. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  94. }
  95. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  96. name = this.preset.directives.triggerDungeons["all"];
  97. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  98. }
  99. if(mode) {
  100. this.activeMode = mode;
  101. this.__mod.command.message(`Mode was changed to ${name}`);
  102. }
  103. }
  104. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  105. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  106. this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  107. }
  108. this._eventTriggerPartial();
  109. });
  110. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  111. if (!this.__mod.settings.enableEvents) return;
  112. this.prevMode = rfdc(this.activeMode);
  113. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  114. this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  115. this._eventTrigger();
  116. });
  117. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  118. if (!this.__mod.settings.enableEvents) return;
  119. this.activeMode = rfdc(this.prevMode);
  120. this.__mod.command.message("Mode was changed back");
  121. this._eventTrigger();
  122. });
  123. }
  124. _eventTrigger() {
  125. console.log("DEBUG: unitState -> _eventTrigger");
  126. this.emit(EVENTS.RECONFIGURE);
  127. }
  128. _eventTriggerPartial() {
  129. console.log("DEBUG: unitState -> _eventTriggerPartial");
  130. this.emit(EVENTS.RECONFIGUREPARTIAL);
  131. }
  132. }
  133. module.exports = unitState;