public function submitAction($input) { try { $coordinate = $this->inputHandler->handle($input); } catch (NoInputException $e) { return $this->render($this->formatter->format()); } catch (DebugException $e) { return $this->render($this->formatter->format(true)); } catch (ValidationException $e) { return $this->render($this->formatter->format(), ConsoleOutputManager::ERROR_MESSAGE); } $hasHit = $this->shootManager->shoot($coordinate); if (!$hasHit) { return $this->render($this->formatter->format(), ConsoleOutputManager::MISS_MESSAGE); } if ($this->shootManager->isBattlefieldDestroyed()) { $shootNumber = $this->shootManager->getShotsCount(); return $this->render($this->formatter->format(), ConsoleOutputManager::SUNK_MESSAGE, $shootNumber); } $ship = $this->battlefield->getFleet()->getShip($coordinate); if ($this->shootManager->isShipSunk($ship)) { return $this->render($this->formatter->format(), ConsoleOutputManager::SUNK_MESSAGE); } return $this->render($this->formatter->format(), ConsoleOutputManager::HIT_MESSAGE); }
public function indexAction(Request $request) { /** @var Session $session */ $session = $request->getSession(); $this->battlefieldCreator->createFromSession($session); $input = $request->get('coord'); try { $coordinate = $this->inputHandler->handle($input); } catch (NoInputException $e) { return $this->render('View/index.html', array('battlefield' => $this->formatter->format())); } catch (DebugException $e) { return $this->render('View/index.html', array('battlefield' => $this->formatter->format(true))); } catch (ValidationException $e) { $session->getFlashBag()->add('notice', ConsoleOutputManager::ERROR_MESSAGE); return $this->render('View/index.html', array('battlefield' => $this->formatter->format())); } $hasHit = $this->shootManager->shoot($coordinate); if (!$hasHit) { $session->getFlashBag()->add('notice', ConsoleOutputManager::MISS_MESSAGE); return $this->render('View/index.html', array('battlefield' => $this->formatter->format())); } if ($this->shootManager->isBattlefieldDestroyed()) { $session->set('gameFinished', true); $shotsNumber = $this->shootManager->getShotsCount(); return $this->render('View/finish.html', array('shots' => $shotsNumber)); } $ship = $this->battlefield->getFleet()->getShip($coordinate); if ($this->shootManager->isShipSunk($ship)) { $session->getFlashBag()->add('notice', ConsoleOutputManager::SUNK_MESSAGE); return $this->render('View/index.html', array('battlefield' => $this->formatter->format())); } $session->getFlashBag()->add('notice', ConsoleOutputManager::HIT_MESSAGE); return $this->render('View/index.html', array('battlefield' => $this->formatter->format())); }
public function createFromSession(Session $session) { if ($session->has('fleet')) { $fleet = unserialize($session->get('fleet')); $shots = unserialize($session->get('shots')); $hits = unserialize($session->get('hits')); $this->battlefield->setFleet($fleet); $this->shotsManager->setAllShots($shots); $this->shotsManager->setHits($hits); } $this->create(); }
public function onPostController(ResponseEvent $event) { $session = $event->getSession(); if ($session->has('gameFinished')) { $session->clear(); return; } $fleet = $this->battlefield->getFleet(); $shots = $this->shotsManager->getAllShots(); $hits = $this->shotsManager->getHits(); $session->set('fleet', serialize($fleet)); $session->set('shots', serialize($shots)); $session->set('hits', serialize($hits)); }
private function getElementSign(Coordinate $coordinate, $debug = false) { if ($debug) { if ($this->battlefield->getFleet()->hasShip($coordinate)) { if (!$this->shootManager->hasHit($coordinate)) { return self::HIT; } } return self::EMPTY_FIELD; } if ($this->shootManager->hasShot($coordinate)) { if ($this->shootManager->hasHit($coordinate)) { return self::HIT; } return self::MISS; } return self::NOT_SHOOT; }