Compare commits

..

No commits in common. "01f0f7d4bca34ef742a0ff5d04bd410baf053c2c" and "bf27f15709d706adc3b2fa43f2ed5e81d301cf62" have entirely different histories.

4 changed files with 51 additions and 45 deletions

View File

@ -26,7 +26,7 @@
});
Game.map.fitBounds(mapBounds);
MapGuesser.httpRequest('GET', '/game/' + mapId + '/initialData.json', function () {
MapGuesser.httpRequest('GET', '/game/' + mapId + '/newPlace.json', function () {
document.getElementById('loading').style.visibility = 'hidden';
document.getElementById('panoCover').style.visibility = 'hidden';
@ -35,8 +35,8 @@
return;
}
Game.panoId = this.response.place.panoId;
Game.pov = this.response.place.pov;
Game.panoId = this.response.panoId;
Game.pov = this.response.pov;
if (this.response.history) {
for (var i = 0; i < this.response.history.length; ++i) {
@ -200,13 +200,8 @@
document.getElementById('showSummaryButton').style.display = 'block';
}
if (this.response.newPlace) {
Game.panoId = this.response.newPlace.panoId;
Game.pov = this.response.newPlace.pov;
} else {
Game.panoId = null;
Game.pov = null;
}
Game.panoId = this.response.panoId;
Game.pov = this.response.pov;
}, data);
},

View File

@ -4,6 +4,7 @@ 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
@ -21,9 +22,10 @@ class GameFlowController
$this->placeRepository = new PlaceRepository();
}
public function getInitialData(): IContent
public function getNewPlace(): IContent
{
$mapId = (int) $this->request->query('mapId');
$session = $this->request->session();
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
@ -31,15 +33,16 @@ class GameFlowController
}
if (count($state['rounds']) === 0) {
$place = $this->selectNewPlace($state, $mapId);
$placesWithoutPano = [];
$place = $this->placeRepository->getRandomForMapWithValidPano($mapId, [], $placesWithoutPano);
$this->addNewRoundToState($state, $place, $placesWithoutPano);
$session->set('state', $state);
$data = [
'place' => [
'panoId' => $place->getPanoIdCached(),
'pov' => $place->getPov()->toArray()
]
];
} else {
$rounds = count($state['rounds']);
@ -58,12 +61,10 @@ class GameFlowController
$data = [
'history' => $history,
'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]
]
['heading' => 0.0, 'pitch' => 0.0, 'zoom' => 0.0],
];
}
@ -73,6 +74,7 @@ class GameFlowController
public function evaluateGuess(): IContent
{
$mapId = (int) $this->request->query('mapId');
$session = $this->request->session();
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
@ -92,43 +94,49 @@ class GameFlowController
$last['score'] = $score;
$state['rounds'][count($state['rounds']) - 1] = $last;
$response = [
'result' => [
'position' => $position->toArray(),
'distance' => $distance,
'score' => $score
]
];
if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) {
$place = $this->selectNewPlace($state, $mapId);
$exclude = [];
$response['newPlace'] = [
'panoId' => $place->getPanoIdCached(),
'pov' => $place->getPov()->toArray()
];
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);
return new JsonContent([
'result' => [
'position' => $position->toArray(),
'distance' => $distance,
'score' => $score
],
'panoId' => $panoId,
'pov' => $pov
]);
}
private function selectNewPlace(array &$state, int $mapId)
private function addNewRoundToState(array &$state, Place $place, array $placesWithoutPano): void
{
$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

View File

@ -29,14 +29,17 @@ class PlaceRepository
}
//TODO: use Map instead of id
public function getRandomForMapWithValidPano(int $mapId, array $exclude = []): Place
public function getRandomForMapWithValidPano(int $mapId, array $exclude = [], array &$placesWithoutPano): Place
{
$placesWithoutPano = [];
do {
$place = $this->selectRandomFromDbForMap($mapId, $exclude);
$panoId = $place->getFreshPanoId();
if ($panoId === null) {
$exclude[] = $place->getId();
$placesWithoutPano[] = $exclude[] = $place->getId();
}
} while ($panoId === null);

View File

@ -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('initialData-json', '{mapId}/initialData.json', [MapGuesser\Controller\GameFlowController::class, 'getInitialData']);
$routeCollection->get('newPlace-json', '{mapId}/newPlace.json', [MapGuesser\Controller\GameFlowController::class, 'getNewPlace']);
$routeCollection->post('guess-json', '{mapId}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'evaluateGuess']);
});
Container::$routeCollection->group('admin', function (MapGuesser\Routing\RouteCollection $routeCollection) {