nearby.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. Coordinate decimal places to earth resolution
  4. decimal
  5. places degrees distance
  6. ------- ------- --------
  7. 0 1 111 km
  8. 1 0.1 11.1 km
  9. 2 0.01 1.11 km
  10. 3 0.001 111 m
  11. 4 0.0001 11.1 m
  12. 5 0.00001 1.11 m
  13. 6 0.000001 11.1 cm
  14. 7 0.0000001 1.11 cm
  15. 8 0.00000001 1.11 mm
  16. */
  17. require 'required.php';
  18. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  19. if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
  20. sendError("Missing information.", true);
  21. }
  22. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{3,}/', $VARS['lat'])) {
  23. sendError("Latitude (lat) is in the wrong format.", true);
  24. }
  25. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{3,}/', $VARS['long'])) {
  26. sendError("Longitude (long) is in the wrong format.", true);
  27. }
  28. $radius = .25;
  29. if (!is_empty($VARS['radius']) && is_numeric($VARS['radius'])) {
  30. $radius = intval($VARS['radius']);
  31. }
  32. $userlocation = GeoLocation::fromDegrees($VARS['lat'], $VARS['long']);
  33. $searchbounds = $userlocation->boundingCoordinates($radius, 'miles');
  34. $people = $database->select('players', ['uuid', 'level', 'nickname', 'teamid', 'energy', 'maxenergy'], ['AND' => [
  35. 'latitude[>]' => $searchbounds[0]->getLatitudeInDegrees(),
  36. 'latitude[<]' => $searchbounds[1]->getLatitudeInDegrees(),
  37. 'longitude[>]' => $searchbounds[0]->getLongitudeInDegrees(),
  38. 'longitude[<]' => $searchbounds[1]->getLongitudeInDegrees(),
  39. 'lastping[>]' => date('Y-m-d H:i:s', strtotime('-1 minute'))],
  40. "LIMIT" => 20
  41. ]);
  42. $out = ["status" => "OK", "people" => []];
  43. foreach ($people as $person) {
  44. $out['people'][] = ["uuid" => $person["uuid"], "name" => $person["nickname"], "team" => $person["teamid"]];
  45. }
  46. echo json_encode($out);