MAPG-86 adapt controller classes to the new controller structure
This commit is contained in:
parent
88fe7b7a03
commit
927b34956f
@ -1,51 +1,48 @@
|
|||||||
<?php namespace MapGuesser\Controller;
|
<?php namespace MapGuesser\Controller;
|
||||||
|
|
||||||
use MapGuesser\Database\Query\Select;
|
use MapGuesser\Database\Query\Select;
|
||||||
use MapGuesser\Interfaces\Controller\IController;
|
|
||||||
use MapGuesser\Interfaces\Database\IResultSet;
|
use MapGuesser\Interfaces\Database\IResultSet;
|
||||||
use MapGuesser\Util\Geo\Bounds;
|
use MapGuesser\Util\Geo\Bounds;
|
||||||
use MapGuesser\View\HtmlView;
|
use MapGuesser\Response\HtmlContent;
|
||||||
use MapGuesser\View\JsonView;
|
use MapGuesser\Response\JsonContent;
|
||||||
use MapGuesser\Interfaces\View\IView;
|
use MapGuesser\Interfaces\Response\IContent;
|
||||||
|
|
||||||
class GameController implements IController
|
class GameController
|
||||||
{
|
{
|
||||||
private int $mapId;
|
public function getGame(array $parameters): IContent
|
||||||
|
|
||||||
private bool $jsonResponse;
|
|
||||||
|
|
||||||
public function __construct(int $mapId, $jsonResponse = false)
|
|
||||||
{
|
{
|
||||||
$this->mapId = $mapId;
|
$mapId = (int) $parameters['mapId'];
|
||||||
$this->jsonResponse = $jsonResponse;
|
$data = $this->prepareGame($mapId);
|
||||||
|
return new HtmlContent('game', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(): IView
|
public function getGameJson(array $parameters): IContent
|
||||||
{
|
{
|
||||||
$bounds = $this->getMapBounds();
|
$mapId = (int) $parameters['mapId'];
|
||||||
|
$data = $this->prepareGame($mapId);
|
||||||
|
return new JsonContent($data);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
|
private function prepareGame(int $mapId)
|
||||||
|
{
|
||||||
|
$bounds = $this->getMapBounds($mapId);
|
||||||
|
|
||||||
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
||||||
$_SESSION['state'] = [
|
$_SESSION['state'] = [
|
||||||
'mapId' => $this->mapId,
|
'mapId' => $mapId,
|
||||||
'area' => $bounds->calculateApproximateArea(),
|
'area' => $bounds->calculateApproximateArea(),
|
||||||
'rounds' => []
|
'rounds' => []
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = ['mapId' => $this->mapId, 'bounds' => $bounds->toArray()];
|
return ['mapId' => $mapId, 'bounds' => $bounds->toArray()];
|
||||||
|
|
||||||
if ($this->jsonResponse) {
|
|
||||||
return new JsonView($data);
|
|
||||||
} else {
|
|
||||||
return new HtmlView('game', $data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMapBounds(): Bounds
|
private function getMapBounds(int $mapId): Bounds
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection, 'maps');
|
$select = new Select(\Container::$dbConnection, 'maps');
|
||||||
$select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']);
|
$select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']);
|
||||||
$select->whereId($this->mapId);
|
$select->whereId($mapId);
|
||||||
|
|
||||||
$map = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
$map = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||||
|
|
||||||
|
@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
use MapGuesser\Database\Query\Select;
|
use MapGuesser\Database\Query\Select;
|
||||||
use MapGuesser\Database\RawExpression;
|
use MapGuesser\Database\RawExpression;
|
||||||
use MapGuesser\Interfaces\Controller\IController;
|
|
||||||
use MapGuesser\Interfaces\Database\IResultSet;
|
use MapGuesser\Interfaces\Database\IResultSet;
|
||||||
use MapGuesser\Interfaces\View\IView;
|
use MapGuesser\Interfaces\Response\IContent;
|
||||||
use MapGuesser\Util\Geo\Bounds;
|
use MapGuesser\Util\Geo\Bounds;
|
||||||
use MapGuesser\View\HtmlView;
|
use MapGuesser\Response\HtmlContent;
|
||||||
|
|
||||||
class MapsController implements IController
|
class MapsController
|
||||||
{
|
{
|
||||||
public function run(): IView
|
public function getMaps(): IContent
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection, 'maps');
|
$select = new Select(\Container::$dbConnection, 'maps');
|
||||||
$select->columns([
|
$select->columns([
|
||||||
@ -38,7 +37,7 @@ class MapsController implements IController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$data = ['maps' => $maps];
|
$data = ['maps' => $maps];
|
||||||
return new HtmlView('maps', $data);
|
return new HtmlContent('maps', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatMapAreaForHuman(float $area): array
|
private function formatMapAreaForHuman(float $area): array
|
||||||
|
@ -2,75 +2,30 @@
|
|||||||
|
|
||||||
use MapGuesser\Database\Query\Select;
|
use MapGuesser\Database\Query\Select;
|
||||||
use MapGuesser\Http\Request;
|
use MapGuesser\Http\Request;
|
||||||
use MapGuesser\Interfaces\Controller\IController;
|
|
||||||
use MapGuesser\Interfaces\Database\IResultSet;
|
use MapGuesser\Interfaces\Database\IResultSet;
|
||||||
use MapGuesser\Util\Geo\Position;
|
use MapGuesser\Util\Geo\Position;
|
||||||
use MapGuesser\View\JsonView;
|
use MapGuesser\Response\JsonContent;
|
||||||
use MapGuesser\Interfaces\View\IView;
|
use MapGuesser\Interfaces\Response\IContent;
|
||||||
|
|
||||||
class PositionController implements IController
|
class PositionController
|
||||||
{
|
{
|
||||||
const NUMBER_OF_ROUNDS = 5;
|
const NUMBER_OF_ROUNDS = 5;
|
||||||
const MAX_SCORE = 1000;
|
const MAX_SCORE = 1000;
|
||||||
|
|
||||||
private int $mapId;
|
public function getPosition(array $parameters): IContent
|
||||||
|
|
||||||
public function __construct(int $mapId)
|
|
||||||
{
|
{
|
||||||
$this->mapId = $mapId;
|
$mapId = (int) $parameters['mapId'];
|
||||||
}
|
|
||||||
|
|
||||||
public function run(): IView
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
||||||
{
|
|
||||||
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
|
|
||||||
$data = ['error' => 'No valid session found!'];
|
$data = ['error' => 'No valid session found!'];
|
||||||
return new JsonView($data);
|
return new JsonContent($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($_SESSION['state']['rounds']) === 0) {
|
if (count($_SESSION['state']['rounds']) === 0) {
|
||||||
$newPosition = $this->getNewPosition();
|
$newPosition = $this->getNewPosition($mapId);
|
||||||
$_SESSION['state']['rounds'][] = $newPosition;
|
$_SESSION['state']['rounds'][] = $newPosition;
|
||||||
|
|
||||||
$data = ['panoId' => $newPosition['panoId']];
|
$data = ['panoId' => $newPosition['panoId']];
|
||||||
} elseif (isset($_POST['guess'])) {
|
|
||||||
$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($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
|
|
||||||
];
|
|
||||||
} else {
|
} else {
|
||||||
$rounds = count($_SESSION['state']['rounds']);
|
$rounds = count($_SESSION['state']['rounds']);
|
||||||
$last = $_SESSION['state']['rounds'][$rounds - 1];
|
$last = $_SESSION['state']['rounds'][$rounds - 1];
|
||||||
@ -92,15 +47,65 @@ class PositionController implements IController
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonView($data);
|
return new JsonContent($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getNewPosition(array $exclude = []): array
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getNewPosition(int $mapId, array $exclude = []): array
|
||||||
{
|
{
|
||||||
$placesWithoutPano = [];
|
$placesWithoutPano = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$place = $this->selectNewPlace($exclude);
|
$place = $this->selectNewPlace($mapId, $exclude);
|
||||||
$position = new Position($place['lat'], $place['lng']);
|
$position = new Position($place['lat'], $place['lng']);
|
||||||
$panoId = $this->getPanorama($position);
|
$panoId = $this->getPanorama($position);
|
||||||
|
|
||||||
@ -117,12 +122,12 @@ class PositionController implements IController
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function selectNewPlace(array $exclude): array
|
private function selectNewPlace(int $mapId, array $exclude): array
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection, 'places');
|
$select = new Select(\Container::$dbConnection, 'places');
|
||||||
$select->columns(['id', 'lat', 'lng']);
|
$select->columns(['id', 'lat', 'lng']);
|
||||||
$select->where('id', 'NOT IN', $exclude);
|
$select->where('id', 'NOT IN', $exclude);
|
||||||
$select->where('map_id', '=', $this->mapId);
|
$select->where('map_id', '=', $mapId);
|
||||||
|
|
||||||
$numberOfPlaces = $select->count();// TODO: what if 0
|
$numberOfPlaces = $select->count();// TODO: what if 0
|
||||||
$randomOffset = random_int(0, $numberOfPlaces - 1);
|
$randomOffset = random_int(0, $numberOfPlaces - 1);
|
||||||
|
Loading…
Reference in New Issue
Block a user