Guest User

PHP kawałek kodu do oceny - powrót projektu gra

a guest
Apr 27th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.57 KB | None | 0 0
  1. <?php
  2.     public function updateUnits()
  3.     {
  4.         /**
  5.          * @var $update \AppBundle\Entity\Game\BarracksQueue
  6.          */
  7.         foreach ($this->villages as $village) {
  8.             //dodalem metode
  9.             $this->updateViaRepository(
  10.                 $this->em->getRepository('AppBundle:Game\BarracksQueue')->findEnd($village->getId())
  11.             );
  12.             $this->em->flush();
  13.         }
  14.         return $this;
  15.     }
  16.  
  17.     /**
  18.      * @param KlasaLubInterfaceLubArrayZwracanyZFindEnd $villageRepository
  19.      *
  20.      * wymysl lepsza nazwe do tego, pamietaj,
  21.      * wypada, by byla czasownikeim
  22.      */
  23.     private function updateViaRepository(KlasaLubInterfaceLubArrayZwracanyZFindEnd $villageRepository)
  24.     {
  25.         foreach ($villageRepository as $repository) {
  26.             $village = $this->em->getRepository('AppBundle:Player\Village\Village')->find($repository->getVillage());
  27.             if (!$village) {
  28.                 $this->em->remove($repository);
  29.                 //dodalem kontynuacje, usunalem else
  30.                 continue;
  31.             }
  32.  
  33.             $this->updateViaArmy($village->getArmy(), $repository);
  34.         }
  35.     }
  36.  
  37.     /**
  38.      * @param KlasaLubInterfaceLubArrayZwracanyZGetArmy $units
  39.      * @param KlasaLubInterfaceLubArrayZwracanyZFindEnd $repository
  40.      */
  41.     private function updateViaArmy(
  42.         KlasaLubInterfaceLubArrayZwracanyZGetArmy $units,
  43.         KlasaLubInterfaceLubArrayZwracanyZFindEnd $repository
  44.     ) {
  45.         //tutaj raczej dalbym metode o nazwie setUnit, i w kazdej z tej klas mial taka metode
  46.         //napisz interfejs
  47.         //$method = "set" . ucfirst($repository->getUnitName());
  48.  
  49.         //ten watunek zamienimy na inny, tylko kazdy units musi posiadac ta metode
  50. //            if (!method_exists($units, $method)) {
  51. //                continue;
  52. //            }
  53.  
  54.         if (!$units->canBeSet()) {
  55.             return;
  56.         }
  57.         if (!($repository->getTimeStart() < $_SERVER['REQUEST_TIME'])) {
  58.             return;
  59.         }
  60.  
  61.         $all = $repository->getUnitCount();
  62.         $end = $repository->getUnitEndSchool();
  63.         if ($repository->getTimeEnd() < $_SERVER['REQUEST_TIME']) {
  64.             //tu tez metoda z interfejsu
  65.             //$notReadyUnits = $units->{'get' . ucfirst($repository->getUnitName())}() + ($all - $end);
  66.             $notReadyUnits = $units->getUnit($repository->getUnitName()) + ($all - $end);
  67.             //dalej uzywam metody zdefiniowanej w interfejsie
  68.             $units->setUnit($notReadyUnits);
  69.             $this->em->persist($units);
  70.             $this->em->remove($repository);
  71.             return;
  72.         }
  73.  
  74.         //tutaj w uj pomieszales, musisz to poprawic
  75.         // w ogole nie wiem, co to jest $all co to jest $end
  76.         if ($this->canUpdate($repository)) {
  77.             $this->singleUpdate($units, $repository);
  78.         } else {
  79.             $this->em->remove($repository);
  80.             $this->em->persist($units);
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * @param NazwaKlasyUnits $units
  86.      * @param NazwaKlasyRepozytorium $repository
  87.      */
  88.     private function singleUpdate(NazwaKlasyUnits $units, NazwaKlasyRepozytorium $repository)
  89.     {
  90.         $nowEnd = $this->getNowEnd();
  91.         //dalej uzywam metody zdefiniowanej w interfejsie
  92.         $units->setUnit($units->getUnit($repository->getUnitName()) + $nowEnd);
  93.         $repository->setUnitEndSchool($repository->getUnitEndSchool() + $nowEnd);
  94.         if ($repository->getUnitEndSchool() === $repository->getUnitCount()) {
  95.             $this->em->remove($repository);
  96.         }
  97.         $this->em->persist($units);
  98.     }
  99.  
  100.     /**
  101.      * @param NazwaKlasyRepozytorium $repository
  102.      * @return bool
  103.      */
  104.     //ta metode mozna zamiescic w klasie $repository
  105.     private function canUpdate(NazwaKlasyRepozytorium $repository)
  106.     {
  107.         $all = $repository->getUnitCount();
  108.         $end = $repository->getUnitEndSchool();
  109.  
  110.         if (!($all > $end)) {
  111.             return false;
  112.         }
  113.  
  114.         if (!($this->getNowEnd > 0)) {
  115.             return false;
  116.         }
  117.  
  118.         return true;
  119.     }
  120.  
  121.     /**
  122.      * @param $repository
  123.      * @return float
  124.      */
  125.     //ta metode mozna zamiescic w klasie $repository
  126.     //ewentualnie, jak juz raz sie pobralo, przetrzymywac ja we wlasciwosci
  127.     private function getNowEnd($repository)
  128.     {
  129.         $nowEnd = $_SERVER['REQUEST_TIME'];
  130.         $nowEnd -= $repository->getTimeStart() + ($repository->getUnitEndSchool() * $repository->getUnitOneTime());
  131.         $nowEnd /= $repository->getUnitOneTime();
  132.         return floor($nowEnd);
  133.     }
Add Comment
Please, Sign In to add comment