2020-05-17 22:30:22 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
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-06-06 01:11:45 +02:00
|
|
|
use MapGuesser\Repository\MapRepository;
|
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;
|
|
|
|
|
2020-06-06 01:11:45 +02:00
|
|
|
private MapRepository $mapRepository;
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function __construct(IRequest $request)
|
2020-05-31 20:43:14 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$this->request = $request;
|
2020-06-06 01:11:45 +02:00
|
|
|
$this->mapRepository = new MapRepository();
|
2020-06-09 00:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-06 01:11:45 +02:00
|
|
|
$map = $this->mapRepository->getById($mapId);
|
|
|
|
|
|
|
|
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$session = $this->request->session();
|
|
|
|
|
2020-06-10 20:34:29 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-06-09 00:56:00 +02:00
|
|
|
$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-06-06 01:11:45 +02:00
|
|
|
return ['mapId' => $mapId, 'mapName' => $map['name'], 'bounds' => $bounds->toArray()];
|
2020-05-17 22:30:22 +02:00
|
|
|
}
|
|
|
|
}
|