request = $request; $this->pdm = new PersistentDataManager(); $this->multiConnector = new MultiConnector(); $this->multiRoomRepository = new MultiRoomRepository(); $this->placeRepository = new PlaceRepository(); } public function initialData(): IContent { $mapId = (int) $this->request->query('mapId'); $session = $this->request->session(); if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { return new JsonContent(['error' => 'no_session_found']); } if (!isset($state['currentRound']) || $state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS) { $this->startNewGame($state, $mapId); $session->set('state', $state); } $response = []; $last = $state['rounds'][$state['currentRound']]; $response['place'] = [ 'panoId' => $last['panoId'], 'pov' => $last['pov']->toArray() ]; $response['history'] = []; for ($i = 0; $i < $state['currentRound']; ++$i) { $round = $state['rounds'][$i]; $response['history'][] = [ 'position' => $round['position']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(), 'distance' => $round['distance'], 'score' => $round['score'] ]; } return new JsonContent($response); } public function multiInitialData(): IContent { $roomId = $this->request->query('roomId'); $session = $this->request->session(); if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) { return new JsonContent(['error' => 'no_session_found']); } $room = $this->multiRoomRepository->getByRoomId($roomId); $state = $room->getStateArray(); $members = $room->getMembersArray(); if ($members['owner'] !== $multiState['token']) { return new JsonContent(['error' => 'not_owner_of_room']); } if ($state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS - 1) { $this->startNewGame($state, $state['mapId']); $room->setStateArray($state); $room->setUpdatedDate(new DateTime()); $this->pdm->saveToDb($room); } $places = []; foreach ($state['rounds'] as $round) { $places[] = [ 'position' => $round['position']->toArray(), 'panoId' => $round['panoId'], 'pov' => $round['pov']->toArray() ]; } $this->multiConnector->sendMessage('start_game', ['roomId' => $roomId, 'places' => $places]); return new JsonContent(['ok' => true]); } public function guess(): IContent { $mapId = (int) $this->request->query('mapId'); $session = $this->request->session(); if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { return new JsonContent(['error' => 'no_session_found']); } $last = $state['rounds'][$state['currentRound']]; $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng')); $result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']); $last['guessPosition'] = $guessPosition; $last['distance'] = $result['distance']; $last['score'] = $result['score']; $response = [ 'result' => [ 'position' => $last['position']->toArray(), 'distance' => $result['distance'], 'score' => $result['score'] ] ]; $state['rounds'][$state['currentRound']] = $last; $state['currentRound'] += 1; if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) { $next = $state['rounds'][$state['currentRound']]; $response['place'] = [ 'panoId' => $next['panoId'], 'pov' => $next['pov']->toArray() ]; } $session->set('state', $state); return new JsonContent($response); } public function multiGuess(): IContent { $roomId = $this->request->query('roomId'); $session = $this->request->session(); if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) { return new JsonContent(['error' => 'no_session_found']); } $room = $this->multiRoomRepository->getByRoomId($roomId); $state = $room->getStateArray(); $last = $state['rounds'][$state['currentRound']]; $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng')); $result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']); $this->multiConnector->sendMessage('guess', [ 'roomId' => $roomId, 'token' => $multiState['token'], 'guess' => $guessPosition->toArray(), 'distance' => $result['distance'], 'score' => $result['score'] ]); $response = [ 'result' => [ 'position' => $last['position']->toArray(), 'distance' => $result['distance'], 'score' => $result['score'] ] ]; return new JsonContent($response); } public function multiNextRound(): IContent { $roomId = $this->request->query('roomId'); $session = $this->request->session(); if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) { return new JsonContent(['error' => 'no_session_found']); } $room = $this->multiRoomRepository->getByRoomId($roomId); $state = $room->getStateArray(); $members = $room->getMembersArray(); if ($members['owner'] !== $multiState['token']) { return new JsonContent(['error' => 'not_owner_of_room']); } $state['currentRound'] += 1; if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) { $this->multiConnector->sendMessage('next_round', ['roomId' => $roomId, 'currentRound' => $state['currentRound']]); } $room->setStateArray($state); $room->setUpdatedDate(new DateTime()); $this->pdm->saveToDb($room); return new JsonContent(['ok' => true]); } private function evalueteGuess(Position $realPosition, Position $guessPosition, float $area) { $distance = $this->calculateDistance($realPosition, $guessPosition); $score = $this->calculateScore($distance, $area); return ['distance' => $distance, 'score' => $score]; } private function startNewGame(array &$state, int $mapId): void { $places = $this->placeRepository->getRandomNForMapWithValidPano($mapId, static::NUMBER_OF_ROUNDS); $state['rounds'] = []; $state['currentRound'] = 0; foreach ($places as $place) { $state['rounds'][] = [ 'placeId' => $place->getId(), 'position' => $place->getPosition(), 'panoId' => $place->getPanoIdCached(), 'pov' => $place->getPov() ]; } } private function calculateDistance(Position $realPosition, Position $guessPosition): float { return $realPosition->calculateDistanceTo($guessPosition); } private function calculateScore(float $distance, float $area): int { $goodness = 1.0 - ($distance / (sqrt($area) * 1000)); return (int) round(pow(static::MAX_SCORE, $goodness)); } }