/** * @param Event $shotEvent * @return string miss|hit|sunk * @throws UnexpectedEventTypeException */ public function getShotResult(Event $shotEvent) { if ($shotEvent->getType() !== Event::TYPE_SHOT) { throw new UnexpectedEventTypeException($shotEvent->getType(), Event::TYPE_SHOT); } $game = $shotEvent->getGame(); $enemyShipsCollection = new CoordsCollection($game->getOtherShips()); $shotCoord = $shotEvent->getValue(); if ($enemyShipsCollection->contains($shotCoord)) { $attackerShots = $this->getAttackerShots($shotEvent); $result = $this->isSunk($shotCoord, $enemyShipsCollection, $attackerShots) ? self::SHOT_RESULT_SUNK : self::SHOT_RESULT_HIT; } else { $result = self::SHOT_RESULT_MISS; } return $result; }
/** * Example response:<pre> * {"timestamp":"2016-11-11T16:21:16+0000"} # for chat * {"result":"miss"} # for shot</pre> * * @ApiDoc( * resource=true, * description="Create new event", * section="Event", * statusCodes={ * 201="Event created", * 400="Incorrect parameter provided", * 404="Game not found", * 409="Action not allow due to game flow restrictions" * } * ) * * @Tag(expression="'game-' ~ game.getId() ~ 'events'") * @Security("game.belongsToUser(user)") * @RequestParam( * name="type", * requirements=@Assert\Choice(callback = {"AppBundle\Entity\Event", "getTypes"}) * ) * @RequestParam(name="value", requirements=".*\S.*", allowBlank=false, default=true) * * @param ParamFetcher $paramFetcher * @param Game $game * @return Response */ public function postEventAction(ParamFetcher $paramFetcher, Game $game) { $event = new Event(); $event->setGame($game)->setPlayer($game->getPlayerNumber())->setType($paramFetcher->get('type'))->setValue($paramFetcher->get('value')); $data = null; switch ($event->getType()) { case Event::TYPE_CHAT: $event->applyCurrentTimestamp(); $data = ['timestamp' => $event->getTimestamp()]; break; case Event::TYPE_SHOT: $shotResult = $this->battleManager->getShotResult($event); $event->setValue([$event->getValue(), $shotResult]); $data = ['result' => $shotResult]; break; } $this->entityManager->persist($event); $this->entityManager->flush(); $view = $this->routeRedirectView('api_v1_get_game_event', ['game' => $game->getId(), 'event' => $event->getId()])->setData($data); return $this->handleView($view); }