MAPG-203 select places at the beginning of the game #6

Merged
bence merged 1 commits from feature/MAPG-203-initial-multiplayer-implementation into develop 2021-03-17 22:48:44 +01:00
3 changed files with 58 additions and 53 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,44 +30,30 @@ class GameFlowController
return new JsonContent(['error' => 'no_session_found']); return new JsonContent(['error' => 'no_session_found']);
} }
if (count($state['rounds']) === 0) { if (!isset($state['currentRound']) || $state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS) {
$place = $this->selectNewPlace($state, $mapId); $this->startNewGame($state, $mapId);
}
$session->set('state', $state); $response = [];
$data = [ $last = $state['rounds'][$state['currentRound']];
'place' => [ $response['place'] = [
'panoId' => $place->getPanoIdCached(), 'panoId' => $last['panoId'],
'pov' => $place->getPov()->toArray() 'pov' => $last['pov']->toArray()
] ];
];
} else {
$rounds = count($state['rounds']);
$last = $state['rounds'][$rounds - 1];
$history = []; $response['history'] = [];
for ($i = 0; $i < $rounds - 1; ++$i) { for ($i = 0; $i < $state['currentRound']; ++$i) {
$round = $state['rounds'][$i]; $round = $state['rounds'][$i];
$history[] = [ $response['history'][] = [
'position' => $round['position']->toArray(), 'position' => $round['position']->toArray(),
'guessPosition' => $round['guessPosition']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(),
'distance' => $round['distance'], 'distance' => $round['distance'],
'score' => $round['score'] 'score' => $round['score']
];
}
$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); return new JsonContent($response);
} }
public function evaluateGuess(): IContent public function evaluateGuess(): IContent
@ -79,7 +65,7 @@ class GameFlowController
return new JsonContent(['error' => 'no_session_found']); return new JsonContent(['error' => 'no_session_found']);
} }
$last = $state['rounds'][count($state['rounds']) - 1]; $last = $state['rounds'][$state['currentRound']];
$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'));
@ -90,7 +76,9 @@ 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' => [
@ -100,15 +88,13 @@ class GameFlowController
] ]
]; ];
if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) { if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) {
$place = $this->selectNewPlace($state, $mapId); $next = $state['rounds'][$state['currentRound']];
$response['newPlace'] = [ $response['place'] = [
'panoId' => $place->getPanoIdCached(), 'panoId' => $next['panoId'],
'pov' => $place->getPov()->toArray() 'pov' => $next['pov']->toArray()
]; ];
} else {
$state['rounds'] = [];
} }
$session->set('state', $state); $session->set('state', $state);
@ -116,19 +102,23 @@ class GameFlowController
return new JsonContent($response); return new JsonContent($response);
} }
private function selectNewPlace(array &$state, int $mapId) private function startNewGame(array &$state, int $mapId)
{ {
$exclude = array_column($state['rounds'], 'placeId'); $places = $this->placeRepository->getRandomNForMapWithValidPano($mapId, static::NUMBER_OF_ROUNDS);
$place = $this->placeRepository->getRandomForMapWithValidPano($mapId, $exclude);
$state['rounds'][] = [ $state['rounds'] = [];
'placeId' => $place->getId(), $state['currentRound'] = 0;
'position' => $place->getPosition(),
'panoId' => $place->getPanoIdCached(),
'pov' => $place->getPov()
];
return $place; foreach ($places as $place) {
$state['rounds'][] = [
'placeId' => $place->getId(),
'position' => $place->getPosition(),
'panoId' => $place->getPanoIdCached(),
'pov' => $place->getPov()
];
}
$this->request->session()->set('state', $state);
} }
private function calculateDistance(Position $realPosition, Position $guessPosition): float private function calculateDistance(Position $realPosition, Position $guessPosition): float

View File

@ -28,6 +28,21 @@ 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
{ {