Do not crash when only few games exist
[stouyapi.git] / bin / build-html.php
index b4561e696c8305919eaca2b16d2fd9509d7f0ea7..c00f811cef8a6ebbb65575d29830375a6ba6bba5 100755 (executable)
@@ -7,6 +7,13 @@
  */
 require_once __DIR__ . '/functions.php';
 
  */
 require_once __DIR__ . '/functions.php';
 
+//default configuration values
+$GLOBALS['pushToMyOuyaUrl'] = '../push-to-my-ouya.php';
+$cfgFile = __DIR__ . '/../config.php';
+if (file_exists($cfgFile)) {
+    include $cfgFile;
+}
+
 $wwwDir = __DIR__ . '/../www/';
 $discoverDir = __DIR__ . '/../www/api/v1/discover-data/';
 $wwwDiscoverDir = $wwwDir . 'discover/';
 $wwwDir = __DIR__ . '/../www/';
 $discoverDir = __DIR__ . '/../www/api/v1/discover-data/';
 $wwwDiscoverDir = $wwwDir . 'discover/';
@@ -39,11 +46,52 @@ foreach (glob($discoverDir . '*.json') as $discoverFile) {
     );
 }
 
     );
 }
 
+file_put_contents(
+    $wwwDiscoverDir . 'allgames.htm',
+    renderAllGamesList(glob($gameDetailsDir . '*.json'))
+);
+
+
+function renderAllGamesList($detailsFiles)
+{
+    $games = [];
+    foreach ($detailsFiles as $gameDataFile) {
+        $json = json_decode(file_get_contents($gameDataFile));
+        $games[] = (object) [
+            'packageName'  => basename($gameDataFile, '.json'),
+            'title'        => $json->title,
+            'genres'       => $json->genres,
+            'developer'    => $json->developer->name,
+            'developerUrl' => $json->stouyapi->{'developer-url'} ?? null,
+            'suggestedAge' => $json->suggestedAge,
+            'apkVersion'   => $json->version->number,
+            'apkTimestamp' => $json->version->publishedAt,
+            'players'      => $json->gamerNumbers,
+            'detailUrl'    => '../game/' . str_replace(
+                        'ouya://launcher/details?app=',
+                        '',
+                        basename($gameDataFile, '.json')
+                    ) . '.htm',
+        ];
+    }
+    $navLinks = [
+        './' => 'back',
+    ];
+
+    $allGamesTemplate = __DIR__ . '/../data/templates/allgames.tpl.php';
+    ob_start();
+    include $allGamesTemplate;
+    $html = ob_get_contents();
+    ob_end_clean();
+
+    return $html;
+}
+
 function renderDiscoverFile($discoverFile)
 {
     $json = json_decode(file_get_contents($discoverFile));
 
 function renderDiscoverFile($discoverFile)
 {
     $json = json_decode(file_get_contents($discoverFile));
 
-    $title    = $json->title;
+    $title    = $json->title . ' OUYA games';
     $sections = [];
     foreach ($json->rows as $row) {
         $section = (object) [
     $sections = [];
     foreach ($json->rows as $row) {
         $section = (object) [
@@ -82,8 +130,10 @@ function renderDiscoverFile($discoverFile)
     }
 
     $navLinks = [];
     }
 
     $navLinks = [];
-    if ($title == 'DISCOVER') {
+    if ($json->title == 'DISCOVER') {
         $navLinks['../'] = 'back';
         $navLinks['../'] = 'back';
+        $navLinks['allgames.htm'] = 'all games';
+        $title = 'OUYA games list';
     } else {
         $navLinks['./'] = 'discover';
     }
     } else {
         $navLinks['./'] = 'discover';
     }
@@ -110,7 +160,19 @@ function renderGameFile($gameDataFile)
             $appsDir . $json->version->uuid . '-download.json'
         )
     );
             $appsDir . $json->version->uuid . '-download.json'
         )
     );
+
     $apkDownloadUrl = $downloadJson->app->downloadLink;
     $apkDownloadUrl = $downloadJson->app->downloadLink;
+    /*
+    if (isset($json->premium) && $json->premium) {
+        $apkDownloadUrl = null;
+    }
+    */
+
+    $internetArchiveUrl = $json->stouyapi->{'internet-archive'} ?? null;
+    $developerUrl       = $json->stouyapi->{'developer-url'} ?? null;
+
+    $pushUrl = $GLOBALS['pushToMyOuyaUrl']
+        . '?game=' . urlencode($json->apk->package);
 
     $navLinks = [];
     foreach ($json->genres as $genreTitle) {
 
     $navLinks = [];
     foreach ($json->genres as $genreTitle) {