From 8b0b245c34eb98a1e6c621f93de817904f3c9968 Mon Sep 17 00:00:00 2001 From: Boris Date: Tue, 15 Nov 2022 17:02:53 +0100 Subject: [PATCH] La fucking biourotique --- config.php | 2 + lib/Gchange.class.php | 133 +++++++++++++++++++++++++++++++++----------------- map.php | 4 +- 3 files changed, 92 insertions(+), 47 deletions(-) diff --git a/config.php b/config.php index 8c4214f..79cd99e 100644 --- a/config.php +++ b/config.php @@ -18,6 +18,8 @@ define('SONAR_DURATION', 5); define("LAT_ASTROPORT_ONE", 44.22484418236386); define("LON_ASTROPORT_ONE", 1.6395813014177014); +define('MAX_NEARBY_PLACES', 15); + class Player { private $pubkey; diff --git a/lib/Gchange.class.php b/lib/Gchange.class.php index 6d425f9..a92eb03 100644 --- a/lib/Gchange.class.php +++ b/lib/Gchange.class.php @@ -191,47 +191,12 @@ class Gchange { return $result->hits->hits; } - public function getPlacesNearUser ($user, $radius) { + public function getPlacesNearUser ($user, $radius, $maxPlacesNb) { - return $this->getNearbyPlaces($user->getLat(), $user->getLon(), $radius); + return $this->getNearbyPlaces($user->getLat(), $user->getLon(), $radius, null, $maxPlacesNb); } - public function getPlaceDetails ($placeId) { - - $placeDetailsCacheDir = 'place/details/'; - $placeDetailsCacheFile = $placeId . '.json'; - - try { - - $json = $this->getJsonFromCache($placeDetailsCacheDir, $placeDetailsCacheFile, $this->cacheLongevity['placeDetails']); - - } catch (Exception $errorMsgs) { - - try { - - $json = $this->fetchJson('/page/record/' + $placeId); - - $this->cacheJson($placeDetailsCacheDir, $placeDetailsCacheFile, $json); - - } catch (Exception $errorMsgs) { - - try { - - $json = $this->getJsonFromCache($placeDetailsCacheDir, $placeDetailsCacheFile, null); - - } catch (Exception $errorMsgs) { - - throw new Exception('Pas pu trouver les détails pour la page ' . $placeId); - } - - } - } - - return json_decode($json); - - } - - public function getNearbyPlaces ($lat, $lon, $maxDistance, $minDistance = NULL) { + public function getNearbyPlaces ($lat, $lon, $maxDistance, $minDistance = NULL, $maxPlacesNb) { $placesCacheDir = 'places-nearby/geopoint/' . $maxDistance . 'km/'; $placesCacheFile = $lat . ',' . $lon . '.json'; @@ -263,11 +228,10 @@ class Gchange { try { - $n = 50; + $n = (string) $maxPlacesNb; $queryParams = [ 'size' => $n, - // 'fields' => ['_id'], 'query' => [ 'bool' => [ 'filter' => [ @@ -287,14 +251,19 @@ class Gchange { ]; $json = $this->fetchJson('/page/record/_search', $queryParams); - - // cache nearby places index - $this->cacheJson($placesCacheDir, $placesCacheFile, $json); - - // cache each nearby place details $result = json_decode($json); + $resultClone = $this->filterIds($result); + + // cache nearby places index + $this->cacheJson($placesCacheDir, $placesCacheFile, json_encode($resultClone)); + + // cache each nearby place details + + // ob_get_clean(); + // echo '
'; print_r($resultClone); echo '
'; + // echo '
'; print_r($result); echo '
'; foreach ($result->hits->hits as $place) { $nearbyPlaces[] = $place; @@ -333,6 +302,80 @@ class Gchange { return $nearbyPlaces; } + + private function filterIds ($r) { + + // $result = clone $r; + + $result = new stdClass; + + $result->took = $r->took; + $result->timed_out = $r->timed_out; + $result->_shards = new stdClass; + $result->_shards->total = $r->_shards->total; + $result->_shards->successful = $r->_shards->successful; + $result->_shards->failed = $r->_shards->failed; + $result->hits = new stdClass; + $result->hits->total = $r->hits->total; + $result->hits->max_score = $r->hits->max_score; + $result->hits->hits = array(); + + $rHitsNb = count($r->hits->hits); + + for ($i = 0; $i < $rHitsNb; $i++) { + + $result->hits->hits[$i] = new stdClass; + + $result->hits->hits[$i]->_id = $r->hits->hits[$i]->_id; + } + + return $result; + } + + /* + private function filterIds (&$item) { + + $itemId = $item->_id; + $item = new stdClass; + $item->_id = $itemId; + } + */ + + public function getPlaceDetails ($placeId) { + + $placeDetailsCacheDir = 'place/details/'; + $placeDetailsCacheFile = $placeId . '.json'; + + try { + + $json = $this->getJsonFromCache($placeDetailsCacheDir, $placeDetailsCacheFile, $this->cacheLongevity['placeDetails']); + + } catch (Exception $errorMsgs) { + + try { + + $json = $this->fetchJson('/page/record/' + $placeId); + + $this->cacheJson($placeDetailsCacheDir, $placeDetailsCacheFile, $json); + + } catch (Exception $errorMsgs) { + + try { + + $json = $this->getJsonFromCache($placeDetailsCacheDir, $placeDetailsCacheFile, null); + + } catch (Exception $errorMsgs) { + + throw new Exception('Pas pu trouver les détails pour la page ' . $placeId); + } + + } + } + + return json_decode($json); + + } + /* public function getNearbyRecords ($lat, $lon, $maxDistance, $minDistance = NULL) { diff --git a/map.php b/map.php index 9578cc3..171ab18 100644 --- a/map.php +++ b/map.php @@ -100,8 +100,8 @@ echo ' '; */ - - $places = $gchange->getPlacesNearUser($player, $_SESSION['radius']); + + $places = $gchange->getPlacesNearUser($player, $_SESSION['radius'], MAX_NEARBY_PLACES); $selectedPlace = NULL;