Tak jak w temacie, wróciłem do projektu swojej gry, gra strategia, a'la plemiona tylko w aktualnych czasach, w pewnej funkcji wyszło mi sporo ifów i chciałbym się dowiedzieć jak pod względem "czystości" kody to wygląda, ponieważ grę robię w celu nauki czyli także git-a ;) wrzucam kod na githuba
Całe repozytorium: https://github.com/makoso/My-Strategy-Game
Serwis wioski, obsługuje wszystkie jej funkcje: https://github.com/makoso/My-Strategy-Game/blob/master/src/AppBundle/Services/VillageService.php
Powyższa klasa zawiera funkcje o której mówiłem wyżej i zamieszczam ją także poniżej
//FUNKCJA ODPOWIADAJĄCA ZA SZKOLENIE JEDNOSTEK jednostki dodaje się grupowo do kolejki ale mogą opuszczać ją pojedynczo
public function updateUnits()
{
/**
* @var $update \AppBundle\Entity\Game\BarracksQueue
*/
foreach ($this->villages as $village) {
$toUpdate = $this->em->getRepository('AppBundle:Game\BarracksQueue')->findEnd($village->getId());
foreach ($toUpdate as $update) {
$village = $this->em->getRepository('AppBundle:Player\Village\Village')->find($update->getVillage());
if (!$village) {
$this->em->remove($update);
} else {
$units = $village->getArmy();
$method = "set".ucfirst($update->getUnitName());
if (method_exists($units, $method)) {
if ($update->getTimeStart() < $_SERVER['REQUEST_TIME']) {
$all = $update->getUnitCount();
$end = $update->getUnitEndSchool();
if($update->getTimeEnd() < $_SERVER['REQUEST_TIME']){
$units->{$method}($units->{'get'.ucfirst($update->getUnitName())}() + ($all - $end) );
$this->em->persist($units);
$this->em->remove($update);
}else {
if($all > $end){
$nowEnd = floor(($_SERVER['REQUEST_TIME'] - ($update->getTimeStart() + ($end * $update->getUnitOneTime()))) / $update->getUnitOneTime());
if($nowEnd > 0){
$units->{$method}($units->{'get'.ucfirst($update->getUnitName())}() + $nowEnd );
$update->setUnitEndSchool($update->getUnitEndSchool() + $nowEnd);
if($update->getUnitEndSchool() === $update->getUnitCount()){
$this->em->remove($update);
}
$this->em->persist($units);
}
} else {
$this->em->remove($update);
$this->em->persist($units);
}
}
}
}
}
}
$this->em->flush();
}
return $this;
}