diff --git a/src/Controller/GameController.php b/src/Controller/GameController.php index c1174f4..246f9b8 100644 --- a/src/Controller/GameController.php +++ b/src/Controller/GameController.php @@ -1,51 +1,48 @@ mapId = $mapId; - $this->jsonResponse = $jsonResponse; + $mapId = (int) $parameters['mapId']; + $data = $this->prepareGame($mapId); + return new HtmlContent('game', $data); } - public function run(): IView + public function getGameJson(array $parameters): IContent { - $bounds = $this->getMapBounds(); + $mapId = (int) $parameters['mapId']; + $data = $this->prepareGame($mapId); + return new JsonContent($data); + } - if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) { + private function prepareGame(int $mapId) + { + $bounds = $this->getMapBounds($mapId); + + if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { $_SESSION['state'] = [ - 'mapId' => $this->mapId, + 'mapId' => $mapId, 'area' => $bounds->calculateApproximateArea(), 'rounds' => [] ]; } - $data = ['mapId' => $this->mapId, 'bounds' => $bounds->toArray()]; - - if ($this->jsonResponse) { - return new JsonView($data); - } else { - return new HtmlView('game', $data); - } + return ['mapId' => $mapId, 'bounds' => $bounds->toArray()]; } - private function getMapBounds(): Bounds + private function getMapBounds(int $mapId): Bounds { $select = new Select(\Container::$dbConnection, 'maps'); $select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']); - $select->whereId($this->mapId); + $select->whereId($mapId); $map = $select->execute()->fetch(IResultSet::FETCH_ASSOC); diff --git a/src/Controller/MapsController.php b/src/Controller/MapsController.php index 569d667..46aa8b3 100644 --- a/src/Controller/MapsController.php +++ b/src/Controller/MapsController.php @@ -2,15 +2,14 @@ use MapGuesser\Database\Query\Select; use MapGuesser\Database\RawExpression; -use MapGuesser\Interfaces\Controller\IController; use MapGuesser\Interfaces\Database\IResultSet; -use MapGuesser\Interfaces\View\IView; +use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Util\Geo\Bounds; -use MapGuesser\View\HtmlView; +use MapGuesser\Response\HtmlContent; -class MapsController implements IController +class MapsController { - public function run(): IView + public function getMaps(): IContent { $select = new Select(\Container::$dbConnection, 'maps'); $select->columns([ @@ -38,7 +37,7 @@ class MapsController implements IController } $data = ['maps' => $maps]; - return new HtmlView('maps', $data); + return new HtmlContent('maps', $data); } private function formatMapAreaForHuman(float $area): array diff --git a/src/Controller/PositionController.php b/src/Controller/PositionController.php index cddee8d..e248446 100644 --- a/src/Controller/PositionController.php +++ b/src/Controller/PositionController.php @@ -2,75 +2,30 @@ use MapGuesser\Database\Query\Select; use MapGuesser\Http\Request; -use MapGuesser\Interfaces\Controller\IController; use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\Util\Geo\Position; -use MapGuesser\View\JsonView; -use MapGuesser\Interfaces\View\IView; +use MapGuesser\Response\JsonContent; +use MapGuesser\Interfaces\Response\IContent; -class PositionController implements IController +class PositionController { const NUMBER_OF_ROUNDS = 5; const MAX_SCORE = 1000; - private int $mapId; - - public function __construct(int $mapId) + public function getPosition(array $parameters): IContent { - $this->mapId = $mapId; - } + $mapId = (int) $parameters['mapId']; - public function run(): IView - { - if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) { + if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { $data = ['error' => 'No valid session found!']; - return new JsonView($data); + return new JsonContent($data); } if (count($_SESSION['state']['rounds']) === 0) { - $newPosition = $this->getNewPosition(); + $newPosition = $this->getNewPosition($mapId); $_SESSION['state']['rounds'][] = $newPosition; $data = ['panoId' => $newPosition['panoId']]; - } elseif (isset($_POST['guess'])) { - $last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1]; - - $position = $last['position']; - $guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']); - - $last['guessPosition'] = $guessPosition; - - $distance = $this->calculateDistance($position, $guessPosition); - $score = $this->calculateScore($distance, $_SESSION['state']['area']); - - $last['distance'] = $distance; - $last['score'] = $score; - - if (count($_SESSION['state']['rounds']) < static::NUMBER_OF_ROUNDS) { - $exclude = []; - - foreach ($_SESSION['state']['rounds'] as $round) { - $exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]); - } - - $newPosition = $this->getNewPosition($exclude); - $_SESSION['state']['rounds'][] = $newPosition; - - $panoId = $newPosition['panoId']; - } else { - $_SESSION['state']['rounds'] = []; - - $panoId = null; - } - - $data = [ - 'result' => [ - 'position' => $position->toArray(), - 'distance' => $distance, - 'score' => $score - ], - 'panoId' => $panoId - ]; } else { $rounds = count($_SESSION['state']['rounds']); $last = $_SESSION['state']['rounds'][$rounds - 1]; @@ -92,15 +47,65 @@ class PositionController implements IController ]; } - return new JsonView($data); + return new JsonContent($data); } - private function getNewPosition(array $exclude = []): array + public function evaluateGuess(array $parameters): IContent + { + $mapId = (int) $parameters['mapId']; + + if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { + $data = ['error' => 'No valid session found!']; + return new JsonContent($data); + } + + $last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1]; + + $position = $last['position']; + $guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']); + + $last['guessPosition'] = $guessPosition; + + $distance = $this->calculateDistance($position, $guessPosition); + $score = $this->calculateScore($distance, $_SESSION['state']['area']); + + $last['distance'] = $distance; + $last['score'] = $score; + + if (count($_SESSION['state']['rounds']) < static::NUMBER_OF_ROUNDS) { + $exclude = []; + + foreach ($_SESSION['state']['rounds'] as $round) { + $exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]); + } + + $newPosition = $this->getNewPosition($mapId, $exclude); + $_SESSION['state']['rounds'][] = $newPosition; + + $panoId = $newPosition['panoId']; + } else { + $_SESSION['state']['rounds'] = []; + + $panoId = null; + } + + $data = [ + 'result' => [ + 'position' => $position->toArray(), + 'distance' => $distance, + 'score' => $score + ], + 'panoId' => $panoId + ]; + return new JsonContent($data); + } + + private function getNewPosition(int $mapId, array $exclude = []): array { $placesWithoutPano = []; do { - $place = $this->selectNewPlace($exclude); + $place = $this->selectNewPlace($mapId, $exclude); $position = new Position($place['lat'], $place['lng']); $panoId = $this->getPanorama($position); @@ -117,12 +122,12 @@ class PositionController implements IController ]; } - private function selectNewPlace(array $exclude): array + private function selectNewPlace(int $mapId, array $exclude): array { $select = new Select(\Container::$dbConnection, 'places'); $select->columns(['id', 'lat', 'lng']); $select->where('id', 'NOT IN', $exclude); - $select->where('map_id', '=', $this->mapId); + $select->where('map_id', '=', $mapId); $numberOfPlaces = $select->count();// TODO: what if 0 $randomOffset = random_int(0, $numberOfPlaces - 1);