MAPG-115 adapt existing controllers to the new request classes

This commit is contained in:
Bence Pőcze 2020-06-09 00:56:00 +02:00
parent 00dc78b50c
commit e8f9d74283
4 changed files with 65 additions and 38 deletions

View File

@ -30,9 +30,10 @@ $match = Container::$routeCollection->match($method, explode('/', $url));
if ($match !== null) { if ($match !== null) {
list($route, $params) = $match; 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) { if ($controller instanceof MapGuesser\Interfaces\Authorization\ISecured) {
$authorized = $controller->authorize(); $authorized = $controller->authorize();
@ -41,7 +42,7 @@ if ($match !== null) {
} }
if ($authorized) { if ($authorized) {
$response = call_user_func([$controller, $handler[1]], $params); $response = call_user_func([$controller, $handler[1]]);
if ($response instanceof MapGuesser\Interfaces\Response\IContent) { if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');

View File

@ -2,6 +2,7 @@
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Util\Geo\Bounds; use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Response\HtmlContent; use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent; use MapGuesser\Response\JsonContent;
@ -9,16 +10,23 @@ use MapGuesser\Interfaces\Response\IContent;
class GameController 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); $data = $this->prepareGame($mapId);
return new HtmlContent('game', $data); 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); $data = $this->prepareGame($mapId);
return new JsonContent($data); return new JsonContent($data);
} }
@ -27,12 +35,14 @@ class GameController
{ {
$bounds = $this->getMapBounds($mapId); $bounds = $this->getMapBounds($mapId);
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) { $session = $this->request->session();
$_SESSION['state'] = [
if (($state = $session->get('state')) && $state['mapId'] !== $mapId) {
$session->set('state', [
'mapId' => $mapId, 'mapId' => $mapId,
'area' => $bounds->calculateApproximateArea(), 'area' => $bounds->calculateApproximateArea(),
'rounds' => [] 'rounds' => []
]; ]);
} }
return ['mapId' => $mapId, 'bounds' => $bounds->toArray()]; return ['mapId' => $mapId, 'bounds' => $bounds->toArray()];

View File

@ -1,8 +1,10 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Authentication\IUser;
use MapGuesser\Interfaces\Authorization\ISecured; use MapGuesser\Interfaces\Authorization\ISecured;
use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\Repository\PlaceRepository; use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Response\HtmlContent; use MapGuesser\Response\HtmlContent;
@ -11,18 +13,21 @@ use MapGuesser\Util\Geo\Bounds;
class MapAdminController implements ISecured class MapAdminController implements ISecured
{ {
private IRequest $request;
private PlaceRepository $placeRepository; private PlaceRepository $placeRepository;
public function __construct() public function __construct(IRequest $request)
{ {
$this->request = $request;
$this->placeRepository = new PlaceRepository(); $this->placeRepository = new PlaceRepository();
} }
public function authorize(): bool public function authorize(): bool
{ {
//TODO $user = $this->request->user();
return false; return $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN);
} }
public function getMaps(): IContent public function getMaps(): IContent
@ -32,9 +37,9 @@ class MapAdminController implements ISecured
return new HtmlContent('admin/maps'); 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); $bounds = $this->getMapBounds($mapId);
@ -44,9 +49,9 @@ class MapAdminController implements ISecured
return new HtmlContent('admin/map_editor', $data); 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); $placeData = $this->placeRepository->getById($placeId);

View File

@ -1,5 +1,6 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Util\Geo\Position; use MapGuesser\Util\Geo\Position;
use MapGuesser\Response\JsonContent; use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
@ -10,34 +11,40 @@ class PositionController
const NUMBER_OF_ROUNDS = 5; const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000; const MAX_SCORE = 1000;
private IRequest $request;
private PlaceRepository $placeRepository; private PlaceRepository $placeRepository;
public function __construct() public function __construct(IRequest $request)
{ {
$this->request = $request;
$this->placeRepository = new PlaceRepository(); $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']; $data = ['error' => 'no_session_found'];
return new JsonContent($data); return new JsonContent($data);
} }
if (count($_SESSION['state']['rounds']) === 0) { if (count($state['rounds']) === 0) {
$newPosition = $this->placeRepository->getForMapWithValidPano($mapId); $newPosition = $this->placeRepository->getForMapWithValidPano($mapId);
$_SESSION['state']['rounds'][] = $newPosition; $state['rounds'][] = $newPosition;
$session->set('state', $state);
$data = ['panoId' => $newPosition['panoId']]; $data = ['panoId' => $newPosition['panoId']];
} else { } else {
$rounds = count($_SESSION['state']['rounds']); $rounds = count($state['rounds']);
$last = $_SESSION['state']['rounds'][$rounds - 1]; $last = $state['rounds'][$rounds - 1];
$history = []; $history = [];
for ($i = 0; $i < $rounds - 1; ++$i) { for ($i = 0; $i < $rounds - 1; ++$i) {
$round = $_SESSION['state']['rounds'][$i]; $round = $state['rounds'][$i];
$history[] = [ $history[] = [
'position' => $round['position']->toArray(), 'position' => $round['position']->toArray(),
'guessPosition' => $round['guessPosition']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(),
@ -55,41 +62,45 @@ class PositionController
return new JsonContent($data); 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']; $data = ['error' => 'no_session_found'];
return new JsonContent($data); return new JsonContent($data);
} }
$last = &$_SESSION['state']['rounds'][count($_SESSION['state']['rounds']) - 1]; $last = $state['rounds'][count($state['rounds']) - 1];
$position = $last['position']; $position = $last['position'];
$guessPosition = new Position((float) $_POST['lat'], (float) $_POST['lng']); $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
$last['guessPosition'] = $guessPosition;
$distance = $this->calculateDistance($position, $guessPosition); $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['distance'] = $distance;
$last['score'] = $score; $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 = []; $exclude = [];
foreach ($_SESSION['state']['rounds'] as $round) { foreach ($state['rounds'] as $round) {
$exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]); $exclude = array_merge($exclude, $round['placesWithoutPano'], [$round['placeId']]);
} }
$newPosition = $this->placeRepository->getForMapWithValidPano($mapId, $exclude); $newPosition = $this->placeRepository->getForMapWithValidPano($mapId, $exclude);
$_SESSION['state']['rounds'][] = $newPosition; $state['rounds'][] = $newPosition;
$session->set('state', $state);
$panoId = $newPosition['panoId']; $panoId = $newPosition['panoId'];
} else { } else {
$_SESSION['state']['rounds'] = []; $state['rounds'] = [];
$session->set('state', $state);
$panoId = null; $panoId = null;
} }