I am writing a strategy game in PHP and running into a design problem with MVC. Player in my game can choose to produce units. Players do this spending some resources. When players asks to produce some units, the game has to check if they have enpugh resources and then spend them. This is done using the Resource Model. Then if this is succesful it has to call the UnitFactory Model to start new production. To communicate errors between models and controllers, I use class Err
with static methods Err::newCom($msg)
and Err::comReceived($msg)
, Err::newCom()
adds a message string to an array Err::$coms
and the Err::comReceived()
method checks if given message exists in the message array. I am doing it like this: Controller:
$ufm =$this->model('unitfactory');
$rm=$this->model('resource');
$checkResult = $rm->hasEnoughByUserAIID($diagramResources,$this->user['ai_id']);
if(Err::comReceived('not_enough_resources'))
{
$data
+= [ 'data'=>
['unsifficient_resources'=>TRUE,'needs_to_have_how_much_more'=>$checkResult
]
];
}else{
$rm->spendByResourcesAndUserAIID($diagramResources,$this->user['ai_id']);
$ufm->startNewProduction($diagram, $amount);
$data['data']=
['time_remaining'=>$ufm->productionTime];
}
Resource Model:
public function hasEnoughByResourcesAndUserAIID($resources,$useraiid) {
// here goes the checking process
// which gives $sufficient_res variable which is either true or false if the player has enough or not respectively and
// requiredToBeEnough is array with info about how much more resources the player needs if that they have is not sufficient
if(!$sufficient_res) Err::newCom('not_enough_resources');
return $requiredToBeEnough;
public function spendByResourcesAndUserAIID($resources,$useraiid) {
// here is the spending procces happens, meaning
// that resources are arithmetically taken from the user_resources DB table
}
}
As you can see, game internally checks in the Controller if the player has enough resources to produce units and depending on the result of this check, it will or won't start new production. If the player has enough resources, only when they are spend and the UnitFactory method is called. If not, game only places som3 information into $data array for display later on. This is how I currently coded the proccess. However I have doubts regarding where the checking should actually be. Would it be more correct to call the hasEnoughByResourcesAndUserAIID()
method from spendByResourcesAndUserAIID()
instead of doing it separetelly in the controller? I am not sure. I do not think it will matter either way, but I am not sure and this design question bugs me, but I could not find the qeustion till now, so here I am. By the way, if you need any clarifications, please ask, I will be happy to answer them. If my qestion was not very clear until now, I guess I am asking if I should do if(someGameCheck()) else statements in the controller or model? I like the controller variant better, but will it BE better when I will edit the code later? P.S. Sorry, if I made some grammatical mistakes somewhere.
Aucun commentaire:
Enregistrer un commentaire