From 6f8f2cf9cf00273ea207a29ee7287c593cd69d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 26 May 2020 23:19:23 +0200 Subject: [PATCH] MAPG-67 make GameController able to return JSON response --- public/index.php | 3 +++ src/Controller/GameController.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index a20f4d7..aa06a8a 100644 --- a/public/index.php +++ b/public/index.php @@ -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; diff --git a/src/Controller/GameController.php b/src/Controller/GameController.php index 43b04e0..c036adb 100644 --- a/src/Controller/GameController.php +++ b/src/Controller/GameController.php @@ -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