2020-05-25 19:46:31 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
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;
|
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;
|
|
|
|
|
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;
|
2020-06-02 01:16:59 +02:00
|
|
|
$this->placeRepository = new PlaceRepository();
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
public function getInitialData(): 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
|
|
|
}
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
if (count($state['rounds']) === 0) {
|
2021-03-15 12:28:06 +01:00
|
|
|
$place = $this->selectNewPlace($state, $mapId);
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$session->set('state', $state);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-07-04 01:11:04 +02:00
|
|
|
$data = [
|
2021-03-15 12:28:06 +01:00
|
|
|
'place' => [
|
|
|
|
'panoId' => $place->getPanoIdCached(),
|
|
|
|
'pov' => $place->getPov()->toArray()
|
|
|
|
]
|
2020-07-04 01:11:04 +02:00
|
|
|
];
|
2020-05-25 19:46:31 +02:00
|
|
|
} else {
|
2020-06-09 00:56:00 +02:00
|
|
|
$rounds = count($state['rounds']);
|
|
|
|
$last = $state['rounds'][$rounds - 1];
|
2020-05-25 19:46:31 +02:00
|
|
|
|
|
|
|
$history = [];
|
|
|
|
for ($i = 0; $i < $rounds - 1; ++$i) {
|
2020-06-09 00:56:00 +02:00
|
|
|
$round = $state['rounds'][$i];
|
2020-05-25 19:46:31 +02:00
|
|
|
$history[] = [
|
|
|
|
'position' => $round['position']->toArray(),
|
|
|
|
'guessPosition' => $round['guessPosition']->toArray(),
|
|
|
|
'distance' => $round['distance'],
|
|
|
|
'score' => $round['score']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'history' => $history,
|
2021-03-15 12:28:06 +01:00
|
|
|
'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]
|
|
|
|
]
|
2020-05-25 19:46:31 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function evaluateGuess(): 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
|
|
|
}
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$last = $state['rounds'][count($state['rounds']) - 1];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
|
|
|
$position = $last['position'];
|
2020-06-09 00:56:00 +02:00
|
|
|
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
2020-05-31 20:43:14 +02:00
|
|
|
|
|
|
|
$distance = $this->calculateDistance($position, $guessPosition);
|
2020-06-09 00:56:00 +02:00
|
|
|
$score = $this->calculateScore($distance, $state['area']);
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$last['guessPosition'] = $guessPosition;
|
2020-05-31 20:43:14 +02:00
|
|
|
$last['distance'] = $distance;
|
|
|
|
$last['score'] = $score;
|
2020-06-09 00:56:00 +02:00
|
|
|
$state['rounds'][count($state['rounds']) - 1] = $last;
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
$response = [
|
|
|
|
'result' => [
|
|
|
|
'position' => $position->toArray(),
|
|
|
|
'distance' => $distance,
|
|
|
|
'score' => $score
|
|
|
|
]
|
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) {
|
|
|
|
$place = $this->selectNewPlace($state, $mapId);
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
$response['newPlace'] = [
|
|
|
|
'panoId' => $place->getPanoIdCached(),
|
|
|
|
'pov' => $place->getPov()->toArray()
|
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
} else {
|
2020-06-09 00:56:00 +02:00
|
|
|
$state['rounds'] = [];
|
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-15 12:28:06 +01:00
|
|
|
private function selectNewPlace(array &$state, int $mapId)
|
2020-06-20 00:03:49 +02:00
|
|
|
{
|
2021-03-15 12:28:06 +01:00
|
|
|
$exclude = array_column($state['rounds'], 'placeId');
|
|
|
|
$place = $this->placeRepository->getRandomForMapWithValidPano($mapId, $exclude);
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$state['rounds'][] = [
|
|
|
|
'placeId' => $place->getId(),
|
|
|
|
'position' => $place->getPosition(),
|
2020-07-04 01:11:04 +02:00
|
|
|
'panoId' => $place->getPanoIdCached(),
|
|
|
|
'pov' => $place->getPov()
|
2020-06-20 00:03:49 +02:00
|
|
|
];
|
2021-03-15 12:28:06 +01:00
|
|
|
|
|
|
|
return $place;
|
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
|
|
|
}
|
|
|
|
}
|