unitState.js 5.8 KB

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