MAPG-115 adapt existing controllers to the new request classes
This commit is contained in:
parent
00dc78b50c
commit
e8f9d74283
@ -30,9 +30,10 @@ $match = Container::$routeCollection->match($method, explode('/', $url));
|
||||
if ($match !== null) {
|
||||
list($route, $params) = $match;
|
||||
|
||||
$handler = $route->getHandler();
|
||||
$request = new MapGuesser\Request\Request($_GET, $params, $_POST, $_SESSION);
|
||||
|
||||
$controller = new $handler[0];
|
||||
$handler = $route->getHandler();
|
||||
$controller = new $handler[0]($request);
|
||||
|
||||
if ($controller instanceof MapGuesser\Interfaces\Authorization\ISecured) {
|
||||
$authorized = $controller->authorize();
|
||||
@ -41,7 +42,7 @@ if ($match !== null) {
|
||||
}
|
||||
|
||||
if ($authorized) {
|
||||
$response = call_user_func([$controller, $handler[1]], $params);
|
||||
$response = call_user_func([$controller, $handler[1]]);
|
||||
|
||||
if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
|
||||
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Util\Geo\Bounds;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
@ -9,16 +10,23 @@ use MapGuesser\Interfaces\Response\IContent;
|
||||
|
||||
class GameController
|
||||
{
|
||||
public function getGame(array $parameters): IContent
|
||||
private IRequest $request;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$mapId = (int) $parameters['mapId'];
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function getGame(): IContent
|
||||
{
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
$data = $this->prepareGame($mapId);
|
||||
return new HtmlContent('game', $data);
|
||||
}
|
||||
|
||||
public function getGameJson(array $parameters): IContent
|
||||
public function getGameJson(): IContent
|
||||
{
|
||||
$mapId = (int) $parameters['mapId'];
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
$data = $this->prepareGame($mapId);
|
||||
return new JsonContent($data);
|
||||
}
|
||||
@ -27,12 +35,14 @@ class GameController
|
||||
{
|
||||
$bounds = $this->getMapBounds($mapId);
|
||||
|
||||
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
||||
$_SESSION['state'] = [
|
||||
$session = $this->request->session();
|
||||
|
||||
if (($state = $session->get('state')) && $state['mapId'] !== $mapId) {
|
||||
$session->set('state', [
|
||||
'mapId' => $mapId,
|
||||
'area' => $bounds->calculateApproximateArea(),
|
||||
'rounds' => []
|
||||
];
|
||||
]);
|
||||
}
|
||||
|
||||
return ['mapId' => $mapId, 'bounds' => $bounds->toArray()];
|
||||
|
@ -1,8 +1,10 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Authentication\IUser;
|
||||
use MapGuesser\Interfaces\Authorization\ISecured;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Repository\PlaceRepository;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
@ -11,18 +13,21 @@ use MapGuesser\Util\Geo\Bounds;
|
||||
|
||||
class MapAdminController implements ISecured
|
||||
{
|
||||
private IRequest $request;
|
||||
|
||||
private PlaceRepository $placeRepository;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
//TODO
|
||||
$user = $this->request->user();
|
||||
|
||||
return false;
|
||||
return $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN);
|
||||
}
|
||||
|
||||
public function getMaps(): IContent
|
||||
@ -32,9 +37,9 @@ class MapAdminController implements ISecured
|
||||
return new HtmlContent('admin/maps');
|
||||
}
|
||||
|
||||
public function getMapEditor(array $parameters): IContent
|
||||
public function getMapEditor(): IContent
|
||||
{
|
||||
$mapId = (int) $parameters['mapId'];
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
|
||||
$bounds = $this->getMapBounds($mapId);
|
||||
|
||||
@ -44,9 +49,9 @@ class MapAdminController implements ISecured
|
||||
return new HtmlContent('admin/map_editor', $data);
|
||||
}
|
||||
|
||||
public function getPlace(array $parameters)
|
||||
public function getPlace()
|
||||
{
|
||||
$placeId = (int) $parameters['placeId'];
|
||||
$placeId = (int) $this->request->query('placeId');
|
||||
|
||||
$placeData = $this->placeRepository->getById($placeId);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Util\Geo\Position;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
@ -10,34 +11,40 @@ class PositionController
|
||||
const NUMBER_OF_ROUNDS = 5;
|
||||
const MAX_SCORE = 1000;
|
||||
|
||||
private IRequest $request;
|
||||
|
||||
private PlaceRepository $placeRepository;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
}
|
||||
|
||||
public function getPosition(array $parameters): IContent
|
||||
public function getPosition(): IContent
|
||||
{
|
||||
$mapId = (int) $parameters['mapId'];
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
|
||||
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
||||
$session = $this->request->session();
|
||||
|
||||
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
||||
$data = ['error' => 'no_session_found'];
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
if (count($_SESSION['state']['rounds']) === 0) {
|
||||
if (count($state['rounds']) === 0) {
|
||||
$newPosition = $this->placeRepository->getForMapWithValidPano($mapId);
|
||||
$_SESSION['state']['rounds'][] = $newPosition;
|
||||
$state['rounds'][] = $newPosition;
|
||||
$session->set('state', $state);
|
||||
|
||||
$data = ['panoId' => $newPosition['panoId']];
|
||||
} else {
|
||||
$rounds = count($_SESSION['state']['rounds']);
|
||||
$last = $_SESSION['state']['rounds'][$rounds - 1];
|
||||
$rounds = count($state['rounds']);
|
||||
$last = $state['rounds'][$rounds - 1];
|
||||
|
||||
$history = [];
|
||||
for ($i = 0; $i < $rounds - 1; ++$i) {
|
||||
$round = $_SESSION['state']['rounds'][$i];
|
||||
$round = $state['rounds'][$i];
|
||||
$history[] = [
|
||||
'position' => $round['position']->toArray(),
|
||||
'guessPosition' => $round['guessPosition']->toArray(),
|
||||
@ -55,41 +62,45 @@ class PositionController
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
public function evaluateGuess(array $parameters): IContent
|
||||
public function evaluateGuess(): IContent
|
||||
{
|
||||
$mapId = (int) $parameters['mapId'];
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
|
||||
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
||||
$session = $this->request->session();
|
||||
|
||||
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
||||
$data = ['error' => 'no_session_found'];
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
$last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1];
|
||||
$last = $state['rounds'][count($state['rounds']) - 1];
|
||||
|
||||
$position = $last['position'];
|
||||
$guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']);
|
||||
|
||||
$last['guessPosition'] = $guessPosition;
|
||||
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
||||
|
||||
$distance = $this->calculateDistance($position, $guessPosition);
|
||||
$score = $this->calculateScore($distance, $_SESSION['state']['area']);
|
||||
$score = $this->calculateScore($distance, $state['area']);
|
||||
|
||||
$last['guessPosition'] = $guessPosition;
|
||||
$last['distance'] = $distance;
|
||||
$last['score'] = $score;
|
||||
$state['rounds'][count($state['rounds']) - 1] = $last;
|
||||
|
||||
if (count($_SESSION['state']['rounds']) < static::NUMBER_OF_ROUNDS) {
|
||||
if (count($state['rounds']) < static::NUMBER_OF_ROUNDS) {
|
||||
$exclude = [];
|
||||
|
||||
foreach ($_SESSION['state']['rounds'] as $round) {
|
||||
foreach ($state['rounds'] as $round) {
|
||||
$exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]);
|
||||
}
|
||||
|
||||
$newPosition = $this->placeRepository->getForMapWithValidPano($mapId, $exclude);
|
||||
$_SESSION['state']['rounds'][] = $newPosition;
|
||||
$state['rounds'][] = $newPosition;
|
||||
$session->set('state', $state);
|
||||
|
||||
$panoId = $newPosition['panoId'];
|
||||
} else {
|
||||
$_SESSION['state']['rounds'] = [];
|
||||
$state['rounds'] = [];
|
||||
$session->set('state', $state);
|
||||
|
||||
$panoId = null;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user