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-06-09 00:56:00 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
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-06-09 00:56:00 +02:00
|
|
|
private IRequest $request;
|
|
|
|
|
|
|
|
public function __construct(IRequest $request)
|
2020-05-31 20:43:14 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGame(): IContent
|
|
|
|
{
|
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2020-05-31 20:43:14 +02:00
|
|
|
$data = $this->prepareGame($mapId);
|
|
|
|
return new HtmlContent('game', $data);
|
|
|
|
}
|
2020-05-25 19:14:49 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function getGameJson(): IContent
|
2020-05-25 19:14:49 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2020-05-31 20:43:14 +02:00
|
|
|
$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-06-09 00:56:00 +02:00
|
|
|
$session = $this->request->session();
|
|
|
|
|
|
|
|
if (($state = $session->get('state')) && $state['mapId'] !== $mapId) {
|
|
|
|
$session->set('state', [
|
2020-05-31 20:43:14 +02:00
|
|
|
'mapId' => $mapId,
|
2020-05-25 19:14:49 +02:00
|
|
|
'area' => $bounds->calculateApproximateArea(),
|
|
|
|
'rounds' => []
|
2020-06-09 00:56:00 +02:00
|
|
|
]);
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|