MAPG-47 refactor GameController and its view
This commit is contained in:
parent
8eb3dcf2e2
commit
567238c979
@ -1,28 +1,47 @@
|
|||||||
<?php namespace MapGuesser\Controller;
|
<?php namespace MapGuesser\Controller;
|
||||||
|
|
||||||
use MapGuesser\Util\Geo\Bounds;
|
use MapGuesser\Util\Geo\Bounds;
|
||||||
use MapGuesser\Util\Geo\Position;
|
|
||||||
use MapGuesser\View\HtmlView;
|
use MapGuesser\View\HtmlView;
|
||||||
use MapGuesser\View\ViewBase;
|
use MapGuesser\View\ViewBase;
|
||||||
use mysqli;
|
use mysqli;
|
||||||
|
|
||||||
class GameController implements ControllerInterface
|
class GameController implements ControllerInterface
|
||||||
{
|
{
|
||||||
|
private mysqli $mysql;
|
||||||
|
|
||||||
|
// demo map
|
||||||
|
private int $mapId = 1;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
||||||
|
}
|
||||||
|
|
||||||
public function run(): ViewBase
|
public function run(): ViewBase
|
||||||
{
|
{
|
||||||
$mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
$bounds = $this->getMapBounds();
|
||||||
|
|
||||||
// demo map
|
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
|
||||||
$mapId = 1;
|
$_SESSION['state'] = [
|
||||||
|
'mapId' => $this->mapId,
|
||||||
|
'area' => $bounds->calculateApproximateArea(),
|
||||||
|
'rounds' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $mysql->prepare('SELECT bound_south_lat, bound_west_lng, bound_north_lat, bound_east_lng FROM maps WHERE id=?');
|
$data = ['bounds' => $bounds->toArray()];
|
||||||
$stmt->bind_param("i", $mapId);
|
return new HtmlView('game', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getMapBounds(): Bounds
|
||||||
|
{
|
||||||
|
$stmt = $this->mysql->prepare('SELECT bound_south_lat, bound_west_lng, bound_north_lat, bound_east_lng FROM maps WHERE id=?');
|
||||||
|
$stmt->bind_param("i", $this->mapId);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$map = $stmt->get_result()->fetch_assoc();
|
$map = $stmt->get_result()->fetch_assoc();
|
||||||
|
|
||||||
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
||||||
|
|
||||||
$data = compact('bounds');
|
return $bounds;
|
||||||
return new HtmlView('game', $data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
var mapArea = <?= $bounds->calculateApproximateArea() ?>;
|
var mapBounds = <?= json_encode($bounds) ?>;
|
||||||
var guessMapBounds = <?= $bounds->toJson() ?>;
|
|
||||||
</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/mapguesser.js"></script>
|
||||||
|
Loading…
Reference in New Issue
Block a user