From a33244fb5652fbe969da6094b2c79fe6bda25f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Thu, 28 May 2020 21:04:53 +0200 Subject: [PATCH] MAPG-43 rewrite controllers to use the new DB classes --- src/Controller/GameController.php | 16 +++++------ src/Controller/PositionController.php | 39 +++++++++------------------ 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/Controller/GameController.php b/src/Controller/GameController.php index 67310c2..9f38af2 100644 --- a/src/Controller/GameController.php +++ b/src/Controller/GameController.php @@ -1,16 +1,15 @@ mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); - $this->jsonResponse = $jsonResponse; } @@ -46,10 +43,11 @@ class GameController implements IController 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(); + $select = new Select(\Container::$dbConnection, 'maps'); + $select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']); + $select->whereId($this->mapId); + + $map = $select->execute()->fetch(IResultSet::FETCH_ASSOC); $bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']); diff --git a/src/Controller/PositionController.php b/src/Controller/PositionController.php index 9687a72..c916f06 100644 --- a/src/Controller/PositionController.php +++ b/src/Controller/PositionController.php @@ -1,10 +1,12 @@ mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); - } - public function run(): IView { if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) { @@ -121,30 +116,20 @@ class PositionController implements IController private function selectNewPlace(array $exclude): array { - $condition = ''; - $params = ['i', &$this->mapId]; - if (($numExcluded = count($exclude)) > 0) { - $condition .= ' AND id NOT IN (' . implode(',', array_fill(0, $numExcluded, '?')) . ')'; - $params[0] .= str_repeat('i', $numExcluded); - foreach ($exclude as &$placeId) { - $params[] = &$placeId; - } - } + $select = new Select(\Container::$dbConnection, 'places'); + $select->columns(['id', 'lat', 'lng']); + $select->where('id', 'NOT IN', $exclude); + $select->where('map_id', '=', $this->mapId); - $stmt = $this->mysql->prepare('SELECT COUNT(*) AS num FROM places WHERE map_id=? ' . $condition . ''); - call_user_func_array([$stmt, 'bind_param'], $params); - $stmt->execute(); - $numberOfPlaces = $stmt->get_result()->fetch_assoc()['num']; + $numberOfPlaces = $select->count(); $randomOffset = random_int(0, $numberOfPlaces - 1); - $params[0] .= 'i'; - $params[] = &$randomOffset; + $select->orderBy('id'); + $select->limit(1, $randomOffset); - $stmt = $this->mysql->prepare('SELECT id, lat, lng FROM places WHERE map_id=? ' . $condition . ' ORDER BY id LIMIT 1 OFFSET ?'); - call_user_func_array([$stmt, 'bind_param'], $params); - $stmt->execute(); + $place = $select->execute()->fetch(IResultSet::FETCH_ASSOC); - return $stmt->get_result()->fetch_assoc(); + return $place; } private function getPanorama(Position $position): ?string