MAPG-73 pass mapId to GameController and PositionController
adapt JS to send mapId make separate JS for the game
This commit is contained in:
1 parent
1ca023e612
commit
92c35a2087
6 files changed
+38
-51
No files matched your search
@@ -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');
|
||||
|
||||
+14
-35
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user