From 9362e53c1554b757072a0dfb951c29c113bfa628 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 19 Oct 2024 17:37:45 +0300 Subject: [PATCH] refactored command execution --- abstractCommand.cpp | 39 +++++++++++++++++++-------------------- abstractCommand.h | 6 ++---- addResourceCommand.cpp | 10 +++------- addResourceCommand.h | 1 - addTechnologyCommand.cpp | 9 ++------- addTechnologyCommand.h | 1 - addUnitCommand.cpp | 10 +++------- addUnitCommand.h | 2 -- toggleDebugCommand.cpp | 3 +-- 9 files changed, 30 insertions(+), 51 deletions(-) diff --git a/abstractCommand.cpp b/abstractCommand.cpp index 5807c62..4c19c45 100644 --- a/abstractCommand.cpp +++ b/abstractCommand.cpp @@ -6,29 +6,28 @@ namespace battleship{ using namespace std; - vector AbstractCommand::explodeString(string commandStr){ - vector spaceIds; - vector fullCommand; - - for(int i = 0; i < commandStr.length(); i++) - if(commandStr[i] == ' ') - spaceIds.push_back(i); - - fullCommand.push_back(commandStr.substr(0, spaceIds[0])); - - for(int i = 0; i < spaceIds.size(); i++){ - bool lastSpace = (i == spaceIds.size() - 1); - string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); - fullCommand.push_back(argument); - } - - return fullCommand; - } - void AbstractCommand::handle(){ if(cmdStr.find(" ") == -1) return; - arguments = explodeString(cmdStr); + vector spaceIds; + arguments.clear(); + + for(int i = 0; i < cmdStr.length(); i++) + if(cmdStr[i] == ' ') + spaceIds.push_back(i); + + arguments.push_back(cmdStr.substr(0, spaceIds[0])); + + for(int i = 0; i < spaceIds.size(); i++){ + bool lastSpace = (i == spaceIds.size() - 1); + string argument = cmdStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); + arguments.push_back(argument); + } + } + + void AbstractCommand::execute(){ + handle(); + validate(); } } diff --git a/abstractCommand.h b/abstractCommand.h index b02f6eb..8e3a97f 100644 --- a/abstractCommand.h +++ b/abstractCommand.h @@ -8,14 +8,12 @@ namespace battleship{ class AbstractCommand{ protected: AbstractCommand(std::string str) : cmdStr(str){} - virtual void execute(){} - virtual void validate(){} virtual void handle(); + virtual void validate(){} + virtual void execute(); std::string cmdStr; std::vector arguments; - private: - std::vector explodeString(std::string); }; } diff --git a/addResourceCommand.cpp b/addResourceCommand.cpp index 3a36278..15bfe6a 100644 --- a/addResourceCommand.cpp +++ b/addResourceCommand.cpp @@ -21,14 +21,10 @@ namespace battleship{ resourceAmmount = atoi(arguments[2].c_str()); } - void AddResourceCommand::addResource(){ + void AddResourceCommand::execute(){ + AbstractCommand::execute(); + Player *player = Game::getSingleton()->getPlayer(playerId); player->updateResource(ResourceType(resourceId), resourceAmmount, true); } - - void AddResourceCommand::execute(){ - AbstractCommand::handle(); - validate(); - addResource(); - } } diff --git a/addResourceCommand.h b/addResourceCommand.h index b5c55bb..711a829 100644 --- a/addResourceCommand.h +++ b/addResourceCommand.h @@ -10,7 +10,6 @@ namespace battleship{ void execute(); private: void validate(); - void addResource(); int playerId, resourceId, resourceAmmount; }; diff --git a/addTechnologyCommand.cpp b/addTechnologyCommand.cpp index 7ff5025..73468c7 100644 --- a/addTechnologyCommand.cpp +++ b/addTechnologyCommand.cpp @@ -30,13 +30,8 @@ namespace battleship{ return; } - void AddTechnologyCommand::addTechnology(){ + void AddTechnologyCommand::execute(){ + AbstractCommand::execute(); Game::getSingleton()->getPlayer(playerId)->addTechnology(techId); } - - void AddTechnologyCommand::execute(){ - AbstractCommand::handle(); - validate(); - addTechnology(); - } } diff --git a/addTechnologyCommand.h b/addTechnologyCommand.h index d3fb32c..f60cd45 100644 --- a/addTechnologyCommand.h +++ b/addTechnologyCommand.h @@ -10,7 +10,6 @@ namespace battleship{ void execute(); private: void validate(); - void addTechnology(); int playerId, techId; }; diff --git a/addUnitCommand.cpp b/addUnitCommand.cpp index d3d0f51..c46452f 100644 --- a/addUnitCommand.cpp +++ b/addUnitCommand.cpp @@ -54,14 +54,10 @@ namespace battleship{ return; } - void AddUnitCommand::addUnit(){ + void AddUnitCommand::execute(){ + AbstractCommand::execute(); + Player* player = Game::getSingleton()->getPlayer(playerId); player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot, 100)); } - - void AddUnitCommand::execute(){ - AbstractCommand::handle(); - validate(); - addUnit(); - } } diff --git a/addUnitCommand.h b/addUnitCommand.h index a0d5429..b91467f 100644 --- a/addUnitCommand.h +++ b/addUnitCommand.h @@ -17,8 +17,6 @@ namespace battleship{ bool posEnabled, rotEnabled; vb01::Vector3 pos = vb01::Vector3::VEC_ZERO; vb01::Quaternion rot = vb01::Quaternion::QUAT_W; - - void addUnit(); }; } diff --git a/toggleDebugCommand.cpp b/toggleDebugCommand.cpp index 02636fd..5ebf9c9 100644 --- a/toggleDebugCommand.cpp +++ b/toggleDebugCommand.cpp @@ -7,8 +7,7 @@ namespace battleship{ } void ToggleDebugCommand::execute(){ - AbstractCommand::handle(); - validate(); + AbstractCommand::execute(); Game *game = Game::getSingleton(); game->setDebug(!game->isDebug());