From ad9f3cf5a07338134a29fee4b6215c50cb8142c8 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 14 Jan 2020 23:02:40 +0100 Subject: [PATCH] add search support --- .gitignore | 1 + bin/filters.php | 22 ++++++++++++++++++++++ bin/import-game-data.php | 39 +++++++++++++++++++++++++++------------ www/.htaccess | 10 ++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 811b6ef..0f58418 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/bin/filters.php b/bin/filters.php index 04a998f..628936a 100644 --- a/bin/filters.php +++ b/bin/filters.php @@ -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; +} ?> diff --git a/bin/import-game-data.php b/bin/import-game-data.php index 467dac5..0dc4926 100755 --- a/bin/import-game-data.php +++ b/bin/import-game-data.php @@ -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 [ diff --git a/www/.htaccess b/www/.htaccess index ba3ec53..a70563d 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -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] -- 2.30.2