/** * @param Championship $championship * @return Championship */ public function makeChampionshipPhase(&$championship) { $em = $this->getDoctrine()->getManager(); $fs = $this->getFightService(); $fights = $em->getRepository('GameBaseBundle:Championship')->getUnfinishedFights($championship->getId()); $fightsNumber = count($fights); if ($fightsNumber == 1) { while ($fights[0]->getWinnerId() == -1) { $fights[0] = $fs->makeFight($fights[0]); } $championship->setWinnerId($fights[0]->getWinnerId()); $championship->setStatus(Championship::STATUS_FINISHED); } else { for ($i = 0; $i < $fightsNumber; $i += 2) { while ($fights[$i]->getWinnerId() == -1) { $fights[$i] = $fs->makeFight($fights[$i]); } while ($fights[$i + 1]->getWinnerId() == -1) { $fights[$i + 1] = $fs->makeFight($fights[$i + 1]); } $winner1 = $fights[$i]->getWinnerId() == $fights[$i]->getFighter1()->getId() ? $fights[$i]->getFighter1() : $fights[$i]->getFighter2(); $winner2 = $fights[$i + 1]->getWinnerId() == $fights[$i + 1]->getFighter1()->getId() ? $fights[$i + 1]->getFighter1() : $fights[$i + 1]->getFighter2(); $newFight = new Fight(); $newFight->setChampionship($championship); $newFight->setDate(new \DateTime()); $newFight->setFighter1($winner1); $newFight->setFighter2($winner2); $newFight->setWinnerId(-1); $em->persist($newFight); } } $em->flush(); return $championship; }
/** * @param Fight $fight * @return Fight */ public function makeFight($fight) { $fighter1 = $fight->getFighter1(); $fighter2 = $fight->getFighter2(); $scoreFighter1 = $this->getScore($fighter1); $scoreFighter2 = $this->getScore($fighter2); $draw = $this->drawTest($scoreFighter1, $scoreFighter2); if ($draw) { $fight->setWinnerId(-1); } else { $fight->setWinnerId($scoreFighter1 > $scoreFighter2 ? $fighter1->getId() : $fighter2->getId()); } return $fight; }
public function load(ObjectManager $manager) { $fight1 = new Fight(); $fight1->setFighter1($this->getReference('fighter1')); $fight1->setFighter2($this->getReference('fighter2')); $fight1->setWinnerId($this->getReference('fighter1')->getId()); $fight1->setChampionship($this->getReference('championship1')); $fight1->setDate(new \DateTime("2013-07-23 06:12:33")); $manager->persist($fight1); $fight2 = new Fight(); $fight2->setFighter1($this->getReference('fighter2')); $fight2->setFighter2($this->getReference('fighter3')); $fight2->setWinnerId($this->getReference('fighter3')->getId()); $fight2->setChampionship($this->getReference('championship1')); $fight2->setDate(new \DateTime("2015-10-23 06:12:33")); $manager->persist($fight2); $fight3 = new Fight(); $fight3->setFighter1($this->getReference('fighter3')); $fight3->setFighter2($this->getReference('fighter1')); $fight3->setWinnerId($this->getReference('fighter3')->getId()); $fight3->setChampionship($this->getReference('championship1')); $fight3->setDate(new \DateTime("2014-07-23 06:12:33")); $manager->persist($fight3); $fight4 = new Fight(); $fight4->setFighter1($this->getReference('fighter1')); $fight4->setFighter2($this->getReference('fighter2')); $fight4->setWinnerId($this->getReference('fighter1')->getId()); $fight4->setChampionship($this->getReference('championship2')); $fight4->setDate(new \DateTime("2013-07-23 06:12:33")); $manager->persist($fight4); $fight5 = new Fight(); $fight5->setFighter1($this->getReference('fighter1')); $fight5->setFighter2($this->getReference('fighter3')); $fight5->setWinnerId($this->getReference('fighter1')->getId()); $fight5->setChampionship($this->getReference('championship2')); $fight5->setDate(new \DateTime("2011-07-23 06:12:33")); $manager->persist($fight5); $fight6 = new Fight(); $fight6->setFighter1($this->getReference('fighter3')); $fight6->setFighter2($this->getReference('fighter1')); $fight6->setWinnerId($this->getReference('fighter3')->getId()); $fight6->setChampionship($this->getReference('championship3')); $fight6->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight6); ////////////////////////////////////// //fights for first real championship// ////////////////////////////////////// $fight7 = new Fight(); $fight7->setFighter1($this->getReference('fighter1')); $fight7->setFighter2($this->getReference('fighter2')); $fight7->setWinnerId($this->getReference('fighter1')->getId()); $fight7->setChampionship($this->getReference('championship4')); $fight7->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight7); $fight8 = new Fight(); $fight8->setFighter1($this->getReference('fighter3')); $fight8->setFighter2($this->getReference('fighter4')); $fight8->setWinnerId($this->getReference('fighter4')->getId()); $fight8->setChampionship($this->getReference('championship4')); $fight8->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight8); $fight9 = new Fight(); $fight9->setFighter1($this->getReference('fighter5')); $fight9->setFighter2($this->getReference('fighter6')); $fight9->setWinnerId($this->getReference('fighter6')->getId()); $fight9->setChampionship($this->getReference('championship4')); $fight9->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight9); $fight10 = new Fight(); $fight10->setFighter1($this->getReference('fighter7')); $fight10->setFighter2($this->getReference('fighter8')); $fight10->setWinnerId($this->getReference('fighter7')->getId()); $fight10->setChampionship($this->getReference('championship4')); $fight10->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight10); $fight11 = new Fight(); $fight11->setFighter1($this->getReference('fighter9')); $fight11->setFighter2($this->getReference('fighter10')); $fight11->setWinnerId($this->getReference('fighter10')->getId()); $fight11->setChampionship($this->getReference('championship4')); $fight11->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight11); $fight12 = new Fight(); $fight12->setFighter1($this->getReference('fighter11')); $fight12->setFighter2($this->getReference('fighter12')); $fight12->setWinnerId($this->getReference('fighter12')->getId()); $fight12->setChampionship($this->getReference('championship4')); $fight12->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight12); $fight13 = new Fight(); $fight13->setFighter1($this->getReference('fighter13')); $fight13->setFighter2($this->getReference('fighter14')); $fight13->setWinnerId($this->getReference('fighter14')->getId()); $fight13->setChampionship($this->getReference('championship4')); $fight13->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight13); $fight14 = new Fight(); $fight14->setFighter1($this->getReference('fighter15')); $fight14->setFighter2($this->getReference('fighter16')); $fight14->setWinnerId($this->getReference('fighter15')->getId()); $fight14->setChampionship($this->getReference('championship4')); $fight14->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight14); $fight15 = new Fight(); $fight15->setFighter1($this->getReference('fighter1')); $fight15->setFighter2($this->getReference('fighter4')); $fight15->setWinnerId($this->getReference('fighter4')->getId()); $fight15->setChampionship($this->getReference('championship4')); $fight15->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight15); $fight16 = new Fight(); $fight16->setFighter1($this->getReference('fighter6')); $fight16->setFighter2($this->getReference('fighter7')); $fight16->setWinnerId($this->getReference('fighter7')->getId()); $fight16->setChampionship($this->getReference('championship4')); $fight16->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight16); $fight17 = new Fight(); $fight17->setFighter1($this->getReference('fighter10')); $fight17->setFighter2($this->getReference('fighter12')); $fight17->setWinnerId($this->getReference('fighter12')->getId()); $fight17->setChampionship($this->getReference('championship4')); $fight17->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight17); $fight18 = new Fight(); $fight18->setFighter1($this->getReference('fighter14')); $fight18->setFighter2($this->getReference('fighter15')); $fight18->setWinnerId($this->getReference('fighter14')->getId()); $fight18->setChampionship($this->getReference('championship4')); $fight18->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight18); $fight19 = new Fight(); $fight19->setFighter1($this->getReference('fighter4')); $fight19->setFighter2($this->getReference('fighter7')); $fight19->setWinnerId($this->getReference('fighter7')->getId()); $fight19->setChampionship($this->getReference('championship4')); $fight19->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight19); $fight20 = new Fight(); $fight20->setFighter1($this->getReference('fighter12')); $fight20->setFighter2($this->getReference('fighter14')); $fight20->setWinnerId($this->getReference('fighter14')->getId()); $fight20->setChampionship($this->getReference('championship4')); $fight20->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight20); $fight21 = new Fight(); $fight21->setFighter1($this->getReference('fighter7')); $fight21->setFighter2($this->getReference('fighter14')); $fight21->setWinnerId($this->getReference('fighter7')->getId()); $fight21->setChampionship($this->getReference('championship4')); $fight21->setDate(new \DateTime("2016-07-23 06:12:33")); $manager->persist($fight21); $manager->flush(); $this->addReference('fight1', $fight1); $this->addReference('fight2', $fight2); $this->addReference('fight3', $fight3); $this->addReference('fight4', $fight4); $this->addReference('fight5', $fight5); $this->addReference('fight6', $fight6); $this->addReference('fight7', $fight7); $this->addReference('fight8', $fight8); $this->addReference('fight9', $fight9); $this->addReference('fight10', $fight10); $this->addReference('fight11', $fight11); $this->addReference('fight12', $fight12); $this->addReference('fight13', $fight13); $this->addReference('fight14', $fight14); $this->addReference('fight15', $fight15); $this->addReference('fight16', $fight16); $this->addReference('fight17', $fight17); $this->addReference('fight18', $fight18); $this->addReference('fight19', $fight19); $this->addReference('fight20', $fight20); $this->addReference('fight21', $fight21); }