From 2c4d809d49df6db7e9ad483d6dfa189b3b1a7747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 15 Mar 2021 12:25:49 +0100 Subject: [PATCH 1/4] MAPG-203 remove placesWithoutPano logic from PlaceRepository --- src/Repository/PlaceRepository.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index 0a2a197..e12cb0b 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -29,17 +29,14 @@ class PlaceRepository } //TODO: use Map instead of id - public function getRandomForMapWithValidPano(int $mapId, array $exclude = [], array &$placesWithoutPano): Place + public function getRandomForMapWithValidPano(int $mapId, array $exclude = []): Place { - $placesWithoutPano = []; - do { $place = $this->selectRandomFromDbForMap($mapId, $exclude); $panoId = $place->getFreshPanoId(); - if ($panoId === null) { - $placesWithoutPano[] = $exclude[] = $place->getId(); + $exclude[] = $place->getId(); } } while ($panoId === null); -- 2.45.2 From 60e20bd92b90130ada34a11f356453c3c6dd978c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 15 Mar 2021 12:26:15 +0100 Subject: [PATCH 2/4] MAPG-203 rename game/newPlace endpoint --- web.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web.php b/web.php index 920ad90..7357324 100644 --- a/web.php +++ b/web.php @@ -51,7 +51,7 @@ Container::$routeCollection->group('account', function (MapGuesser\Routing\Route Container::$routeCollection->group('game', function (MapGuesser\Routing\RouteCollection $routeCollection) { $routeCollection->get('game', '{mapId}', [MapGuesser\Controller\GameController::class, 'getGame']); $routeCollection->get('game-json', '{mapId}/json', [MapGuesser\Controller\GameController::class, 'getGameJson']); - $routeCollection->get('newPlace-json', '{mapId}/newPlace.json', [MapGuesser\Controller\GameFlowController::class, 'getNewPlace']); + $routeCollection->get('initialData-json', '{mapId}/initialData.json', [MapGuesser\Controller\GameFlowController::class, 'getInitialData']); $routeCollection->post('guess-json', '{mapId}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'evaluateGuess']); }); Container::$routeCollection->group('admin', function (MapGuesser\Routing\RouteCollection $routeCollection) { -- 2.45.2 From 9c4b7448a9635f3ae9c5f472a3400d4938c90349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 15 Mar 2021 12:28:06 +0100 Subject: [PATCH 3/4] MAPG-203 refactor GameFlowController --- src/Controller/GameFlowController.php | 80 ++++++++++++--------------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/src/Controller/GameFlowController.php b/src/Controller/GameFlowController.php index b414eb0..d6c446a 100644 --- a/src/Controller/GameFlowController.php +++ b/src/Controller/GameFlowController.php @@ -4,7 +4,6 @@ use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Util\Geo\Position; use MapGuesser\Response\JsonContent; use MapGuesser\Interfaces\Response\IContent; -use MapGuesser\PersistentData\Model\Place; use MapGuesser\Repository\PlaceRepository; class GameFlowController @@ -22,10 +21,9 @@ class GameFlowController $this->placeRepository = new PlaceRepository(); } - public function getNewPlace(): IContent + public function getInitialData(): IContent { $mapId = (int) $this->request->query('mapId'); - $session = $this->request->session(); if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { @@ -33,16 +31,15 @@ class GameFlowController } if (count($state['rounds']) === 0) { - $placesWithoutPano = []; - $place = $this->placeRepository->getRandomForMapWithValidPano($mapId, [], $placesWithoutPano); - - $this->addNewRoundToState($state, $place, $placesWithoutPano); + $place = $this->selectNewPlace($state, $mapId); $session->set('state', $state); $data = [ - 'panoId' => $place->getPanoIdCached(), - 'pov' => $place->getPov()->toArray() + 'place' => [ + 'panoId' => $place->getPanoIdCached(), + 'pov' => $place->getPov()->toArray() + ] ]; } else { $rounds = count($state['rounds']); @@ -61,10 +58,12 @@ class GameFlowController $data = [ 'history' => $history, - 'panoId' => $last['panoId'], - 'pov' => isset($last['pov']) ? // should be checked not to break with old sessions - $last['pov']->toArray() : - ['heading' => 0.0, 'pitch' => 0.0, 'zoom' => 0.0], + 'place' => [ + 'panoId' => $last['panoId'], + 'pov' => isset($last['pov']) ? // should be checked not to break with old sessions + $last['pov']->toArray() : + ['heading' => 0.0, 'pitch' => 0.0, 'zoom' => 0.0] + ] ]; } @@ -74,7 +73,6 @@ class GameFlowController public function evaluateGuess(): IContent { $mapId = (int) $this->request->query('mapId'); - $session = $this->request->session(); if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { @@ -94,49 +92,43 @@ class GameFlowController $last['score'] = $score; $state['rounds'][count($state['rounds']) - 1] = $last; - if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) { - $exclude = []; - - foreach ($state['rounds'] as $round) { - $exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]); - } - - $placesWithoutPano = []; - $place = $this->placeRepository->getRandomForMapWithValidPano($mapId, $exclude, $placesWithoutPano); - - $this->addNewRoundToState($state, $place, $placesWithoutPano); - - $panoId = $place->getPanoIdCached(); - $pov = $place->getPov()->toArray(); - } else { - $state['rounds'] = []; - - $panoId = null; - $pov = null; - } - - $session->set('state', $state); - - return new JsonContent([ + $response = [ 'result' => [ 'position' => $position->toArray(), 'distance' => $distance, 'score' => $score - ], - 'panoId' => $panoId, - 'pov' => $pov - ]); + ] + ]; + + if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) { + $place = $this->selectNewPlace($state, $mapId); + + $response['newPlace'] = [ + 'panoId' => $place->getPanoIdCached(), + 'pov' => $place->getPov()->toArray() + ]; + } else { + $state['rounds'] = []; + } + + $session->set('state', $state); + + return new JsonContent($response); } - private function addNewRoundToState(array &$state, Place $place, array $placesWithoutPano): void + private function selectNewPlace(array &$state, int $mapId) { + $exclude = array_column($state['rounds'], 'placeId'); + $place = $this->placeRepository->getRandomForMapWithValidPano($mapId, $exclude); + $state['rounds'][] = [ - 'placesWithoutPano' => $placesWithoutPano, 'placeId' => $place->getId(), 'position' => $place->getPosition(), 'panoId' => $place->getPanoIdCached(), 'pov' => $place->getPov() ]; + + return $place; } private function calculateDistance(Position $realPosition, Position $guessPosition): float -- 2.45.2 From c317a0abf42bbec6c5e02c9c0864070e7500102f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 15 Mar 2021 12:28:25 +0100 Subject: [PATCH 4/4] MAPG-203 adapt game.js to the new endpoints --- public/static/js/game.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/public/static/js/game.js b/public/static/js/game.js index de9098b..f02d1f8 100644 --- a/public/static/js/game.js +++ b/public/static/js/game.js @@ -26,7 +26,7 @@ }); Game.map.fitBounds(mapBounds); - MapGuesser.httpRequest('GET', '/game/' + mapId + '/newPlace.json', function () { + MapGuesser.httpRequest('GET', '/game/' + mapId + '/initialData.json', function () { document.getElementById('loading').style.visibility = 'hidden'; document.getElementById('panoCover').style.visibility = 'hidden'; @@ -35,8 +35,8 @@ return; } - Game.panoId = this.response.panoId; - Game.pov = this.response.pov; + Game.panoId = this.response.place.panoId; + Game.pov = this.response.place.pov; if (this.response.history) { for (var i = 0; i < this.response.history.length; ++i) { @@ -200,8 +200,13 @@ document.getElementById('showSummaryButton').style.display = 'block'; } - Game.panoId = this.response.panoId; - Game.pov = this.response.pov; + if (this.response.newPlace) { + Game.panoId = this.response.newPlace.panoId; + Game.pov = this.response.newPlace.pov; + } else { + Game.panoId = null; + Game.pov = null; + } }, data); }, -- 2.45.2