Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- public function updateUnits()
- {
- /**
- * @var $update \AppBundle\Entity\Game\BarracksQueue
- */
- foreach ($this->villages as $village) {
- //dodalem metode
- $this->updateViaRepository(
- $this->em->getRepository('AppBundle:Game\BarracksQueue')->findEnd($village->getId())
- );
- $this->em->flush();
- }
- return $this;
- }
- /**
- * @param KlasaLubInterfaceLubArrayZwracanyZFindEnd $villageRepository
- *
- * wymysl lepsza nazwe do tego, pamietaj,
- * wypada, by byla czasownikeim
- */
- private function updateViaRepository(KlasaLubInterfaceLubArrayZwracanyZFindEnd $villageRepository)
- {
- foreach ($villageRepository as $repository) {
- $village = $this->em->getRepository('AppBundle:Player\Village\Village')->find($repository->getVillage());
- if (!$village) {
- $this->em->remove($repository);
- //dodalem kontynuacje, usunalem else
- continue;
- }
- $this->updateViaArmy($village->getArmy(), $repository);
- }
- }
- /**
- * @param KlasaLubInterfaceLubArrayZwracanyZGetArmy $units
- * @param KlasaLubInterfaceLubArrayZwracanyZFindEnd $repository
- */
- private function updateViaArmy(
- KlasaLubInterfaceLubArrayZwracanyZGetArmy $units,
- KlasaLubInterfaceLubArrayZwracanyZFindEnd $repository
- ) {
- //tutaj raczej dalbym metode o nazwie setUnit, i w kazdej z tej klas mial taka metode
- //napisz interfejs
- //$method = "set" . ucfirst($repository->getUnitName());
- //ten watunek zamienimy na inny, tylko kazdy units musi posiadac ta metode
- // if (!method_exists($units, $method)) {
- // continue;
- // }
- if (!$units->canBeSet()) {
- return;
- }
- if (!($repository->getTimeStart() < $_SERVER['REQUEST_TIME'])) {
- return;
- }
- $all = $repository->getUnitCount();
- $end = $repository->getUnitEndSchool();
- if ($repository->getTimeEnd() < $_SERVER['REQUEST_TIME']) {
- //tu tez metoda z interfejsu
- //$notReadyUnits = $units->{'get' . ucfirst($repository->getUnitName())}() + ($all - $end);
- $notReadyUnits = $units->getUnit($repository->getUnitName()) + ($all - $end);
- //dalej uzywam metody zdefiniowanej w interfejsie
- $units->setUnit($notReadyUnits);
- $this->em->persist($units);
- $this->em->remove($repository);
- return;
- }
- //tutaj w uj pomieszales, musisz to poprawic
- // w ogole nie wiem, co to jest $all co to jest $end
- if ($this->canUpdate($repository)) {
- $this->singleUpdate($units, $repository);
- } else {
- $this->em->remove($repository);
- $this->em->persist($units);
- }
- }
- /**
- * @param NazwaKlasyUnits $units
- * @param NazwaKlasyRepozytorium $repository
- */
- private function singleUpdate(NazwaKlasyUnits $units, NazwaKlasyRepozytorium $repository)
- {
- $nowEnd = $this->getNowEnd();
- //dalej uzywam metody zdefiniowanej w interfejsie
- $units->setUnit($units->getUnit($repository->getUnitName()) + $nowEnd);
- $repository->setUnitEndSchool($repository->getUnitEndSchool() + $nowEnd);
- if ($repository->getUnitEndSchool() === $repository->getUnitCount()) {
- $this->em->remove($repository);
- }
- $this->em->persist($units);
- }
- /**
- * @param NazwaKlasyRepozytorium $repository
- * @return bool
- */
- //ta metode mozna zamiescic w klasie $repository
- private function canUpdate(NazwaKlasyRepozytorium $repository)
- {
- $all = $repository->getUnitCount();
- $end = $repository->getUnitEndSchool();
- if (!($all > $end)) {
- return false;
- }
- if (!($this->getNowEnd > 0)) {
- return false;
- }
- return true;
- }
- /**
- * @param $repository
- * @return float
- */
- //ta metode mozna zamiescic w klasie $repository
- //ewentualnie, jak juz raz sie pobralo, przetrzymywac ja we wlasciwosci
- private function getNowEnd($repository)
- {
- $nowEnd = $_SERVER['REQUEST_TIME'];
- $nowEnd -= $repository->getTimeStart() + ($repository->getUnitEndSchool() * $repository->getUnitOneTime());
- $nowEnd /= $repository->getUnitOneTime();
- return floor($nowEnd);
- }
Add Comment
Please, Sign In to add comment