MAPG-86 adapt controller classes to the new controller structure

This commit is contained in:
Bence Pőcze 2020-05-31 20:43:14 +02:00
parent 88fe7b7a03
commit 927b34956f
3 changed files with 90 additions and 89 deletions

View File

@ -1,51 +1,48 @@
<?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\View\HtmlView;
use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView;
use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent;
class GameController implements IController
class GameController
{
private int $mapId;
private bool $jsonResponse;
public function __construct(int $mapId, $jsonResponse = false)
public function getGame(array $parameters): IContent
{
$this->mapId = $mapId;
$this->jsonResponse = $jsonResponse;
$mapId = (int) $parameters['mapId'];
$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'] = [
'mapId' => $this->mapId,
'mapId' => $mapId,
'area' => $bounds->calculateApproximateArea(),
'rounds' => []
];
}
$data = ['mapId' => $this->mapId, 'bounds' => $bounds->toArray()];
if ($this->jsonResponse) {
return new JsonView($data);
} else {
return new HtmlView('game', $data);
}
return ['mapId' => $mapId, 'bounds' => $bounds->toArray()];
}
private function getMapBounds(): Bounds
private function getMapBounds(int $mapId): Bounds
{
$select = new Select(\Container::$dbConnection, 'maps');
$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);

View File

@ -2,15 +2,14 @@
use MapGuesser\Database\Query\Select;
use MapGuesser\Database\RawExpression;
use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Interfaces\View\IView;
use MapGuesser\Interfaces\Response\IContent;
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->columns([
@ -38,7 +37,7 @@ class MapsController implements IController
}
$data = ['maps' => $maps];
return new HtmlView('maps', $data);
return new HtmlContent('maps', $data);
}
private function formatMapAreaForHuman(float $area): array

View File

@ -2,75 +2,30 @@
use MapGuesser\Database\Query\Select;
use MapGuesser\Http\Request;
use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Position;
use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView;
use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent;
class PositionController implements IController
class PositionController
{
const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000;
private int $mapId;
public function __construct(int $mapId)
public function getPosition(array $parameters): IContent
{
$this->mapId = $mapId;
}
$mapId = (int) $parameters['mapId'];
public function run(): IView
{
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
$data = ['error' => 'No valid session found!'];
return new JsonView($data);
return new JsonContent($data);
}
if (count($_SESSION['state']['rounds']) === 0) {
$newPosition = $this->getNewPosition();
$newPosition = $this->getNewPosition($mapId);
$_SESSION['state']['rounds'][] = $newPosition;
$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 {
$rounds = count($_SESSION['state']['rounds']);
$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 = [];
do {
$place = $this->selectNewPlace($exclude);
$place = $this->selectNewPlace($mapId, $exclude);
$position = new Position($place['lat'], $place['lng']);
$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->columns(['id', 'lat', 'lng']);
$select->where('id', 'NOT IN', $exclude);
$select->where('map_id', '=', $this->mapId);
$select->where('map_id', '=', $mapId);
$numberOfPlaces = $select->count();// TODO: what if 0
$randomOffset = random_int(0, $numberOfPlaces - 1);