MAPG-73 pass mapId to GameController and PositionController
adapt JS to send mapId make separate JS for the game
This commit is contained in:
parent
1ca023e612
commit
92c35a2087
@ -3,20 +3,23 @@
|
||||
require '../main.php';
|
||||
|
||||
// very basic routing
|
||||
$host = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"];
|
||||
$host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'];
|
||||
$url = $_SERVER['REQUEST_URI'];
|
||||
if (($pos = strpos($url, '?')) !== false) {
|
||||
$url = substr($url, 0, $pos);
|
||||
}
|
||||
switch($url) {
|
||||
case '/game':
|
||||
$controller = new MapGuesser\Controller\GameController();
|
||||
$mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0;
|
||||
$controller = new MapGuesser\Controller\GameController($mapId);
|
||||
break;
|
||||
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;
|
||||
case '/position.json':
|
||||
$controller = new MapGuesser\Controller\PositionController();
|
||||
$mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0;
|
||||
$controller = new MapGuesser\Controller\PositionController($mapId);
|
||||
break;
|
||||
case '/':
|
||||
header('Location: ' . $host . '/game', true, 302);
|
||||
|
@ -51,7 +51,7 @@
|
||||
Core.startNewRound();
|
||||
};
|
||||
|
||||
xhr.open('GET', 'position.json', true);
|
||||
xhr.open('GET', 'position.json?map=' + mapId, true);
|
||||
xhr.send();
|
||||
},
|
||||
|
||||
@ -127,7 +127,7 @@
|
||||
Core.resetGame();
|
||||
};
|
||||
|
||||
xhr.open('GET', 'game.json', true);
|
||||
xhr.open('GET', 'game.json?map=' + mapId, true);
|
||||
xhr.send();
|
||||
},
|
||||
|
||||
@ -200,7 +200,7 @@
|
||||
Core.panoId = this.response.panoId;
|
||||
};
|
||||
|
||||
xhr.open('POST', 'position.json', true);
|
||||
xhr.open('POST', 'position.json?map=' + mapId, true);
|
||||
xhr.send(data);
|
||||
},
|
||||
|
@ -10,13 +10,13 @@ use MapGuesser\Interfaces\View\IView;
|
||||
|
||||
class GameController implements IController
|
||||
{
|
||||
private int $mapId;
|
||||
|
||||
private bool $jsonResponse;
|
||||
|
||||
// demo map
|
||||
private int $mapId = 1;
|
||||
|
||||
public function __construct($jsonResponse = false)
|
||||
public function __construct(int $mapId, $jsonResponse = false)
|
||||
{
|
||||
$this->mapId = $mapId;
|
||||
$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) {
|
||||
return new JsonView($data);
|
||||
|
@ -13,8 +13,12 @@ class PositionController implements IController
|
||||
const NUMBER_OF_ROUNDS = 5;
|
||||
const MAX_SCORE = 1000;
|
||||
|
||||
// demo map
|
||||
private int $mapId = 1;
|
||||
private int $mapId;
|
||||
|
||||
public function __construct(int $mapId)
|
||||
{
|
||||
$this->mapId = $mapId;
|
||||
}
|
||||
|
||||
public function run(): IView
|
||||
{
|
||||
@ -120,7 +124,7 @@ class PositionController implements IController
|
||||
$select->where('id', 'NOT IN', $exclude);
|
||||
$select->where('map_id', '=', $this->mapId);
|
||||
|
||||
$numberOfPlaces = $select->count();
|
||||
$numberOfPlaces = $select->count();// TODO: what if 0
|
||||
$randomOffset = random_int(0, $numberOfPlaces - 1);
|
||||
|
||||
$select->orderBy('id');
|
||||
|
@ -4,21 +4,25 @@ class Bounds
|
||||
{
|
||||
const ONE_DEGREE_OF_LATITUDE_IN_METER = 111132.954;
|
||||
|
||||
private float $southLat;
|
||||
private float $westLng;
|
||||
private float $southLat = 90.0;
|
||||
private float $westLng = 180.0;
|
||||
|
||||
private float $northLat;
|
||||
private float $eastLng;
|
||||
private float $northLat = -90.0;
|
||||
private float $eastLng = -180.0;
|
||||
|
||||
private bool $initialized = false;
|
||||
|
||||
public static function createWithPosition(Position $position): Bounds
|
||||
public function __construct(Position $position = null)
|
||||
{
|
||||
$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
|
||||
@ -30,19 +34,11 @@ class Bounds
|
||||
$instance->northLat = $northLat;
|
||||
$instance->eastLng = $eastLng;
|
||||
|
||||
$instance->initialized = true;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function extend(Position $position): void
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initialize($position);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$lat = $position->getLat();
|
||||
$lng = $position->getLng();
|
||||
|
||||
@ -77,10 +73,6 @@ class Bounds
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
throw new \Exception("Bounds are not initialized!");
|
||||
}
|
||||
|
||||
return [
|
||||
'south' => $this->southLat,
|
||||
'west' => $this->westLng,
|
||||
@ -93,17 +85,4 @@ class Bounds
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +48,10 @@
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
var mapId = '<?= $mapId ?>';
|
||||
var mapBounds = <?= json_encode($bounds) ?>;
|
||||
</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>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user