2020-05-17 22:30:22 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2020-05-28 21:04:53 +02:00
|
|
|
use MapGuesser\Database\Query\Select;
|
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
2020-05-17 22:30:22 +02:00
|
|
|
use MapGuesser\Util\Geo\Bounds;
|
2020-05-31 20:43:14 +02:00
|
|
|
use MapGuesser\Response\HtmlContent;
|
|
|
|
use MapGuesser\Response\JsonContent;
|
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
class GameController
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2020-05-31 20:43:14 +02:00
|
|
|
public function getGame(array $parameters): IContent
|
|
|
|
{
|
|
|
|
$mapId = (int) $parameters['mapId'];
|
|
|
|
$data = $this->prepareGame($mapId);
|
|
|
|
return new HtmlContent('game', $data);
|
|
|
|
}
|
2020-05-25 19:14:49 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
public function getGameJson(array $parameters): IContent
|
2020-05-25 19:14:49 +02:00
|
|
|
{
|
2020-05-31 20:43:14 +02:00
|
|
|
$mapId = (int) $parameters['mapId'];
|
|
|
|
$data = $this->prepareGame($mapId);
|
|
|
|
return new JsonContent($data);
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
private function prepareGame(int $mapId)
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2020-05-31 20:43:14 +02:00
|
|
|
$bounds = $this->getMapBounds($mapId);
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $mapId) {
|
2020-05-25 19:14:49 +02:00
|
|
|
$_SESSION['state'] = [
|
2020-05-31 20:43:14 +02:00
|
|
|
'mapId' => $mapId,
|
2020-05-25 19:14:49 +02:00
|
|
|
'area' => $bounds->calculateApproximateArea(),
|
|
|
|
'rounds' => []
|
|
|
|
];
|
|
|
|
}
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
return ['mapId' => $mapId, 'bounds' => $bounds->toArray()];
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
private function getMapBounds(int $mapId): Bounds
|
2020-05-25 19:14:49 +02:00
|
|
|
{
|
2020-05-28 21:04:53 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'maps');
|
|
|
|
$select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']);
|
2020-05-31 20:43:14 +02:00
|
|
|
$select->whereId($mapId);
|
2020-05-28 21:04:53 +02:00
|
|
|
|
|
|
|
$map = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
2020-05-18 00:23:45 +02:00
|
|
|
|
|
|
|
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2020-05-25 19:14:49 +02:00
|
|
|
return $bounds;
|
2020-05-17 22:30:22 +02:00
|
|
|
}
|
|
|
|
}
|