forked from mirrors/0ad
@@ -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/";
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user