MAPG-10 add GetNewPosition controller

remove position selecing from GuessController
adapt GuessController to the new view-controller concept
This commit is contained in:
Bence Pőcze 2020-05-19 03:17:40 +02:00
parent 61cde2d40b
commit 88e8a4e9e4
3 changed files with 34 additions and 6 deletions

View File

@ -0,0 +1,28 @@
<?php namespace MapGuesser\Controller;
use MapGuesser\Util\Geo\Position;
use MapGuesser\View\JsonView;
use MapGuesser\View\ViewBase;
use mysqli;
class GetNewPosition implements ControllerInterface
{
public function run(): ViewBase
{
$mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
// demo map
$mapId = 1;
// using RAND() for the time being, could be changed in the future
$stmt = $mysql->prepare('SELECT lat, lng FROM places WHERE map_id=? ORDER BY RAND() LIMIT 1');
$stmt->bind_param("i", $mapId);
$stmt->execute();
$place = $stmt->get_result()->fetch_assoc();
$position = new Position($place['lat'], $place['lng']);
$data = ['position' => $position->toArray()];
return new JsonView($data);
}
}

View File

@ -2,13 +2,13 @@
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Geo\Position;
use MapGuesser\View\HtmlView;
use MapGuesser\View\ViewBase;
use mysqli;
class GuessController extends BaseController
class GuessController implements ControllerInterface
{
protected string $view = 'guess';
protected function operate() : void
public function run(): ViewBase
{
$mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
@ -29,6 +29,7 @@ class GuessController extends BaseController
$realPosition = new Position($place['lat'], $place['lng']);
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
$this->variables = compact('realPosition', 'bounds');
$data = compact('bounds');
return new HtmlView('guess', $data);
}
}

View File

@ -14,7 +14,6 @@
</div>
</div>
<script>
var realPosition = <?= $realPosition->toJson() ?>;
var guessMapBounds = <?= $bounds->toJson() ?>;
</script>
<script src="static/js/mapguesser.js" async defer></script>