MAPG-43 rewrite controllers to use the new DB classes

This commit is contained in:
Bence Pőcze 2020-05-28 21:04:53 +02:00
parent 5a41348cc3
commit a33244fb56
2 changed files with 19 additions and 36 deletions

View File

@ -1,16 +1,15 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Controller\IController; use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Bounds; use MapGuesser\Util\Geo\Bounds;
use MapGuesser\View\HtmlView; use MapGuesser\View\HtmlView;
use MapGuesser\View\JsonView; use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView; use MapGuesser\Interfaces\View\IView;
use mysqli;
class GameController implements IController class GameController implements IController
{ {
private mysqli $mysql;
private bool $jsonResponse; private bool $jsonResponse;
// demo map // demo map
@ -18,8 +17,6 @@ class GameController implements IController
public function __construct($jsonResponse = false) public function __construct($jsonResponse = false)
{ {
$this->mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
$this->jsonResponse = $jsonResponse; $this->jsonResponse = $jsonResponse;
} }
@ -46,10 +43,11 @@ class GameController implements IController
private function getMapBounds(): Bounds 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=?'); $select = new Select(\Container::$dbConnection, 'maps');
$stmt->bind_param("i", $this->mapId); $select->columns(['bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng']);
$stmt->execute(); $select->whereId($this->mapId);
$map = $stmt->get_result()->fetch_assoc();
$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']); $bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);

View File

@ -1,10 +1,12 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select;
use MapGuesser\Http\Request;
use MapGuesser\Interfaces\Controller\IController; use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Position; use MapGuesser\Util\Geo\Position;
use MapGuesser\View\JsonView; use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView; use MapGuesser\Interfaces\View\IView;
use mysqli;
use RestClient\Client; use RestClient\Client;
class PositionController implements IController class PositionController implements IController
@ -12,16 +14,9 @@ class PositionController implements IController
const NUMBER_OF_ROUNDS = 5; const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000; const MAX_SCORE = 1000;
private mysqli $mysql;
// demo map // demo map
private int $mapId = 1; 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(): IView public function run(): IView
{ {
if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) { if (!isset($_SESSION['state']) || $_SESSION['state']['mapId'] !== $this->mapId) {
@ -121,30 +116,20 @@ class PositionController implements IController
private function selectNewPlace(array $exclude): array private function selectNewPlace(array $exclude): array
{ {
$condition = ''; $select = new Select(\Container::$dbConnection, 'places');
$params = ['i', &$this->mapId]; $select->columns(['id', 'lat', 'lng']);
if (($numExcluded = count($exclude)) > 0) { $select->where('id', 'NOT IN', $exclude);
$condition .= ' AND id NOT IN (' . implode(',', array_fill(0, $numExcluded, '?')) . ')'; $select->where('map_id', '=', $this->mapId);
$params[0] .= str_repeat('i', $numExcluded);
foreach ($exclude as &$placeId) {
$params[] = &$placeId;
}
}
$stmt = $this->mysql->prepare('SELECT COUNT(*) AS num FROM places WHERE map_id=? ' . $condition . ''); $numberOfPlaces = $select->count();
call_user_func_array([$stmt, 'bind_param'], $params);
$stmt->execute();
$numberOfPlaces = $stmt->get_result()->fetch_assoc()['num'];
$randomOffset = random_int(0, $numberOfPlaces - 1); $randomOffset = random_int(0, $numberOfPlaces - 1);
$params[0] .= 'i'; $select->orderBy('id');
$params[] = &$randomOffset; $select->limit(1, $randomOffset);
$stmt = $this->mysql->prepare('SELECT id, lat, lng FROM places WHERE map_id=? ' . $condition . ' ORDER BY id LIMIT 1 OFFSET ?'); $place = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
call_user_func_array([$stmt, 'bind_param'], $params);
$stmt->execute();
return $stmt->get_result()->fetch_assoc(); return $place;
} }
private function getPanorama(Position $position): ?string private function getPanorama(Position $position): ?string