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-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-07-05 00:58:03 +02:00
|
|
|
|
|
|
|
return new HtmlContent('game', $this->prepareGame($mapId));
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
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-07-05 00:58:03 +02:00
|
|
|
|
|
|
|
return new JsonContent($this->prepareGame($mapId));
|
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);
|
|
|
|
|
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-06-19 18:52:07 +02:00
|
|
|
'area' => $map->getArea(),
|
2020-05-25 19:14:49 +02:00
|
|
|
'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-19 18:52:07 +02:00
|
|
|
return ['mapId' => $mapId, 'mapName' => $map->getName(), 'bounds' => $map->getBounds()->toArray()];
|
2020-05-17 22:30:22 +02:00
|
|
|
}
|
|
|
|
}
|