Script to generate apps cache file
[gamestick-pjgsapi.git] / bin / functions.php
1 <?php
2 function loadGames(string $gamesDir)
3 {
4     $gamesDir = str_replace('//', '/', $gamesDir . '/');
5
6     $games = [];
7     foreach (glob($gamesDir . '*.json') as $gameFile) {
8         $gameData = json_decode(file_get_contents($gameFile));
9         if ($gameData === null) {
10             throw new \Exception('Cannot load game file: ' . $gameFile);
11         }
12         $games[$gameData->package] = $gameData;
13     }
14     return $games;
15 }
16
17 /**
18  * Convert a meta data game info into a structure that is suitable
19  * for http://l2.gamestickservices.net/api/rest/connect/stick/stick/xxx/view.json
20  */
21 function convertGameDataForConnect($gameData): array
22 {
23     $userCurrency = 'EUR';
24
25     $connectData = [
26         'id'          => $gameData->id,
27         'package'     => $gameData->package,
28         'name'        => $gameData->name,
29         'description' => $gameData->description,
30
31         'minAge' => $gameData->minAge,
32         'genre'  => current($gameData->genres),
33         'genres' => $gameData->genres,
34
35         'version'     => '',
36         'gameversion' => '0',
37         'size'        => 0,
38         'download'    => [
39             'url'     => '',
40             'version' => 0,
41         ],
42
43         'popular'  => 0,
44         'featured' => 0,
45
46         'bought'         => true,
47         'downloadedfree' => false,
48
49         'pricing' => [
50             'buy' => [
51                 'price' => $userCurrency . ' '
52                     . $gameData->prices->buy->$userCurrency->amount,
53             ],
54             'rent' => [],
55         ],
56         'multipricing' => [
57             'buy' => [],
58             'rent' => [],
59         ],
60
61         'images' => [],
62         'videos' => [],
63
64         'social' => [],
65         'companyname' => $gameData->companyname,
66         'companylogofile' => '',
67         'companyiconsize' => 0,
68     ];
69
70     //multipricing
71     foreach ($gameData->prices->buy as $currencySymbol => $price) {
72         $connectData['multipricing']['buy'][] = [
73             'amount'            => $price->amount,
74             'isocurrency'       => $currencySymbol,
75             'symbol'            => $price->symbol,
76             'symbolpostindex'   => $price->symbolpostindex,
77             'formattedcurrency' => $price->formattedcurrency,
78         ];
79     }
80
81     //download
82     $highestVersionCode = 0;
83     $highestVersionKey  = null;
84     foreach ($gameData->releases as $releaseKey => $release) {
85         if (isset($release->broken) && $release->broken === true) {
86             continue;
87         }
88         if ($release->versionCode > $highestVersionCode) {
89             $highestVersionKey = $releaseKey;
90         }
91     }
92     if ($highestVersionKey !== null) {
93         $release = $gameData->releases[$highestVersionKey];
94         $connectData['version']     = $release->uuid;
95         $connectData['gameversion'] = $release->gsName ?? $release->name;
96         $connectData['size']        = round($release->size / 1024 / 1024 * 1000);
97         $connectData['download']    = [
98             'url'     => $release->url,
99             'version' => $release->gsVersion,
100         ];
101     }
102
103     foreach ($gameData->videos as $videoNum => $video) {
104         $connectData['videos'][] = [
105             'version' => $video->version,
106             'url'     => $video->url,
107         ];
108         $connectData['images'][] = [
109             'urls' => [
110                 [
111                     'url'     => $video->thumb,
112                     'version' => 1,
113                 ]
114             ],
115             'name'   => 'STICK_VIDEO' . ($videoNum + 1) . '_SCREENSHOT',
116             'width'  => 350,
117             'height' => 160,
118         ];
119     }
120
121     $imageNameMap = [
122         'logo-1'            => [
123             'name'          => 'STICK_THUMBNAIL1',
124             'width'         => 350,
125             'height'        => 400,
126         ],
127         'logo-2'            => [
128             'name'          => 'STICK_THUMBNAIL4',
129             'width'         => 350,
130             'height'        => 400,
131         ],
132         'logo-3'            => [
133             'name'          => 'STICK_THUMBNAIL4',
134             'width'         => 350,
135             'height'        => 400,
136         ],
137         'logo-4'            => [
138             'name'          => 'STICK_THUMBNAIL4',
139             'width'         => 350,
140             'height'        => 400,
141         ],
142         'logo-1-big'        => [
143             'name'          => 'STICK_THUMBNAIL1_1080',
144             'width'         => 525,
145             'height'        => 240,
146         ],
147         'logo-2-big'        => [
148             'name'          => 'STICK_THUMBNAIL2_1080',
149             'width'         => 525,
150             'height'        => 240,
151         ],
152         'logo-3-big'        => [
153             'name'          => 'STICK_THUMBNAIL3_1080',
154             'width'         => 525,
155             'height'        => 240,
156         ],
157         'logo-4-big'        => [
158             'name'          => 'STICK_THUMBNAIL4_1080',
159             'width'         => 525,
160             'height'        => 240,
161         ],
162         'icon-registration' => [
163             'name'          => 'STICK_REGISTRATION_GAME_ICON',
164             'width'         => 200,
165             'height'        => 200,
166         ],
167         'icon'              => [
168             'name'          => 'STICK_ICON',
169             'width'         => 85,
170             'height'        => 48,
171         ],
172         'screenshots'       => [
173             'name'          => 'STICK_SCREENSHOT',
174             'width'         => 350,
175             'height'        => 160,
176         ],
177         'screenshots-big'   => [
178             'name'          => 'STICK_SCREENSHOT_1080',
179             'width'         => 525,
180             'height'        => 240,
181         ],
182     ];
183     foreach ($gameData->images as $imageKey => $imageData) {
184         if (!isset($imageNameMap[$imageKey])) {
185             throw new \Exception('Unknown image key: ' . $imageKey);
186         }
187         if ($imageData === null) {
188             continue;
189         }
190         $connectImage = [
191             'name'   => $imageNameMap[$imageKey]['name'],
192             'width'  => $imageNameMap[$imageKey]['width'],
193             'height' => $imageNameMap[$imageKey]['height'],
194         ];
195         $urls = (array) $imageData;
196         foreach ($urls as $imageUrl) {
197             $connectImage['urls'][] = [
198                 'url'     => $imageUrl,
199                 'version' => 1,
200             ];
201         }
202         $connectData['images'][] = $connectImage;
203     }
204
205     return $connectData;
206 }