unitState.js 5.8 KB

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