refactored command execution

This commit is contained in:
devZoGok
2024-10-19 17:37:45 +03:00
parent e0b6550787
commit 9362e53c15
9 changed files with 30 additions and 51 deletions
+19 -20
View File
@@ -6,29 +6,28 @@
namespace battleship{
using namespace std;
vector<string> AbstractCommand::explodeString(string commandStr){
vector<int> spaceIds;
vector<string> 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<int> 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();
}
}
+2 -4
View File
@@ -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<std::string> arguments;
private:
std::vector<std::string> explodeString(std::string);
};
}
+3 -7
View File
@@ -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();
}
}
-1
View File
@@ -10,7 +10,6 @@ namespace battleship{
void execute();
private:
void validate();
void addResource();
int playerId, resourceId, resourceAmmount;
};
+2 -7
View File
@@ -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();
}
}
-1
View File
@@ -10,7 +10,6 @@ namespace battleship{
void execute();
private:
void validate();
void addTechnology();
int playerId, techId;
};
+3 -7
View File
@@ -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();
}
}
-2
View File
@@ -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();
};
}
+1 -2
View File
@@ -7,8 +7,7 @@ namespace battleship{
}
void ToggleDebugCommand::execute(){
AbstractCommand::handle();
validate();
AbstractCommand::execute();
Game *game = Game::getSingleton();
game->setDebug(!game->isDebug());