unitState.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 equal = require("fast-deep-equal");
  12. const EventEmitter = require("events").EventEmitter;
  13. const rfdc = require("rfdc")({ "proto": true });
  14. const utils = require("./helpers/utils");
  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];
  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. 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. "settings": {}, //mod settings
  35. "presets": {}, // all loaded per class presets
  36. "preset": {}, //current chosen preset
  37. "activeMode": undefined,
  38. "prevMode": undefined,
  39. "availableClasses": ["common"],
  40. "userTemplateToClassMap": {},
  41. "lockedPacketsData": [],
  42. "throttlePacketsData": [],
  43. //lookup structures
  44. "huntingRewardsBuffs": new Set(),
  45. "shapeChangersBuffs": new Set(),
  46. "onScreenEffectsBuffs": new Set(),
  47. "smtsSet": 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. deps.mod.game.data.users.forEach(template => {
  59. this.availableClasses.push(template.class);
  60. this.userTemplateToClassMap[template] = template.class;
  61. });
  62. this.Init();
  63. }
  64. Init() {
  65. this.settings = utils.loadJson(utils.getFullPath(__dirname, "../settings/settings.json"));
  66. if (!this.settings) throw new Error("Settings file can't be loaded! Critical error. ");
  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.smtsSet = new Set(...staticData.blockedSmt);
  74. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  75. this.preset = this.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[this.preset.directives["default"]];
  80. });
  81. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), HOOK_SETTINGS.FIRST, (event) => {
  82. if (!this.settings["applyTrigger"] || equal(this.prevMode, this.activeMode)) return;
  83. this.prevMode = rfdc(this.activeMode);
  84. if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  85. this.activeMode = this.preset[this.preset.directives.triggerOW];
  86. }
  87. else if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  88. this.activeMode = this.preset[this.preset.directives.triggerBG];
  89. }
  90. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  91. this.activeMode = this.preset[this.preset.directives.triggerCU];
  92. }
  93. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  94. this.activeMode = this.preset[this.preset.directives.triggerDungeons[event.zone]] ||
  95. this.preset[this.preset.directives.triggerDungeons["all"]];
  96. }
  97. });
  98. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  99. if (!this.settings["applyTrigger"] || equal(this.prevMode, this.activeMode)) return;
  100. this.prevMode = rfdc(this.activeMode);
  101. this.activeMode = this.preset[this.preset.directives.triggerGuardians];
  102. this._eventTrigger();
  103. });
  104. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  105. if (!this.settings["applyTrigger"] || equal(this.prevMode, this.activeMode)) return;
  106. this.activeMode = rfdc(this.prevMode);
  107. this._eventTrigger();
  108. });
  109. }
  110. _eventTrigger() {
  111. this.emit(EVENTS.RECONFIGURE, this.prevMode, this.activeMode);
  112. }
  113. }
  114. module.exports = unitState;