Merged in feature/MAPG-37-use-better-random-algorithm (pull request #15)

feature/MAPG-37-use-better-random-algorithm
This commit is contained in:
Bence Pőcze 2020-05-20 19:50:07 +00:00
commit f039c80eaa

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']);