MAPG-67 make GameController able to return JSON response

This commit is contained in:
Bence Pőcze 2020-05-26 23:19:23 +02:00
parent 7633f793d6
commit 6f8f2cf9cf
2 changed files with 15 additions and 2 deletions

View File

@ -12,6 +12,9 @@ switch($url) {
case '/game':
$controller = new MapGuesser\Controller\GameController();
break;
case '/game.json':
$controller = new MapGuesser\Controller\GameController(true);
break;
case '/position.json':
$controller = new MapGuesser\Controller\PositionController();
break;

View File

@ -2,6 +2,7 @@
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\View\HtmlView;
use MapGuesser\View\JsonView;
use MapGuesser\View\ViewBase;
use mysqli;
@ -9,12 +10,16 @@ class GameController implements ControllerInterface
{
private mysqli $mysql;
private bool $jsonResponse;
// demo map
private int $mapId = 1;
public function __construct()
public function __construct($jsonResponse = false)
{
$this->mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
$this->jsonResponse = $jsonResponse;
}
public function run(): ViewBase
@ -30,7 +35,12 @@ class GameController implements ControllerInterface
}
$data = ['bounds' => $bounds->toArray()];
return new HtmlView('game', $data);
if ($this->jsonResponse) {
return new JsonView($data);
} else {
return new HtmlView('game', $data);
}
}
private function getMapBounds(): Bounds