diff --git a/src/Controller/GameController.php b/src/Controller/GameController.php index 4e74cd0..43b04e0 100644 --- a/src/Controller/GameController.php +++ b/src/Controller/GameController.php @@ -1,28 +1,47 @@ mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); + } + public function run(): ViewBase { - $mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); + $bounds = $this->getMapBounds(); - // demo map - $mapId = 1; + if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) { + $_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=?'); - $stmt->bind_param("i", $mapId); + $data = ['bounds' => $bounds->toArray()]; + 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(); $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']); - $data = compact('bounds'); - return new HtmlView('game', $data); + return $bounds; } } diff --git a/views/game.php b/views/game.php index 07a03e0..fee7284 100644 --- a/views/game.php +++ b/views/game.php @@ -48,8 +48,7 @@