Add razer forge specific category renderings
authorChristian Weiske <cweiske@cweiske.de>
Sun, 8 Oct 2023 14:14:57 +0000 (16:14 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 8 Oct 2023 14:14:57 +0000 (16:14 +0200)
bin/import-game-data.php
www/.htaccess

index 4495dc3ea230c970a9985b46925e1fdc3c9a62e0..854b3d6762c6302a64c1c7ef4502d699fc499fdb 100755 (executable)
@@ -172,7 +172,9 @@ foreach ($developers as $developer) {
     }
 }
 
-writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
+$data = buildDiscover($games);
+writeJson('api/v1/discover-data/discover.json', $data);
+writeJson('api/v1/discover-data/discover.forge.json', convertCategoryToForge($data));
 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
 
 //make
@@ -227,26 +229,26 @@ function buildDiscover(array $games)
             'Last updated',
         ]
     );
-    writeJson(
+    writeCategoryJson(
         'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
         buildSpecialCategory('Best rated', filterBestRated($games, 99))
     );
-    writeJson(
+    writeCategoryJson(
         'api/v1/discover-data/' . categoryPath('Best rated games') . '.json',
         buildSpecialCategory('Best rated games', filterBestRatedGames($games, 99))
     );
-    writeJson(
+    writeCategoryJson(
         'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
         buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
     );
-    writeJson(
+    writeCategoryJson(
         'api/v1/discover-data/' . categoryPath('Random') . '.json',
         buildSpecialCategory(
             'Random ' . date('Y-m-d H:i'),
             filterRandom($games, 99)
         )
     );
-    writeJson(
+    writeCategoryJson(
         'api/v1/discover-data/' . categoryPath('Last updated') . '.json',
         buildSpecialCategory('Last updated', filterLastUpdated($games, 99))
     );
@@ -259,7 +261,7 @@ function buildDiscover(array $games)
     ];
     addDiscoverRow($data, 'Multiplayer', $players);
     foreach ($players as $num => $title) {
-        writeJson(
+        writeCategoryJson(
             'api/v1/discover-data/' . categoryPath($title) . '.json',
             buildDiscoverCategory(
                 $title,
@@ -280,7 +282,7 @@ function buildDiscover(array $games)
     natsort($ages);
     addDiscoverRow($data, 'Content rating', $ages);
     foreach ($ages as $num => $title) {
-        writeJson(
+        writeCategoryJson(
             'api/v1/discover-data/' . categoryPath($title) . '.json',
             buildDiscoverCategory($title, filterByAge($games, $title))
         );
@@ -291,7 +293,7 @@ function buildDiscover(array $games)
     addChunkedDiscoverRows($data, $genres, 'Genres');
 
     foreach ($genres as $genre) {
-        writeJson(
+        writeCategoryJson(
             'api/v1/discover-data/' . categoryPath($genre) . '.json',
             buildDiscoverCategory($genre, filterByGenre($games, $genre))
         );
@@ -300,7 +302,7 @@ function buildDiscover(array $games)
     $abc = array_merge(range('A', 'Z'), ['Other']);
     addChunkedDiscoverRows($data, $abc, 'Alphabetical');
     foreach ($abc as $letter) {
-        writeJson(
+        writeCategoryJson(
             'api/v1/discover-data/' . categoryPath($letter) . '.json',
             buildDiscoverCategory($letter, filterByLetter($games, $letter))
         );
@@ -325,7 +327,7 @@ function buildDiscoverCategory($name, $games)
 
     if (count($games) >= 20) {
         addDiscoverRow(
-            $data, 'Last Updated',
+            $data, 'Last updated',
             filterLastUpdated($games, 10)
         );
         addDiscoverRow(
@@ -337,10 +339,51 @@ function buildDiscoverCategory($name, $games)
 
     $games = sortByTitle($games);
     $chunks = array_chunk($games, 4);
+    $title = 'All';
     foreach ($chunks as $chunkGames) {
-        addDiscoverRow($data, '', $chunkGames);
+        addDiscoverRow($data, $title, $chunkGames);
+        $title = '';
+    }
+
+    return $data;
+}
+
+/**
+ * Modify a category to make it suitable for the Razer Forge TV
+ *
+ * - Fold rows without title into the previous row
+ * - Remove automatically generated categories ("Last updated", "Best rated")
+ *
+ * @see buildDiscoverCategory()
+ */
+function convertCategoryToForge($data, $removeAutoCategories = false)
+{
+    //merge tiles from rows without title into the previous row
+    $lastTitleRowId = null;
+    foreach ($data['rows'] as $rowId => $row) {
+        if ($row['title'] !== '') {
+            $lastTitleRowId = $rowId;
+        } else if ($lastTitleRowId !== null) {
+            $data['rows'][$lastTitleRowId]['tiles'] = array_merge(
+                $data['rows'][$lastTitleRowId]['tiles'],
+                $row['tiles']
+            );
+            unset($data['rows'][$rowId]);
+        }
     }
 
+    if ($removeAutoCategories) {
+        foreach ($data['rows'] as $rowId => $row) {
+            if ($row['title'] === 'Last updated'
+                || $row['title'] === 'Best rated'
+            ) {
+                unset($data['rows'][$rowId]);
+            }
+        }
+    }
+
+    $data['rows'] = array_values($data['rows']);
+
     return $data;
 }
 
@@ -1077,6 +1120,15 @@ function writeJson($path, $data)
     );
 }
 
+function writeCategoryJson($path, $data)
+{
+    writeJson($path, $data);
+
+    $forgePath = str_replace('.json', '.forge.json', $path);
+    $forgeData = convertCategoryToForge($data, true);
+    writeJson($forgePath, $forgeData);
+}
+
 function error($msg)
 {
     fwrite(STDERR, $msg . "\n");
index 7469bae162be2d600b989957a8e54569537b406f..8f2efe3a07ff0592b324ca8605211b9e822d81b7 100644 (file)
@@ -30,7 +30,18 @@ RewriteCond %{QUERY_STRING} &only=([^&]+)
 RewriteCond %{DOCUMENT_ROOT}/api/v1/developers/$1/products/%1.json -f
 RewriteRule ^api/v1/developers/(.+)/products/ /api/v1/developers/$1/products/%1.json? [END]
 
+#discover homepage
+RewriteCond %{HTTP_USER_AGENT} "Forge"
+RewriteRule ^api/v1/discover/?$ /api/v1/discover-data/discover.forge.json [END]
+
 RewriteRule ^api/v1/discover/?$ /api/v1/discover-data/discover.json [END]
+
+#discover category
+RewriteCond %{HTTP_USER_AGENT} "Forge"
+RewriteCond %{REQUEST_URI} ^/api/v1/discover/(.+)$
+RewriteCond %{DOCUMENT_ROOT}/api/v1/discover-data/$1.forge.json -f
+RewriteRule ^api/v1/discover/(.+)$ /api/v1/discover-data/$1.forge.json [END]
+
 RewriteRule ^api/v1/discover/(.+)$ /api/v1/discover-data/$1.json [END]
 
 #if directoryslash is on, gamers gets redirected to gamers/ (dir index)