コード例 #1
0
 /**
  * Creates a new Battleship entity.
  *
  * @Route("/new/{game}", name="battleship_new")
  * @Method({"GET", "POST"})
  */
 public function newAction(Request $request, Game $game)
 {
     $battleship = new Battleship();
     $battleship->setType('player');
     $battleship->setHit('0');
     $battleship->setSink(false);
     $battleship->setGame($game);
     $horizon = rand(0, 1);
     $battleshipIa = new Battleship();
     $battleshipIa->setGame($game);
     $battleshipIa->setHorizontal($horizon);
     $battleshipIa->setType('ia');
     $battleshipIa->setHit(0);
     $battleshipIa->setSink(false);
     $battleshipIaInclinaison = $battleshipIa->getHorizontal();
     $battleshipIaLength = $battleshipIa->getLength();
     $battleshipIaSize = 10 - $battleshipIaLength;
     if ($battleshipIaInclinaison) {
         $coordX = random_int(1, $battleshipIaSize);
         $coordY = random_int(1, 10);
     } else {
         $coordX = random_int(1, 10);
         $coordY = random_int(1, $battleshipIaSize);
     }
     $battleshipIa->setCoordx($coordX);
     $battleshipIa->setCoordy($coordY);
     $form = $this->createForm('BattleBundle\\Form\\BattleshipType', $battleship);
     $form->handleRequest($request);
     $battleshipName = $battleship->getName();
     $battleshipIa->setName($battleship->getName());
     $battleshipIaName = $battleshipIa->getName();
     if ($battleshipName == 'porte-avion' || $battleshipIaName == 'porte-avion') {
         $battleship->setLength(5);
         $battleshipIa->setLength(5);
     } elseif ($battleshipName == 'torpilleur' || $battleshipIaName == 'torpilleur') {
         $battleship->setLength(2);
         $battleshipIa->setLength(2);
     } else {
         $battleship->setLength(3);
         $battleshipIa->setLength(3);
     }
     $user = $this->container->get('security.context')->getToken()->getUser();
     $battleshipLength = $battleship->getLength();
     $battleshipCoordX = $battleship->getCoordx();
     $battleshipCoordY = $battleship->getCoordy();
     $battleshipTilt = $battleship->getHorizontal();
     $battleshipSizeX = $battleshipLength + $battleshipCoordX;
     $battleshipSizeY = $battleshipLength + $battleshipCoordY;
     $em = $this->getDoctrine()->getManager();
     $tooManyBoat = $em->getRepository('BattleBundle:Battleship')->checkBattleshipName($game, $battleship);
     $isTooBig = false;
     $battleshipCount = false;
     if ($form->isSubmitted() && $form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $battleshipCount = $em->getRepository('BattleBundle:Battleship')->countBattleship($game);
         if ($battleshipTilt == true && $battleshipSizeX < 9 || $battleshipTilt == false && $battleshipSizeY < 9) {
             if (!$tooManyBoat) {
                 $em->persist($battleship);
                 $em->persist($battleshipIa);
                 $em->flush();
             }
         } else {
             $isTooBig = true;
         }
         if ($battleshipCount && $tooManyBoat) {
             return $this->redirectToRoute('game_show', array('id' => $game->getId()));
         }
     }
     $em = $this->getDoctrine()->getManager();
     $battleships = $em->getRepository('BattleBundle:Battleship')->findAllByGameType($game);
     return $this->render('battleship/new.html.twig', array('battleship' => $battleship, 'battleships' => $battleships, 'isTooBig' => $isTooBig, 'tooManyBoat' => $tooManyBoat, 'battleshipCount' => $battleshipCount, 'user' => $user, 'game' => $game, 'form' => $form->createView()));
 }