MAPG-37 use random_int() for selecting the position

This commit is contained in:
Bence Pőcze 2020-05-20 21:48:37 +02:00
parent b673be741a
commit 6116b94527

View File

@ -14,10 +14,16 @@ class GetNewPosition implements ControllerInterface
// demo map // demo map
$mapId = 1; $mapId = 1;
// using RAND() for the time being, could be changed in the future $stmt = $mysql->prepare('SELECT COUNT(*) AS num FROM places WHERE map_id=?');
$stmt = $mysql->prepare('SELECT lat, lng FROM places WHERE map_id=? ORDER BY RAND() LIMIT 1');
$stmt->bind_param("i", $mapId); $stmt->bind_param("i", $mapId);
$stmt->execute(); $stmt->execute();
$numberOfPlaces = $stmt->get_result()->fetch_assoc()['num'];
$randomOffset = random_int(0, $numberOfPlaces-1);
$stmt = $mysql->prepare('SELECT lat, lng FROM places WHERE map_id=? ORDER BY id LIMIT 1 OFFSET ?');
$stmt->bind_param("ii", $mapId, $randomOffset);
$stmt->execute();
$place = $stmt->get_result()->fetch_assoc(); $place = $stmt->get_result()->fetch_assoc();
$position = new Position($place['lat'], $place['lng']); $position = new Position($place['lat'], $place['lng']);