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;
|
2020-05-28 00:27:35 +02:00
|
|
|
use MapGuesser\Interfaces\Controller\IController;
|
2020-05-28 21:04:53 +02:00
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
2020-05-17 22:30:22 +02:00
|
|
|
use MapGuesser\Util\Geo\Bounds;
|
2020-05-19 03:17:40 +02:00
|
|
|
use MapGuesser\View\HtmlView;
|
2020-05-26 23:19:23 +02:00
|
|
|
use MapGuesser\View\JsonView;
|
2020-05-28 00:27:35 +02:00
|
|
|
use MapGuesser\Interfaces\View\IView;
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2020-05-28 00:27:35 +02:00
|
|
|
class GameController implements IController
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2020-05-30 15:33:28 +02:00
|
|
|
private int $mapId;
|
2020-05-26 23:19:23 +02:00
|
|
|
|
2020-05-30 15:33:28 +02:00
|
|
|
private bool $jsonResponse;
|
2020-05-25 19:14:49 +02:00
|
|
|
|
2020-05-30 15:33:28 +02:00
|
|
|
public function __construct(int $mapId, $jsonResponse = false)
|
2020-05-25 19:14:49 +02:00
|
|
|
{
|
2020-05-30 15:33:28 +02:00
|
|
|
$this->mapId = $mapId;
|
2020-05-26 23:19:23 +02:00
|
|
|
$this->jsonResponse = $jsonResponse;
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 00:27:35 +02:00
|
|
|
public function run(): IView
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2020-05-25 19:14:49 +02:00
|
|
|
$bounds = $this->getMapBounds();
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2020-05-25 19:14:49 +02:00
|
|
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
|
|
|
|
$_SESSION['state'] = [
|
|
|
|
'mapId' => $this->mapId,
|
|
|
|
'area' => $bounds->calculateApproximateArea(),
|
|
|
|
'rounds' => []
|
|
|
|
];
|
|
|
|
}
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2020-05-30 15:33:28 +02:00
|
|
|
$data = ['mapId' => $this->mapId, 'bounds' => $bounds->toArray()];
|
2020-05-26 23:19:23 +02:00
|
|
|
|
|
|
|
if ($this->jsonResponse) {
|
|
|
|
return new JsonView($data);
|
|
|
|
} else {
|
|
|
|
return new HtmlView('game', $data);
|
|
|
|
}
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getMapBounds(): Bounds
|
|
|
|
{
|
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']);
|
|
|
|
$select->whereId($this->mapId);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|