add search support
authorChristian Weiske <cweiske@cweiske.de>
Tue, 14 Jan 2020 22:02:40 +0000 (23:02 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 14 Jan 2020 22:02:40 +0000 (23:02 +0100)
.gitignore
bin/filters.php
bin/import-game-data.php
www/.htaccess

index 811b6ef..0f58418 100644 (file)
@@ -5,3 +5,4 @@ www/api/v1/developers/
 www/api/v1/discover-data/
 www/api/v1/discover.json
 www/api/v1/games/*/
+www/api/v1/search-data/
index 04a998f..628936a 100644 (file)
@@ -65,6 +65,17 @@ function filterByPlayers($origGames, $numOfPlayers)
     return $filtered;
 }
 
+function filterBySearchWord($origGames, $searchWord)
+{
+    $filtered = [];
+    foreach ($origGames as $game) {
+        if (stripos($game->title, $searchWord) !== false) {
+            $filtered[] = $game;
+        }
+    }
+    return $filtered;
+}
+
 function filterLastUpdated($origGames, $limit)
 {
     $games = array_values($origGames);
@@ -90,4 +101,15 @@ function filterBestRated($origGames, $limit)
 
     return array_slice($games, 0, $limit);
 }
+
+function sortByTitle($games)
+{
+    usort(
+        $games,
+        function ($gameA, $gameB) {
+            return strcasecmp($gameA->title, $gameB->title);
+        }
+    );
+    return $games;
+}
 ?>
index 467dac5..0dc4926 100755 (executable)
@@ -124,6 +124,14 @@ writeJson(
     buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
 );
 
+$searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
+foreach (str_split($searchLetters) as $letter) {
+    $letterGames = filterBySearchWord($games, $letter);
+    writeJson(
+        'api/v1/search-data/' . $letter . '.json',
+        buildSearch($letterGames)
+    );
+}
 
 
 function buildDiscover(array $games)
@@ -214,12 +222,7 @@ function buildDiscoverCategory($name, $games)
         filterBestRated($games, 10)
     );
 
-    usort(
-        $games,
-        function ($gameA, $gameB) {
-            return strcasecmp($gameA->title, $gameB->title);
-        }
-    );
+    $games = sortByTitle($games);
     $chunks = array_chunk($games, 4);
     foreach ($chunks as $chunkGames) {
         addDiscoverRow($data, '', $chunkGames);
@@ -236,12 +239,7 @@ function buildMakeCategory($name, $games)
         'tiles' => [],
     ];
 
-    usort(
-        $games,
-        function ($gameA, $gameB) {
-            return strcasecmp($gameA->title, $gameB->title);
-        }
-    );
+    $games = sortByTitle($games);
     addDiscoverRow($data, '', $games);
 
     return $data;
@@ -523,6 +521,23 @@ function buildPurchases($game)
     return $encryptedTwice;
 }
 
+function buildSearch($games)
+{
+    $games = sortByTitle($games);
+    $results = [];
+    foreach ($games as $game) {
+        $results[] = [
+            'title' => $game->title,
+            'url'   => 'ouya://launcher/details?app=' . $game->packageName,
+            'contentRating' => $game->contentRating,
+        ];
+    }
+    return [
+        'count'   => count($results),
+        'results' => $results,
+    ];
+}
+
 function dummyEncrypt($data)
 {
     return [
index ba3ec53..a70563d 100644 (file)
@@ -22,5 +22,15 @@ RewriteRule ^api/v1/discover/(.+)$ /api/v1/discover-data/$1.json [END]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^api/v1/games/(.+)/purchases?$ /api/v1/games/purchases-empty.json [END]
 
+#search
+# q is first parameter
+RewriteCond %{QUERY_STRING} ^q=([^&]+)
+RewriteCond %{DOCUMENT_ROOT}/api/v1/search-data/%1.json -f
+RewriteRule ^api/v1/search /api/v1/search-data/%1.json? [END]
+# q is not the first parameter
+RewriteCond %{QUERY_STRING} &q=([^&]+)
+RewriteCond %{DOCUMENT_ROOT}/api/v1/search-data/%1.json -f
+RewriteRule ^api/v1/search /api/v1/search-data/%1.json? [END]
+
 #this one wants a 204 status code
 RewriteRule ^api/v1/status$ - [R=204,L]