/** * @Route("/wotr/create/{id}") * @param Scenario $scenario * @return Response */ public function createGameAction(Scenario $scenario) { // Get the entity manager $em = $this->getDoctrine()->getManager(); // Create new game $game = new Game(); $em->persist($game); // Attach the scenario $game->setScenario($scenario); $as = new ActionStack(); $as->setGame($game); $log = new Log(); $log->setGame($game); $game->setPhase($em->getRepository('WotRBundle:Phase')->find(1)); // Set up the starting nations $startNations = $em->getRepository('WotRBundle:NationDetails')->findAll(); foreach ($startNations as $sN) { new Nation($sN, $game); } $em->flush(); $fs = new Fellowship(); // Set up the board $startLocs = $em->getRepository('WotRBundle:LocationDetails')->findAll(); // Add locations - saving the FS Location to be added later foreach ($startLocs as $sL) { $location = new Location($sL, $game); if ($sL == $scenario->getFSStart()) { $FSLoc = $location; $fs->setLocation($FSLoc); } } $em->flush(); // Can't add connections until all locations initialises /** @var Location $sL */ foreach ($startLocs as $sL) { /** @var ConnectionDetails $cD */ foreach ($sL->getConnections() as $cD) { $c = new Connection(); $source = $game->getLocations()->filter(function ($l) use($cD) { return $l->getDetails() == $cD->getSource(); })->first(); $dest = $game->getLocations()->filter(function ($l) use($cD) { return $l->getDetails() == $cD->getDestination(); })->first(); $c->setSource($source); $c->setDestination($dest); $c->setStronghold($cD->getStronghold()); } } // Set up the fellowship and location $fs->setCorruption($scenario->getCorruption()); $em->flush(); // Set up the characters and add guide to fellowship. $startChars = $em->getRepository('WotRBundle:CharacterDetails')->getStartCharacters($scenario); $charUnitType = $em->getRepository('WotRBundle:UnitType')->findOneById(4); /* @var $sC CharacterDetails */ foreach ($startChars as $sC) { $cN = $game->getNations()->filter(function ($n) use($sC) { return $sC->getNationDetails()->first() && $n->getDetails()->getId() == $sC->getNationDetails()->first()->getId(); })->first(); if (!$cN) { $cN = NULL; } if ($sC->getStartloc()) { $cL = $game->getLocations()->filter(function ($l) use($sC) { return $l->getDetails() == $sC->getStartloc(); })->first(); } else { $cL = NULL; } $c = new Character($cN, $charUnitType, $cL, $game, $sC); if ($sC->getStartInFellowship()) { $c->setFellowship($fs); } if ($sC == $scenario->getStartGuide()) { $startGuide = $c; $fs->setGuide($startGuide); } } // Add fellowship to game $fs->setGame($game); $em->flush(); // Set up the rings and add to game $sides = $em->getRepository('WotRBundle:Side')->getPlayedSides(); foreach ($sides as $side) { $eR = new ElvenRings($side, $scenario->getStartRings($side), $game); $player = new Player($em->getRepository('UserBundle:User')->find(3), $side, $game); } // Set up the card decks and hunt tiles $cardGroupDetails = $em->getRepository('WotRBundle:CardGroupDetails')->findAll(); /* @var $cGD CardGroupDetails */ foreach ($cardGroupDetails as $cGD) { $cG = new CardGroup($cGD, $game); } $em->flush(); // Add cards to groups $cardEntryDetails = $em->getRepository('WotRBundle:CardEntryDetails')->getStartCards($scenario); /* @var $cED CardEntryDetails */ foreach ($cardEntryDetails as $cED) { if ($cED->getTurn() == 1) { $cG = $game->getCardGroupByID($cED->getStartGroup()->getId()); for ($i = 1; $i <= $cED->getCopies(); $i++) { $card = new Card($cED->getCardDetails()); $cG->addToBottom($card); } } } // Shuffle groups $cGs = $game->getCardGroups(); foreach ($cGs as $cG) { $cG->shuffle(); } $em->flush(); // Set up units $startUnits = $em->getRepository('WotRBundle:UnitEntryDetails')->getStartUnits($scenario); $locations = $game->getLocations(); /* @var $sU UnitEntryDetails */ foreach ($startUnits as $sU) { $startLocDetail = $sU->getStartLoc(); $startLoc = $locations->filter(function ($entry) use($startLocDetail) { return $entry->getDetails() == $startLocDetail; })->first(); if ($startLoc === false) { $startLoc = NULL; } $unitNationDetail = $sU->getNationDetails(); $unitNation = $game->getNations()->filter(function ($entry) use($unitNationDetail) { return $entry->getDetails() == $unitNationDetail; })->first(); for ($i = 0; $i < $sU->getNumber(); $i++) { $unit = new Unit($unitNation, $sU->getUnitType(), $startLoc, $game); } } $game->setCurrentSide($game->getSide('FP')); try { $em->flush(); } catch (\Exception $e) { echo "<pre>Error: {$e}</pre>"; } return $this->redirect($this->generateUrl('meldon_wotr_wotr_index', array('id' => $game->getID()))); }
/** * Set fellowship * * @param \Meldon\WotRBundle\Entity\Fellowship $fellowship * @return Character */ public function setFellowship(\Meldon\WotRBundle\Entity\Fellowship $fellowship = null) { $this->fellowship = $fellowship; $fellowship->addCharacter($this); return $this; }