Compare commits

..

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

3 changed files with 53 additions and 58 deletions

View File

@ -35,14 +35,14 @@ class GameController
private function prepareGame(int $mapId): array private function prepareGame(int $mapId): array
{ {
$map = $this->mapRepository->getById($mapId); $map = $this->mapRepository->getById($mapId);
$session = $this->request->session(); $session = $this->request->session();
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
$session->set('state', [ $session->set('state', [
'mapId' => $mapId, 'mapId' => $mapId,
'area' => $map->getArea(), 'area' => $map->getArea(),
'rounds' => [], 'rounds' => []
'currentRound' => -1
]); ]);
} }

View File

@ -30,22 +30,25 @@ class GameFlowController
return new JsonContent(['error' => 'no_session_found']); return new JsonContent(['error' => 'no_session_found']);
} }
if (!isset($state['currentRound']) || $state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS) { if (count($state['rounds']) === 0) {
$this->startNewGame($state, $mapId); $place = $this->selectNewPlace($state, $mapId);
}
$response = []; $session->set('state', $state);
$last = $state['rounds'][$state['currentRound']]; $data = [
$response['place'] = [ 'place' => [
'panoId' => $last['panoId'], 'panoId' => $place->getPanoIdCached(),
'pov' => $last['pov']->toArray() 'pov' => $place->getPov()->toArray()
]
]; ];
} else {
$rounds = count($state['rounds']);
$last = $state['rounds'][$rounds - 1];
$response['history'] = []; $history = [];
for ($i = 0; $i < $state['currentRound']; ++$i) { for ($i = 0; $i < $rounds - 1; ++$i) {
$round = $state['rounds'][$i]; $round = $state['rounds'][$i];
$response['history'][] = [ $history[] = [
'position' => $round['position']->toArray(), 'position' => $round['position']->toArray(),
'guessPosition' => $round['guessPosition']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(),
'distance' => $round['distance'], 'distance' => $round['distance'],
@ -53,7 +56,18 @@ class GameFlowController
]; ];
} }
return new JsonContent($response); $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]
]
];
}
return new JsonContent($data);
} }
public function evaluateGuess(): IContent public function evaluateGuess(): IContent
@ -65,7 +79,7 @@ class GameFlowController
return new JsonContent(['error' => 'no_session_found']); return new JsonContent(['error' => 'no_session_found']);
} }
$last = $state['rounds'][$state['currentRound']]; $last = $state['rounds'][count($state['rounds']) - 1];
$position = $last['position']; $position = $last['position'];
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng')); $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
@ -76,9 +90,7 @@ class GameFlowController
$last['guessPosition'] = $guessPosition; $last['guessPosition'] = $guessPosition;
$last['distance'] = $distance; $last['distance'] = $distance;
$last['score'] = $score; $last['score'] = $score;
$state['rounds'][count($state['rounds']) - 1] = $last;
$state['rounds'][$state['currentRound']] = $last;
$state['currentRound'] += 1;
$response = [ $response = [
'result' => [ 'result' => [
@ -88,13 +100,15 @@ class GameFlowController
] ]
]; ];
if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) { if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) {
$next = $state['rounds'][$state['currentRound']]; $place = $this->selectNewPlace($state, $mapId);
$response['place'] = [ $response['newPlace'] = [
'panoId' => $next['panoId'], 'panoId' => $place->getPanoIdCached(),
'pov' => $next['pov']->toArray() 'pov' => $place->getPov()->toArray()
]; ];
} else {
$state['rounds'] = [];
} }
$session->set('state', $state); $session->set('state', $state);
@ -102,23 +116,19 @@ class GameFlowController
return new JsonContent($response); return new JsonContent($response);
} }
private function startNewGame(array &$state, int $mapId) private function selectNewPlace(array &$state, int $mapId)
{ {
$places = $this->placeRepository->getRandomNForMapWithValidPano($mapId, static::NUMBER_OF_ROUNDS); $exclude = array_column($state['rounds'], 'placeId');
$place = $this->placeRepository->getRandomForMapWithValidPano($mapId, $exclude);
$state['rounds'] = [];
$state['currentRound'] = 0;
foreach ($places as $place) {
$state['rounds'][] = [ $state['rounds'][] = [
'placeId' => $place->getId(), 'placeId' => $place->getId(),
'position' => $place->getPosition(), 'position' => $place->getPosition(),
'panoId' => $place->getPanoIdCached(), 'panoId' => $place->getPanoIdCached(),
'pov' => $place->getPov() 'pov' => $place->getPov()
]; ];
}
$this->request->session()->set('state', $state); return $place;
} }
private function calculateDistance(Position $realPosition, Position $guessPosition): float private function calculateDistance(Position $realPosition, Position $guessPosition): float

View File

@ -28,21 +28,6 @@ class PlaceRepository
yield from $this->pdm->selectMultipleFromDb($select, Place::class); yield from $this->pdm->selectMultipleFromDb($select, Place::class);
} }
//TODO: use Map instead of id
public function getRandomNForMapWithValidPano(int $mapId, int $n, array $exclude = []): array
{
$places = [];
for ($i = 1; $i <= $n; ++$i) {
$place = $this->getRandomForMapWithValidPano($mapId, $exclude);
$places[] = $place;
$exclude[] = $place->getId();
}
return $places;
}
//TODO: use Map instead of id //TODO: use Map instead of id
public function getRandomForMapWithValidPano(int $mapId, array $exclude = []): Place public function getRandomForMapWithValidPano(int $mapId, array $exclude = []): Place
{ {