2020-05-25 19:46:31 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2020-05-28 21:04:53 +02:00
|
|
|
use MapGuesser\Database\Query\Select;
|
|
|
|
use MapGuesser\Http\Request;
|
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
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-05-25 19:46:31 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
class PositionController
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
|
|
|
const NUMBER_OF_ROUNDS = 5;
|
|
|
|
const MAX_SCORE = 1000;
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
public function getPosition(array $parameters): IContent
|
2020-05-30 15:33:28 +02:00
|
|
|
{
|
2020-05-31 20:43:14 +02:00
|
|
|
$mapId = (int) $parameters['mapId'];
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
2020-05-26 21:27:15 +02:00
|
|
|
$data = ['error' => 'No valid session found!'];
|
2020-05-31 20:43:14 +02:00
|
|
|
return new JsonContent($data);
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($_SESSION['state']['rounds']) === 0) {
|
2020-05-31 20:43:14 +02:00
|
|
|
$newPosition = $this->getNewPosition($mapId);
|
2020-05-25 19:46:31 +02:00
|
|
|
$_SESSION['state']['rounds'][] = $newPosition;
|
|
|
|
|
|
|
|
$data = ['panoId' => $newPosition['panoId']];
|
|
|
|
} else {
|
|
|
|
$rounds = count($_SESSION['state']['rounds']);
|
|
|
|
$last = $_SESSION['state']['rounds'][$rounds - 1];
|
|
|
|
|
|
|
|
$history = [];
|
|
|
|
for ($i = 0; $i < $rounds - 1; ++$i) {
|
|
|
|
$round = $_SESSION['state']['rounds'][$i];
|
|
|
|
$history[] = [
|
|
|
|
'position' => $round['position']->toArray(),
|
|
|
|
'guessPosition' => $round['guessPosition']->toArray(),
|
|
|
|
'distance' => $round['distance'],
|
|
|
|
'score' => $round['score']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'history' => $history,
|
|
|
|
'panoId' => $last['panoId']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function evaluateGuess(array $parameters): IContent
|
|
|
|
{
|
|
|
|
$mapId = (int) $parameters['mapId'];
|
|
|
|
|
|
|
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
|
|
|
$data = ['error' => 'No valid session found!'];
|
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1];
|
|
|
|
|
|
|
|
$position = $last['position'];
|
|
|
|
$guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']);
|
|
|
|
|
|
|
|
$last['guessPosition'] = $guessPosition;
|
|
|
|
|
|
|
|
$distance = $this->calculateDistance($position, $guessPosition);
|
|
|
|
$score = $this->calculateScore($distance, $_SESSION['state']['area']);
|
|
|
|
|
|
|
|
$last['distance'] = $distance;
|
|
|
|
$last['score'] = $score;
|
|
|
|
|
|
|
|
if (count($_SESSION['state']['rounds']) < static::NUMBER_OF_ROUNDS) {
|
|
|
|
$exclude = [];
|
|
|
|
|
|
|
|
foreach ($_SESSION['state']['rounds'] as $round) {
|
|
|
|
$exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newPosition = $this->getNewPosition($mapId, $exclude);
|
|
|
|
$_SESSION['state']['rounds'][] = $newPosition;
|
|
|
|
|
|
|
|
$panoId = $newPosition['panoId'];
|
|
|
|
} else {
|
|
|
|
$_SESSION['state']['rounds'] = [];
|
|
|
|
|
|
|
|
$panoId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'result' => [
|
|
|
|
'position' => $position->toArray(),
|
|
|
|
'distance' => $distance,
|
|
|
|
'score' => $score
|
|
|
|
],
|
|
|
|
'panoId' => $panoId
|
|
|
|
];
|
|
|
|
return new JsonContent($data);
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
private function getNewPosition(int $mapId, array $exclude = []): array
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
|
|
|
$placesWithoutPano = [];
|
|
|
|
|
|
|
|
do {
|
2020-05-31 20:43:14 +02:00
|
|
|
$place = $this->selectNewPlace($mapId, $exclude);
|
2020-05-25 19:46:31 +02:00
|
|
|
$position = new Position($place['lat'], $place['lng']);
|
|
|
|
$panoId = $this->getPanorama($position);
|
|
|
|
|
|
|
|
if ($panoId === null) {
|
|
|
|
$placesWithoutPano[] = $place['id'];
|
|
|
|
}
|
|
|
|
} while ($panoId === null);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'placesWithoutPano' => $placesWithoutPano,
|
|
|
|
'placeId' => $place['id'],
|
|
|
|
'position' => $position,
|
|
|
|
'panoId' => $panoId
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
private function selectNewPlace(int $mapId, array $exclude): array
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
2020-05-28 21:04:53 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'places');
|
|
|
|
$select->columns(['id', 'lat', 'lng']);
|
|
|
|
$select->where('id', 'NOT IN', $exclude);
|
2020-05-31 20:43:14 +02:00
|
|
|
$select->where('map_id', '=', $mapId);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-30 15:33:28 +02:00
|
|
|
$numberOfPlaces = $select->count();// TODO: what if 0
|
2020-05-25 19:46:31 +02:00
|
|
|
$randomOffset = random_int(0, $numberOfPlaces - 1);
|
|
|
|
|
2020-05-28 21:04:53 +02:00
|
|
|
$select->orderBy('id');
|
|
|
|
$select->limit(1, $randomOffset);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:04:53 +02:00
|
|
|
$place = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:04:53 +02:00
|
|
|
return $place;
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getPanorama(Position $position): ?string
|
|
|
|
{
|
2020-05-28 21:05:55 +02:00
|
|
|
$request = new Request('https://maps.googleapis.com/maps/api/streetview/metadata', Request::HTTP_GET);
|
|
|
|
$request->setQuery([
|
2020-05-25 19:46:31 +02:00
|
|
|
'key' => $_ENV['GOOGLE_MAPS_SERVER_API_KEY'],
|
|
|
|
'location' => $position->getLat() . ',' . $position->getLng(),
|
|
|
|
'source' => 'outdoor'
|
2020-05-28 21:05:55 +02:00
|
|
|
]);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
$response = $request->send();
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
$panoData = json_decode($response->getBody(), true);
|
2020-05-25 19:46:31 +02:00
|
|
|
|
|
|
|
if ($panoData['status'] !== 'OK') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $panoData['pano_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$goodness = 1.0 - ($distance / sqrt($area));
|
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
return (int) round(pow(static::MAX_SCORE, $goodness));
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
}
|