MAPG-203 refactor GameFlowController

This commit is contained in:
Bence Pőcze 2021-03-15 12:28:06 +01:00
parent 60e20bd92b
commit 9c4b7448a9
Signed by: bence
GPG Key ID: AA52B11A3269D1C1

View File

@ -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