Script to generate apps cache file
authorChristian Weiske <cweiske@cweiske.de>
Wed, 10 May 2023 16:27:28 +0000 (18:27 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 10 May 2023 16:27:28 +0000 (18:27 +0200)
.gitignore [new file with mode: 0644]
bin/functions.php [new file with mode: 0644]
bin/generate-apps-cache.php [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..0a2101f
--- /dev/null
@@ -0,0 +1 @@
+/cache/
diff --git a/bin/functions.php b/bin/functions.php
new file mode 100644 (file)
index 0000000..bbd4c88
--- /dev/null
@@ -0,0 +1,206 @@
+<?php
+function loadGames(string $gamesDir)
+{
+    $gamesDir = str_replace('//', '/', $gamesDir . '/');
+
+    $games = [];
+    foreach (glob($gamesDir . '*.json') as $gameFile) {
+        $gameData = json_decode(file_get_contents($gameFile));
+        if ($gameData === null) {
+            throw new \Exception('Cannot load game file: ' . $gameFile);
+        }
+        $games[$gameData->package] = $gameData;
+    }
+    return $games;
+}
+
+/**
+ * Convert a meta data game info into a structure that is suitable
+ * for http://l2.gamestickservices.net/api/rest/connect/stick/stick/xxx/view.json
+ */
+function convertGameDataForConnect($gameData): array
+{
+    $userCurrency = 'EUR';
+
+    $connectData = [
+        'id'          => $gameData->id,
+        'package'     => $gameData->package,
+        'name'        => $gameData->name,
+        'description' => $gameData->description,
+
+        'minAge' => $gameData->minAge,
+        'genre'  => current($gameData->genres),
+        'genres' => $gameData->genres,
+
+        'version'     => '',
+        'gameversion' => '0',
+        'size'        => 0,
+        'download'    => [
+            'url'     => '',
+            'version' => 0,
+        ],
+
+        'popular'  => 0,
+        'featured' => 0,
+
+        'bought'         => true,
+        'downloadedfree' => false,
+
+        'pricing' => [
+            'buy' => [
+                'price' => $userCurrency . ' '
+                    . $gameData->prices->buy->$userCurrency->amount,
+            ],
+            'rent' => [],
+        ],
+        'multipricing' => [
+            'buy' => [],
+            'rent' => [],
+        ],
+
+        'images' => [],
+        'videos' => [],
+
+        'social' => [],
+        'companyname' => $gameData->companyname,
+        'companylogofile' => '',
+        'companyiconsize' => 0,
+    ];
+
+    //multipricing
+    foreach ($gameData->prices->buy as $currencySymbol => $price) {
+        $connectData['multipricing']['buy'][] = [
+            'amount'            => $price->amount,
+            'isocurrency'       => $currencySymbol,
+            'symbol'            => $price->symbol,
+            'symbolpostindex'   => $price->symbolpostindex,
+            'formattedcurrency' => $price->formattedcurrency,
+        ];
+    }
+
+    //download
+    $highestVersionCode = 0;
+    $highestVersionKey  = null;
+    foreach ($gameData->releases as $releaseKey => $release) {
+        if (isset($release->broken) && $release->broken === true) {
+            continue;
+        }
+        if ($release->versionCode > $highestVersionCode) {
+            $highestVersionKey = $releaseKey;
+        }
+    }
+    if ($highestVersionKey !== null) {
+        $release = $gameData->releases[$highestVersionKey];
+        $connectData['version']     = $release->uuid;
+        $connectData['gameversion'] = $release->gsName ?? $release->name;
+        $connectData['size']        = round($release->size / 1024 / 1024 * 1000);
+        $connectData['download']    = [
+            'url'     => $release->url,
+            'version' => $release->gsVersion,
+        ];
+    }
+
+    foreach ($gameData->videos as $videoNum => $video) {
+        $connectData['videos'][] = [
+            'version' => $video->version,
+            'url'     => $video->url,
+        ];
+        $connectData['images'][] = [
+            'urls' => [
+                [
+                    'url'     => $video->thumb,
+                    'version' => 1,
+                ]
+            ],
+            'name'   => 'STICK_VIDEO' . ($videoNum + 1) . '_SCREENSHOT',
+            'width'  => 350,
+            'height' => 160,
+        ];
+    }
+
+    $imageNameMap = [
+        'logo-1'            => [
+            'name'          => 'STICK_THUMBNAIL1',
+            'width'         => 350,
+            'height'        => 400,
+        ],
+        'logo-2'            => [
+            'name'          => 'STICK_THUMBNAIL4',
+            'width'         => 350,
+            'height'        => 400,
+        ],
+        'logo-3'            => [
+            'name'          => 'STICK_THUMBNAIL4',
+            'width'         => 350,
+            'height'        => 400,
+        ],
+        'logo-4'            => [
+            'name'          => 'STICK_THUMBNAIL4',
+            'width'         => 350,
+            'height'        => 400,
+        ],
+        'logo-1-big'        => [
+            'name'          => 'STICK_THUMBNAIL1_1080',
+            'width'         => 525,
+            'height'        => 240,
+        ],
+        'logo-2-big'        => [
+            'name'          => 'STICK_THUMBNAIL2_1080',
+            'width'         => 525,
+            'height'        => 240,
+        ],
+        'logo-3-big'        => [
+            'name'          => 'STICK_THUMBNAIL3_1080',
+            'width'         => 525,
+            'height'        => 240,
+        ],
+        'logo-4-big'        => [
+            'name'          => 'STICK_THUMBNAIL4_1080',
+            'width'         => 525,
+            'height'        => 240,
+        ],
+        'icon-registration' => [
+            'name'          => 'STICK_REGISTRATION_GAME_ICON',
+            'width'         => 200,
+            'height'        => 200,
+        ],
+        'icon'              => [
+            'name'          => 'STICK_ICON',
+            'width'         => 85,
+            'height'        => 48,
+        ],
+        'screenshots'       => [
+            'name'          => 'STICK_SCREENSHOT',
+            'width'         => 350,
+            'height'        => 160,
+        ],
+        'screenshots-big'   => [
+            'name'          => 'STICK_SCREENSHOT_1080',
+            'width'         => 525,
+            'height'        => 240,
+        ],
+    ];
+    foreach ($gameData->images as $imageKey => $imageData) {
+        if (!isset($imageNameMap[$imageKey])) {
+            throw new \Exception('Unknown image key: ' . $imageKey);
+        }
+        if ($imageData === null) {
+            continue;
+        }
+        $connectImage = [
+            'name'   => $imageNameMap[$imageKey]['name'],
+            'width'  => $imageNameMap[$imageKey]['width'],
+            'height' => $imageNameMap[$imageKey]['height'],
+        ];
+        $urls = (array) $imageData;
+        foreach ($urls as $imageUrl) {
+            $connectImage['urls'][] = [
+                'url'     => $imageUrl,
+                'version' => 1,
+            ];
+        }
+        $connectData['images'][] = $connectImage;
+    }
+
+    return $connectData;
+}
diff --git a/bin/generate-apps-cache.php b/bin/generate-apps-cache.php
new file mode 100755 (executable)
index 0000000..f3006d3
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env php
+<?php
+/**
+ * Generate cache file with information about apps
+ *
+ * Needed by:
+ * - www/api/rest/connect/stick/stick/generate.php
+ */
+require_once __DIR__ . '/functions.php';
+$cacheDir = dirname(__FILE__, 2) . '/cache/';
+if (!is_dir($cacheDir)) {
+    if (!mkdir($cacheDir)) {
+        fwrite(STDERR, "Failed to create cache dir: $cacheDir\n");
+        exit(10);
+    }
+}
+
+if (!isset($argv[1])) {
+    fwrite(STDERR, "Pass the path to a directory with game data json files\n");
+    exit(1);
+}
+$gamesDir = $argv[1];
+if (!is_dir($gamesDir)) {
+    fwrite(STDERR, 'Given path is not a directory: ' . $gamesDir . "\n");
+    exit(1);
+}
+
+$appsCacheFile         = $cacheDir . 'connect-apps.min.json';
+$featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
+
+$games = loadGames($gamesDir);
+$connectGames = [];
+foreach ($games as $gameData) {
+    $connectGames[] = convertGameDataForConnect($gameData);
+}
+file_put_contents(
+    $appsCacheFile,
+    json_encode($connectGames, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
+);
+?>