mirror of
https://github.com/ApfelTeeSaft/PlanetFleet.git
synced 2026-08-26 19:33:38 +00:00
refactored command execution
This commit is contained in:
+19
-20
@@ -6,29 +6,28 @@
|
|||||||
namespace battleship{
|
namespace battleship{
|
||||||
using namespace std;
|
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(){
|
void AbstractCommand::handle(){
|
||||||
if(cmdStr.find(" ") == -1)
|
if(cmdStr.find(" ") == -1)
|
||||||
return;
|
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
@@ -8,14 +8,12 @@ namespace battleship{
|
|||||||
class AbstractCommand{
|
class AbstractCommand{
|
||||||
protected:
|
protected:
|
||||||
AbstractCommand(std::string str) : cmdStr(str){}
|
AbstractCommand(std::string str) : cmdStr(str){}
|
||||||
virtual void execute(){}
|
|
||||||
virtual void validate(){}
|
|
||||||
virtual void handle();
|
virtual void handle();
|
||||||
|
virtual void validate(){}
|
||||||
|
virtual void execute();
|
||||||
|
|
||||||
std::string cmdStr;
|
std::string cmdStr;
|
||||||
std::vector<std::string> arguments;
|
std::vector<std::string> arguments;
|
||||||
private:
|
|
||||||
std::vector<std::string> explodeString(std::string);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,10 @@ namespace battleship{
|
|||||||
resourceAmmount = atoi(arguments[2].c_str());
|
resourceAmmount = atoi(arguments[2].c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddResourceCommand::addResource(){
|
void AddResourceCommand::execute(){
|
||||||
|
AbstractCommand::execute();
|
||||||
|
|
||||||
Player *player = Game::getSingleton()->getPlayer(playerId);
|
Player *player = Game::getSingleton()->getPlayer(playerId);
|
||||||
player->updateResource(ResourceType(resourceId), resourceAmmount, true);
|
player->updateResource(ResourceType(resourceId), resourceAmmount, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddResourceCommand::execute(){
|
|
||||||
AbstractCommand::handle();
|
|
||||||
validate();
|
|
||||||
addResource();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ namespace battleship{
|
|||||||
void execute();
|
void execute();
|
||||||
private:
|
private:
|
||||||
void validate();
|
void validate();
|
||||||
void addResource();
|
|
||||||
|
|
||||||
int playerId, resourceId, resourceAmmount;
|
int playerId, resourceId, resourceAmmount;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -30,13 +30,8 @@ namespace battleship{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddTechnologyCommand::addTechnology(){
|
void AddTechnologyCommand::execute(){
|
||||||
|
AbstractCommand::execute();
|
||||||
Game::getSingleton()->getPlayer(playerId)->addTechnology(techId);
|
Game::getSingleton()->getPlayer(playerId)->addTechnology(techId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddTechnologyCommand::execute(){
|
|
||||||
AbstractCommand::handle();
|
|
||||||
validate();
|
|
||||||
addTechnology();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ namespace battleship{
|
|||||||
void execute();
|
void execute();
|
||||||
private:
|
private:
|
||||||
void validate();
|
void validate();
|
||||||
void addTechnology();
|
|
||||||
|
|
||||||
int playerId, techId;
|
int playerId, techId;
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-7
@@ -54,14 +54,10 @@ namespace battleship{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddUnitCommand::addUnit(){
|
void AddUnitCommand::execute(){
|
||||||
|
AbstractCommand::execute();
|
||||||
|
|
||||||
Player* player = Game::getSingleton()->getPlayer(playerId);
|
Player* player = Game::getSingleton()->getPlayer(playerId);
|
||||||
player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot, 100));
|
player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddUnitCommand::execute(){
|
|
||||||
AbstractCommand::handle();
|
|
||||||
validate();
|
|
||||||
addUnit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ namespace battleship{
|
|||||||
bool posEnabled, rotEnabled;
|
bool posEnabled, rotEnabled;
|
||||||
vb01::Vector3 pos = vb01::Vector3::VEC_ZERO;
|
vb01::Vector3 pos = vb01::Vector3::VEC_ZERO;
|
||||||
vb01::Quaternion rot = vb01::Quaternion::QUAT_W;
|
vb01::Quaternion rot = vb01::Quaternion::QUAT_W;
|
||||||
|
|
||||||
void addUnit();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ namespace battleship{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ToggleDebugCommand::execute(){
|
void ToggleDebugCommand::execute(){
|
||||||
AbstractCommand::handle();
|
AbstractCommand::execute();
|
||||||
validate();
|
|
||||||
|
|
||||||
Game *game = Game::getSingleton();
|
Game *game = Game::getSingleton();
|
||||||
game->setDebug(!game->isDebug());
|
game->setDebug(!game->isDebug());
|
||||||
|
|||||||
Reference in New Issue
Block a user