2020-05-25 19:46:31 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
use DateTime;
|
2020-06-09 00:56:00 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
2020-05-25 19:46:31 +02:00
|
|
|
use MapGuesser\Util\Geo\Position;
|
2020-05-31 20:43:14 +02:00
|
|
|
use MapGuesser\Response\JsonContent;
|
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
2021-03-19 23:14:01 +01:00
|
|
|
use MapGuesser\Multi\MultiConnector;
|
|
|
|
use MapGuesser\PersistentData\PersistentDataManager;
|
|
|
|
use MapGuesser\Repository\MultiRoomRepository;
|
2020-06-02 01:16:59 +02:00
|
|
|
use MapGuesser\Repository\PlaceRepository;
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-06-05 00:59:23 +02:00
|
|
|
class GameFlowController
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
|
|
|
const NUMBER_OF_ROUNDS = 5;
|
|
|
|
const MAX_SCORE = 1000;
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
private IRequest $request;
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
private PersistentDataManager $pdm;
|
|
|
|
|
|
|
|
private MultiConnector $multiConnector;
|
|
|
|
|
|
|
|
private MultiRoomRepository $multiRoomRepository;
|
|
|
|
|
2020-06-02 01:16:59 +02:00
|
|
|
private PlaceRepository $placeRepository;
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function __construct(IRequest $request)
|
2020-06-02 01:16:59 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$this->request = $request;
|
2021-03-19 23:14:01 +01:00
|
|
|
$this->pdm = new PersistentDataManager();
|
|
|
|
$this->multiConnector = new MultiConnector();
|
|
|
|
$this->multiRoomRepository = new MultiRoomRepository();
|
2020-06-02 01:16:59 +02:00
|
|
|
$this->placeRepository = new PlaceRepository();
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function initialData(): IContent
|
2020-05-30 15:33:28 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$mapId = (int) $this->request->query('mapId');
|
|
|
|
$session = $this->request->session();
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-07-05 00:58:03 +02:00
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
if (!isset($state['currentRound']) || $state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS) {
|
|
|
|
$this->startNewGame($state, $mapId);
|
2021-03-19 23:14:01 +01:00
|
|
|
$session->set('state', $state);
|
2021-03-15 13:58:44 +01:00
|
|
|
}
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$response = [];
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$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(),
|
2021-04-03 13:38:16 +02:00
|
|
|
'result' => [
|
|
|
|
'guessPosition' => $round['guessPosition']->toArray(),
|
|
|
|
'distance' => $round['distance'],
|
|
|
|
'score' => $round['score']
|
|
|
|
]
|
2020-05-25 19:46:31 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
return new JsonContent($response);
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
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
|
2020-05-31 20:43:14 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$mapId = (int) $this->request->query('mapId');
|
|
|
|
$session = $this->request->session();
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-07-05 00:58:03 +02:00
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$last = $state['rounds'][$state['currentRound']];
|
2020-06-09 00:56:00 +02:00
|
|
|
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
2021-03-19 23:14:01 +01:00
|
|
|
$result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']);
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$last['guessPosition'] = $guessPosition;
|
2021-03-19 23:14:01 +01:00
|
|
|
$last['distance'] = $result['distance'];
|
|
|
|
$last['score'] = $result['score'];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
$response = [
|
2021-04-03 13:38:16 +02:00
|
|
|
'position' => $last['position']->toArray(),
|
|
|
|
'result' => $result
|
2021-03-15 12:28:06 +01:00
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
$state['rounds'][$state['currentRound']] = $last;
|
|
|
|
$state['currentRound'] += 1;
|
2021-03-15 13:58:44 +01:00
|
|
|
if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) {
|
|
|
|
$next = $state['rounds'][$state['currentRound']];
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$response['place'] = [
|
|
|
|
'panoId' => $next['panoId'],
|
|
|
|
'pov' => $next['pov']->toArray()
|
2021-03-15 12:28:06 +01:00
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$session->set('state', $state);
|
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
return new JsonContent($response);
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
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']);
|
|
|
|
|
2021-04-04 23:18:26 +02:00
|
|
|
$responseFromMulti = $this->multiConnector->sendMessage('guess', [
|
2021-03-19 23:14:01 +01:00
|
|
|
'roomId' => $roomId,
|
|
|
|
'token' => $multiState['token'],
|
2021-04-03 13:38:16 +02:00
|
|
|
'guessPosition' => $guessPosition->toArray(),
|
2021-03-19 23:14:01 +01:00
|
|
|
'distance' => $result['distance'],
|
|
|
|
'score' => $result['score']
|
|
|
|
]);
|
|
|
|
|
2021-04-04 23:18:26 +02:00
|
|
|
if (isset($responseFromMulti['error'])) {
|
|
|
|
return new JsonContent(['error' => $responseFromMulti['error']]);
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
$response = [
|
2021-04-03 13:38:16 +02:00
|
|
|
'position' => $last['position']->toArray(),
|
|
|
|
'result' => $result,
|
2021-04-04 23:18:26 +02:00
|
|
|
'allResults' => $responseFromMulti['allResults']
|
2021-03-19 23:14:01 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
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
|
2020-06-20 00:03:49 +02:00
|
|
|
{
|
2021-03-15 13:58:44 +01:00
|
|
|
$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()
|
|
|
|
];
|
|
|
|
}
|
2020-06-20 00:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:46:31 +02:00
|
|
|
private function calculateDistance(Position $realPosition, Position $guessPosition): float
|
|
|
|
{
|
|
|
|
return $realPosition->calculateDistanceTo($guessPosition);
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
private function calculateScore(float $distance, float $area): int
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
2020-06-12 21:55:04 +02:00
|
|
|
$goodness = 1.0 - ($distance / (sqrt($area) * 1000));
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
return (int) round(pow(static::MAX_SCORE, $goodness));
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
}
|