Browse Source

integrate logger, fpsUtilsCommandsPrefix settings, registerCommands

SaltyMonkey 6 years ago
parent
commit
8a02e71976
3 changed files with 42 additions and 29 deletions
  1. 6 0
      lib/helpers/utils.js
  2. 12 16
      lib/management/cmd.js
  3. 24 13
      lib/unitState.js

+ 6 - 0
lib/helpers/utils.js

@@ -99,3 +99,9 @@ exports.saveJson = (obj, filepath) => {
 * @returns {number} returns false if error happened
 */
 exports.getSkillBase = (skill) => Math.floor(skill / 10000);
+
+exports.safeRequire = (path) => {
+	// eslint-disable-next-line global-require
+	try { return require(path); }
+	catch (e) { return false; }
+};

+ 12 - 16
lib/management/cmd.js

@@ -19,46 +19,44 @@ class Commands {
 	 * @memberof Commands
 	*/
 	constructor(deps) {
-		let prefixes = ["fm", "fps"];
-
-		console.log("DEBUG: unitControl -> constructor");
 
 		const helpHandler = () => {
-			deps.mod.command.message("FPS Manager help.");
+			deps.log.send("FPS Manager help.");
 
 		};
 
 		const autoSettingHandler = () => {
 			deps.mod.settings.enableEvents = !deps.mod.settings.enableEvents;
-			deps.mod.command.message(`Triggers ${deps.mod.settings.enableEvents ? "a" : "dea"}ctivated.`);
+			deps.log.send(`Triggers ${deps.mod.settings.enableEvents ? "a" : "dea"}ctivated.`);
 		};
 
 		const classBasedPresetSettingHandler = () => {
 			deps.mod.settings.classBasedPresets = !deps.mod.settings.classBasedPresets;
-			deps.mod.command.message(`Class based presets will be ${deps.mod.settings.classBasedPresets ? "a" : "dea"}ctivated after relog.`);
+			deps.log.send(`Class based presets will be ${deps.mod.settings.classBasedPresets ? "a" : "dea"}ctivated after relog.`);
 		};
 
 		const debugSettingsHandler = () => {
 			deps.mod.settings.debugMode = !deps.mod.settings.debugMode;
-			deps.mod.command.message(`Debug mode was ${deps.mod.settings.debugMode ? "a" : "dea"}ctivated.`);
+			deps.log.send(`Debug mode was ${deps.mod.settings.debugMode ? "a" : "dea"}ctivated.`);
 		};
 
 		const activeModeHandler = (str) => {
 			let res = stringSimilarity.findBestMatch(str, Object.keys(deps.data.preset.modes));
 
 			// eslint-disable-next-line no-magic-numbers
-			if(res.bestMatch.rating < 0.2) { deps.mod.command.message("Mode was not found"); return; }
+			if(res.bestMatch.rating < 0.2) { deps.log.send("Mode was not found"); return; }
 
 			deps.data.prevMode = rfdc(deps.data.activeMode);
 			deps.data.activeMode = deps.data.preset.modes[res.bestMatch.target];
-			deps.mod.command.message(`Mode was changed to "${res.bestMatch.target}".`);
+			
+			deps.log.send(`Mode was changed to "${res.bestMatch.target}".`);
 			deps.data._eventTrigger();
 		};
 
 		const modeListHandler = () => {
-			deps.mod.command.message("Current preset contains:");
+			deps.log.send("Current preset contains:");
 			Object.keys(deps.data.preset.modes).forEach(key => {
-				deps.mod.command.message(`${key}`);
+				deps.log.send(`${key}`);
 			});
 		};
 
@@ -77,11 +75,9 @@ class Commands {
 			"debug": debugSettingsHandler
 		};
 
-		if (deps.mod.settings.customCommandsTag && deps.mod.settings.customCommandTag.length)
-			prefixes.push(deps.mod.settings.customCommandsTag);
-
-		if (deps.mod.settings.registerCommands)
-			deps.mod.command.add(prefixes, commands, this);
+		let prefixes = ["fm"];
+		if(deps.mod.settings.fpsUtilsCommandsPrefix) prefixes.push("fps");
+		if(deps.mod.settings.registerCommands) deps.mod.command.add("fm", commands, this);
 	}
 }
 module.exports = Commands;

+ 24 - 13
lib/unitState.js

@@ -32,11 +32,10 @@ class unitState extends EventEmitter {
 	 */
 	constructor(deps) {
 		super();
-		console.log("DEBUG: unitState -> constructor");
 
 		this.__mod = deps.mod;
 		this.__proto = deps.proto;
-		this.__fps = deps.fps;
+		this.__log = deps.log;
 
 		Object.assign(this, {
 			//main properties for state management
@@ -44,6 +43,7 @@ class unitState extends EventEmitter {
 			"preset": {}, //current chosen preset
 			"activeMode": undefined,
 			"prevMode": undefined,
+			"lockedMode": false,
 			"availableClasses": new Set(),
 			"userTemplateToClassMap": {},
 			"lockedPacketsData": [],
@@ -89,23 +89,31 @@ class unitState extends EventEmitter {
 		this.smtData = new Set(...staticData.blockedSmt);
 
 		this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
+			this.lockedMode = false;
+
 			this.preset = this.__mod.settings.classBasedPresets
 				? (this.presets[this.__mod.game.me.class] || this.presets["common"])
 				: this.presets["common"];
 			if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
+
 			this.activeMode = this.preset.modes[this.preset.directives["default"]];
+
+			if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`)
+			_eventTriggerPartial();
 		});
 
 		this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
-			if (!this.__mod.settings.enableEvents) return;
+			if (!this.__mod.settings.enableEvents || this.lockedMode) return;
 			this.prevMode = rfdc(this.activeMode);
 			if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
 				this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
-				this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerBG}".`);
+
+				if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
 			}
 			else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
 				this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
-				this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerCU}".`);
+
+				if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
 			}
 			else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
 				let mode = undefined;
@@ -119,35 +127,38 @@ class unitState extends EventEmitter {
 					name = this.preset.directives.triggerDungeons["all"];
 					mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
 				}
-				if(mode) {
+
+				if (mode) {
 					this.activeMode = mode;
-					this.__mod.command.message(`Mode was changed to ${name}`);
+					if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
 				}
 			}
 			else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
 				this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
-				this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerOW}".`);
+				if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
 			}
-			
+
 			this._eventTriggerPartial();
 		});
 
 		this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
 			if (!this.__mod.settings.enableEvents) return;
-
 			this.prevMode = rfdc(this.activeMode);
+			this.lockedMode = true;
 
 			this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
-			this.__mod.command.message(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
+
+			if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
 
 			this._eventTrigger();
 		});
 
 		this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
 			if (!this.__mod.settings.enableEvents) return;
-
 			this.activeMode = rfdc(this.prevMode);
-			this.__mod.command.message("Mode was changed back");
+			this.lockedMode = false;
+
+			if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
 
 			this._eventTrigger();
 		});