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;
use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\View\HtmlView;
use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView;
use mysqli;
class GameController implements IController
{
private mysqli $mysql;
private bool $jsonResponse;
// demo map
@ -18,8 +17,6 @@ class GameController implements IController
public function __construct($jsonResponse = false)
{
$this->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']);

View File

@ -1,10 +1,12 @@
<?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select;
use MapGuesser\Http\Request;
use MapGuesser\Interfaces\Controller\IController;
use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Position;
use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView;
use mysqli;
use RestClient\Client;
class PositionController implements IController
@ -12,16 +14,9 @@ class PositionController implements IController
const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000;
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(): 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