From 205dba32c7f2e416b6cd4081dc698128fc301bb3 Mon Sep 17 00:00:00 2001 From: elexis Date: Wed, 30 Oct 2019 14:06:27 +0000 Subject: [PATCH] Cheats GUI container class, refs #5387. This was SVN commit r23116. --- .../data/mods/public/gui/session/Cheats.js | 58 ++++++++++++++++++ .../data/mods/public/gui/session/chat/Chat.js | 4 +- .../data/mods/public/gui/session/messages.js | 60 ------------------- .../data/mods/public/gui/session/session.js | 4 +- 4 files changed, 63 insertions(+), 63 deletions(-) create mode 100644 binaries/data/mods/public/gui/session/Cheats.js diff --git a/binaries/data/mods/public/gui/session/Cheats.js b/binaries/data/mods/public/gui/session/Cheats.js new file mode 100644 index 0000000000..48643d392b --- /dev/null +++ b/binaries/data/mods/public/gui/session/Cheats.js @@ -0,0 +1,58 @@ +/** + * The purpose of this class is to run a given user input against the cheats database on request + * and perform that cheat if an according cheat was found. + */ +class Cheats +{ + constructor() + { + this.cheats = {}; + for (let fileName of Engine.ListDirectoryFiles(this.Directory, "*.json", false)) + { + let cheat = Engine.ReadJSONFile(fileName); + if (this.cheats[cheat.Name]) + warn("Cheat name '" + cheat.Name + "' is already present"); + else + this.cheats[cheat.Name] = cheat.Data; + } + } + + /** + * Reads userinput from the chat and sends a simulation command in case it is a known cheat. + * @returns {boolean} - True if a cheat was executed. + */ + executeCheat(text) + { + if (!controlsPlayer(Engine.GetPlayerID()) || + !g_Players[Engine.GetPlayerID()].cheatsEnabled) + return false; + + // Find the cheat code that is a prefix of the user input + let cheatCode = Object.keys(this.cheats).find(code => text.indexOf(code) == 0); + if (!cheatCode) + return false; + + let cheat = this.cheats[cheatCode]; + + let parameter = text.substr(cheatCode.length + 1); + if (cheat.isNumeric) + parameter = +parameter; + + if (cheat.DefaultParameter && !parameter) + parameter = cheat.DefaultParameter; + + Engine.PostNetworkCommand({ + "type": "cheat", + "action": cheat.Action, + "text": cheat.Type, + "player": Engine.GetPlayerID(), + "parameter": parameter, + "templates": cheat.Templates, + "selected": g_Selection.toList() + }); + + return true; + } +} + +Cheats.prototype.Directory = "simulation/data/cheats/"; diff --git a/binaries/data/mods/public/gui/session/chat/Chat.js b/binaries/data/mods/public/gui/session/chat/Chat.js index 879be249f2..9d1a4aa657 100644 --- a/binaries/data/mods/public/gui/session/chat/Chat.js +++ b/binaries/data/mods/public/gui/session/chat/Chat.js @@ -4,7 +4,7 @@ */ class Chat { - constructor(playerViewControl) + constructor(playerViewControl, cheats) { this.ChatWindow = new ChatWindow(); this.ChatOverlay = new ChatOverlay(); @@ -14,7 +14,7 @@ class Chat this.ChatInput = new ChatInput(); this.ChatInput.registerChatSubmitHandler(executeNetworkCommand); - this.ChatInput.registerChatSubmitHandler(executeCheat); + this.ChatInput.registerChatSubmitHandler(cheats.executeCheat.bind(cheats)); this.ChatInput.registerChatSubmitHandler(this.submitChat.bind(this)); this.ChatInput.registerChatSubmittedHandler(this.closePage.bind(this)); diff --git a/binaries/data/mods/public/gui/session/messages.js b/binaries/data/mods/public/gui/session/messages.js index 334dd3776b..341f9a1a6e 100644 --- a/binaries/data/mods/public/gui/session/messages.js +++ b/binaries/data/mods/public/gui/session/messages.js @@ -1,8 +1,3 @@ -/** - * All known cheat commands. - */ -const g_Cheats = getCheatsData(); - /** * All tutorial messages received so far. */ @@ -324,61 +319,6 @@ function registerClientsLoadingHandler(handler) g_ClientsLoadingHandlers.add(handler); } -/** - * Loads all known cheat commands. - */ -function getCheatsData() -{ - let cheats = {}; - for (let fileName of Engine.ListDirectoryFiles("simulation/data/cheats/", "*.json", false)) - { - let currentCheat = Engine.ReadJSONFile(fileName); - if (cheats[currentCheat.Name]) - warn("Cheat name '" + currentCheat.Name + "' is already present"); - else - cheats[currentCheat.Name] = currentCheat.Data; - } - return deepfreeze(cheats); -} - -/** - * Reads userinput from the chat and sends a simulation command in case it is a known cheat. - * - * @returns {boolean} - True if a cheat was executed. - */ -function executeCheat(text) -{ - if (!controlsPlayer(Engine.GetPlayerID()) || - !g_Players[Engine.GetPlayerID()].cheatsEnabled) - return false; - - // Find the cheat code that is a prefix of the user input - let cheatCode = Object.keys(g_Cheats).find(code => text.indexOf(code) == 0); - if (!cheatCode) - return false; - - let cheat = g_Cheats[cheatCode]; - - let parameter = text.substr(cheatCode.length + 1); - if (cheat.isNumeric) - parameter = +parameter; - - if (cheat.DefaultParameter && !parameter) - parameter = cheat.DefaultParameter; - - Engine.PostNetworkCommand({ - "type": "cheat", - "action": cheat.Action, - "text": cheat.Type, - "player": Engine.GetPlayerID(), - "parameter": parameter, - "templates": cheat.Templates, - "selected": g_Selection.toList() - }); - - return true; -} - function findGuidForPlayerID(playerID) { return Object.keys(g_PlayerAssignments).find(guid => g_PlayerAssignments[guid].player == playerID); diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index 424318c268..555e02ebf7 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -11,6 +11,7 @@ const g_VictoryDurations = prepareForDropdown(g_Settings && g_Settings.VictoryDu const g_VictoryConditions = g_Settings && g_Settings.VictoryConditions; var g_Chat; +var g_Cheats; var g_DeveloperOverlay; var g_DiplomacyColors; var g_DiplomacyDialog; @@ -262,6 +263,7 @@ function init(initData, hotloadData) restoreSavedGameData(initData.savedGUIData); } + g_Cheats = new Cheats(); g_DiplomacyColors = new DiplomacyColors(); g_PlayerViewControl = new PlayerViewControl(); g_PlayerViewControl.registerViewedPlayerChangeHandler(g_DiplomacyColors.updateDisplayedPlayerColors.bind(g_DiplomacyColors)); @@ -271,7 +273,7 @@ function init(initData, hotloadData) g_PlayerViewControl.registerPreViewedPlayerChangeHandler(removeStatusBarDisplay); g_PlayerViewControl.registerViewedPlayerChangeHandler(resetTemplates); - g_Chat = new Chat(g_PlayerViewControl); + g_Chat = new Chat(g_PlayerViewControl, g_Cheats); g_DeveloperOverlay = new DeveloperOverlay(g_PlayerViewControl, g_Selection); g_DiplomacyDialog = new DiplomacyDialog(g_PlayerViewControl, g_DiplomacyColors); g_GameSpeedControl = new GameSpeedControl(g_PlayerViewControl);