MAPG-73 pass mapId to GameController and PositionController

adapt JS to send mapId
make separate JS for the game
This commit is contained in:
Bence Pőcze 2020-05-30 15:33:28 +02:00
parent 1ca023e612
commit 92c35a2087
6 changed files with 38 additions and 51 deletions

View File

@ -3,20 +3,23 @@
require '../main.php'; require '../main.php';
// very basic routing // very basic routing
$host = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"]; $host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'];
$url = $_SERVER['REQUEST_URI']; $url = $_SERVER['REQUEST_URI'];
if (($pos = strpos($url, '?')) !== false) { if (($pos = strpos($url, '?')) !== false) {
$url = substr($url, 0, $pos); $url = substr($url, 0, $pos);
} }
switch($url) { switch($url) {
case '/game': case '/game':
$controller = new MapGuesser\Controller\GameController(); $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0;
$controller = new MapGuesser\Controller\GameController($mapId);
break; break;
case '/game.json': case '/game.json':
$controller = new MapGuesser\Controller\GameController(true); $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0;
$controller = new MapGuesser\Controller\GameController($mapId, true);
break; break;
case '/position.json': case '/position.json':
$controller = new MapGuesser\Controller\PositionController(); $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0;
$controller = new MapGuesser\Controller\PositionController($mapId);
break; break;
case '/': case '/':
header('Location: ' . $host . '/game', true, 302); header('Location: ' . $host . '/game', true, 302);

View File

@ -51,7 +51,7 @@
Core.startNewRound(); Core.startNewRound();
}; };
xhr.open('GET', 'position.json', true); xhr.open('GET', 'position.json?map=' + mapId, true);
xhr.send(); xhr.send();
}, },
@ -127,7 +127,7 @@
Core.resetGame(); Core.resetGame();
}; };
xhr.open('GET', 'game.json', true); xhr.open('GET', 'game.json?map=' + mapId, true);
xhr.send(); xhr.send();
}, },
@ -200,7 +200,7 @@
Core.panoId = this.response.panoId; Core.panoId = this.response.panoId;
}; };
xhr.open('POST', 'position.json', true); xhr.open('POST', 'position.json?map=' + mapId, true);
xhr.send(data); xhr.send(data);
}, },

View File

@ -10,13 +10,13 @@ use MapGuesser\Interfaces\View\IView;
class GameController implements IController class GameController implements IController
{ {
private int $mapId;
private bool $jsonResponse; private bool $jsonResponse;
// demo map public function __construct(int $mapId, $jsonResponse = false)
private int $mapId = 1;
public function __construct($jsonResponse = false)
{ {
$this->mapId = $mapId;
$this->jsonResponse = $jsonResponse; $this->jsonResponse = $jsonResponse;
} }
@ -32,7 +32,7 @@ class GameController implements IController
]; ];
} }
$data = ['bounds' => $bounds->toArray()]; $data = ['mapId' => $this->mapId, 'bounds' => $bounds->toArray()];
if ($this->jsonResponse) { if ($this->jsonResponse) {
return new JsonView($data); return new JsonView($data);

View File

@ -13,8 +13,12 @@ class PositionController implements IController
const NUMBER_OF_ROUNDS = 5; const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000; const MAX_SCORE = 1000;
// demo map private int $mapId;
private int $mapId = 1;
public function __construct(int $mapId)
{
$this->mapId = $mapId;
}
public function run(): IView public function run(): IView
{ {
@ -120,7 +124,7 @@ class PositionController implements IController
$select->where('id', 'NOT IN', $exclude); $select->where('id', 'NOT IN', $exclude);
$select->where('map_id', '=', $this->mapId); $select->where('map_id', '=', $this->mapId);
$numberOfPlaces = $select->count(); $numberOfPlaces = $select->count();// TODO: what if 0
$randomOffset = random_int(0, $numberOfPlaces - 1); $randomOffset = random_int(0, $numberOfPlaces - 1);
$select->orderBy('id'); $select->orderBy('id');

View File

@ -4,21 +4,25 @@ class Bounds
{ {
const ONE_DEGREE_OF_LATITUDE_IN_METER = 111132.954; const ONE_DEGREE_OF_LATITUDE_IN_METER = 111132.954;
private float $southLat; private float $southLat = 90.0;
private float $westLng; private float $westLng = 180.0;
private float $northLat; private float $northLat = -90.0;
private float $eastLng; private float $eastLng = -180.0;
private bool $initialized = false; public function __construct(Position $position = null)
public static function createWithPosition(Position $position): Bounds
{ {
$instance = new static(); if ($position === null) {
return;
}
$instance->initialize($position); $lat = $position->getLat();
$lng = $position->getLng();
return $instance; $this->northLat = $lat;
$this->westLng = $lng;
$this->southLat = $lat;
$this->eastLng = $lng;
} }
public static function createDirectly(float $southLat, float $westLng, float $northLat, float $eastLng): Bounds public static function createDirectly(float $southLat, float $westLng, float $northLat, float $eastLng): Bounds
@ -30,19 +34,11 @@ class Bounds
$instance->northLat = $northLat; $instance->northLat = $northLat;
$instance->eastLng = $eastLng; $instance->eastLng = $eastLng;
$instance->initialized = true;
return $instance; return $instance;
} }
public function extend(Position $position): void public function extend(Position $position): void
{ {
if (!$this->initialized) {
$this->initialize($position);
return;
}
$lat = $position->getLat(); $lat = $position->getLat();
$lng = $position->getLng(); $lng = $position->getLng();
@ -77,10 +73,6 @@ class Bounds
public function toArray(): array public function toArray(): array
{ {
if (!$this->initialized) {
throw new \Exception("Bounds are not initialized!");
}
return [ return [
'south' => $this->southLat, 'south' => $this->southLat,
'west' => $this->westLng, 'west' => $this->westLng,
@ -93,17 +85,4 @@ class Bounds
{ {
return json_encode($this->toArray()); return json_encode($this->toArray());
} }
private function initialize(Position $position)
{
$lat = $position->getLat();
$lng = $position->getLng();
$this->northLat = $lat;
$this->westLng = $lng;
$this->southLat = $lat;
$this->eastLng = $lng;
$this->initialized = true;
}
} }

View File

@ -48,9 +48,10 @@
</div> </div>
</div> </div>
<script> <script>
var mapId = '<?= $mapId ?>';
var mapBounds = <?= json_encode($bounds) ?>; var mapBounds = <?= json_encode($bounds) ?>;
</script> </script>
<script src="https://maps.googleapis.com/maps/api/js?key=<?= $_ENV['GOOGLE_MAPS_JS_API_KEY'] ?>"></script> <script src="https://maps.googleapis.com/maps/api/js?key=<?= $_ENV['GOOGLE_MAPS_JS_API_KEY'] ?>"></script>
<script src="static/js/mapguesser.js"></script> <script src="static/js/game.js"></script>
</body> </body>
</html> </html>