Allow returning null when loading game
[gamestick-pjgsapi.git] / bin / functions.php
1 <?php
2 function loadConfigPopularTxtFile()
3 {
4     if (!$GLOBALS['popuplarTxtFile']) {
5         return;
6     }
7
8     if (!file_exists($GLOBALS['popuplarTxtFile'])) {
9         throw new \Exception('popuplarTxtFile does not exist');
10     }
11
12     $GLOBALS['popular'] = file(
13         $GLOBALS['popuplarTxtFile'], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
14     );
15 }
16
17 function loadConfigFeaturedFile()
18 {
19     if (!$GLOBALS['featuredFile']) {
20         return;
21     }
22
23     if (!file_exists($GLOBALS['featuredFile'])) {
24         throw new \Exception('featuredFile does not exist');
25     }
26
27     $featured = json_decode(file_get_contents($GLOBALS['featuredFile']));
28     if ($featured === null) {
29         throw new \Exception('cache/featured.json cannot be loaded');
30     }
31
32     foreach ($featured as $age => $packages) {
33         $GLOBALS['featured'][$age] = $packages;
34     }
35 }
36
37 /**
38  * Load game data files and return them
39  */
40 function loadGames(array $gameFiles): array
41 {
42     $games = [];
43     foreach ($gameFiles as $gameFile) {
44         $gameData = json_decode(file_get_contents($gameFile));
45         if ($gameData === null) {
46             throw new \Exception('Cannot load game file: ' . $gameFile);
47         }
48         $games[$gameData->package] = $gameData;
49     }
50     return $games;
51 }
52
53 /**
54  * Convert a meta data game info into a structure that is suitable
55  * for http://l2.gamestickservices.net/api/rest/connect/stick/stick/xxx/view.json
56  */
57 function convertGameDataForConnect(object $gameData, array $popular): ?array
58 {
59     $userCurrency = 'EUR';
60
61     $connectData = [
62         'id'          => $gameData->id,
63         'package'     => $gameData->package,
64         'name'        => $gameData->name,
65         'description' => $gameData->description,
66
67         'minAge' => $gameData->minAge,
68         'genre'  => current($gameData->genres),
69         'genres' => [],
70
71         'version'     => '',
72         'gameversion' => '0',
73         'size'        => 0,
74         'download'    => [
75             'url'     => '',
76             'version' => 0,
77         ],
78
79         'popular'  => $popular[$gameData->package] ?? 0,
80         'featured' => 0,
81
82         'bought'         => true,
83         'downloadedfree' => false,
84
85         'isfree' => $gameData->prices->buy->$userCurrency->amount == 0,
86         'pricing' => [
87             'buy' => [
88                 'price' => $userCurrency . ' '
89                     . $gameData->prices->buy->$userCurrency->amount,
90             ],
91             'rent' => [],
92         ],
93         'multipricing' => [
94             'buy' => [],
95             'rent' => [],
96         ],
97
98         'images' => [],
99         'videos' => [],
100
101         'social' => [],
102         'companyname' => $gameData->companyname,
103         'companylogofile' => '',
104         'companyiconsize' => 0,
105     ];
106
107     //genres
108     foreach ($gameData->genres as $genre) {
109         $connectData['genres'][] = [
110             'genre' => $genre,
111         ];
112     }
113
114     //multipricing
115     foreach ($gameData->prices->buy as $currencySymbol => $price) {
116         $connectData['multipricing']['buy'][] = [
117             'amount'            => $price->amount,
118             'isocurrency'       => $currencySymbol,
119             'symbol'            => $price->symbol,
120             'symbolpostindex'   => $price->symbolpostindex,
121             'formattedcurrency' => $price->formattedcurrency,
122         ];
123     }
124
125     //download
126     $highestVersionCode = 0;
127     $highestVersionKey  = null;
128     foreach ($gameData->releases as $releaseKey => $release) {
129         if (isset($release->broken) && $release->broken === true) {
130             continue;
131         }
132         if ($release->versionCode > $highestVersionCode) {
133             $highestVersionCode = $release->versionCode;
134             $highestVersionKey = $releaseKey;
135         }
136     }
137     if ($highestVersionKey !== null) {
138         $release = $gameData->releases[$highestVersionKey];
139         $connectData['version']     = $release->uuid;
140         $connectData['gameversion'] = $release->gsName ?? $release->name;
141         $connectData['size']        = round($release->size / 1024 / 1024 * 1000);
142         $connectData['download']    = [
143             'url'     => $release->url,
144             'version' => $release->versionCode,
145         ];
146     } else {
147         $connectData['name'] = '!! ' . $connectData['name'];
148     }
149
150     foreach ($gameData->videos as $videoNum => $video) {
151         $connectData['videos'][] = [
152             'version' => $video->version,
153             'url'     => $video->url,
154         ];
155         $connectData['images'][] = [
156             'urls' => [
157                 [
158                     'url'     => $video->thumb,
159                     'version' => 1,
160                 ]
161             ],
162             'name'   => 'STICK_VIDEO' . ($videoNum + 1) . '_SCREENSHOT',
163             'width'  => 350,
164             'height' => 160,
165         ];
166     }
167
168     $imageNameMap = [
169         'logo-1'            => [
170             'name'          => 'STICK_THUMBNAIL1',
171             'width'         => 350,
172             'height'        => 88,
173         ],
174         'logo-2'            => [
175             'name'          => 'STICK_THUMBNAIL2',
176             'width'         => 350,
177             'height'        => 160,
178         ],
179         'logo-3'            => [
180             'name'          => 'STICK_THUMBNAIL3',
181             'width'         => 350,
182             'height'        => 236,
183         ],
184         'logo-4'            => [
185             'name'          => 'STICK_THUMBNAIL4',
186             'width'         => 350,
187             'height'        => 400,
188         ],
189         'logo-1-big'        => [
190             'name'          => 'STICK_THUMBNAIL1_1080',
191             'width'         => 525,
192             'height'        => 240,
193         ],
194         'logo-2-big'        => [
195             'name'          => 'STICK_THUMBNAIL2_1080',
196             'width'         => 525,
197             'height'        => 240,
198         ],
199         'logo-3-big'        => [
200             'name'          => 'STICK_THUMBNAIL3_1080',
201             'width'         => 525,
202             'height'        => 240,
203         ],
204         'logo-4-big'        => [
205             'name'          => 'STICK_THUMBNAIL4_1080',
206             'width'         => 525,
207             'height'        => 240,
208         ],
209         'icon-registration' => [
210             'name'          => 'STICK_REGISTRATION_GAME_ICON',
211             'width'         => 200,
212             'height'        => 200,
213         ],
214         'icon'              => [
215             'name'          => 'STICK_ICON',
216             'width'         => 85,
217             'height'        => 48,
218         ],
219         'screenshots'       => [
220             'name'          => 'STICK_SCREENSHOT',
221             'width'         => 350,
222             'height'        => 160,
223         ],
224         'screenshots-big'   => [
225             'name'          => 'STICK_SCREENSHOT_1080',
226             'width'         => 525,
227             'height'        => 240,
228         ],
229     ];
230     foreach ($gameData->images as $imageKey => $imageData) {
231         if (!isset($imageNameMap[$imageKey])) {
232             throw new \Exception('Unknown image key: ' . $imageKey);
233         }
234         if ($imageData === null) {
235             continue;
236         }
237         $connectImage = [
238             'name'   => $imageNameMap[$imageKey]['name'],
239             'width'  => $imageNameMap[$imageKey]['width'],
240             'height' => $imageNameMap[$imageKey]['height'],
241             'urls'   => [],
242         ];
243         $urls = (array) $imageData;
244         foreach ($urls as $imageUrl) {
245             $connectImage['urls'][] = [
246                 'url'     => $imageUrl,
247                 'version' => 1,
248             ];
249         }
250         $connectData['images'][] = $connectImage;
251     }
252
253     return $connectData;
254 }
255
256
257 /**
258  * @param array $featured Array keys are the ages, values
259  *                        are array of "package name => height"
260  *
261  * @return array Suitable for
262  * http://l2.gamestickservices.net/api/rest/connect/stick/stick/xxx/view.json
263  */
264 function buildFeaturedMenu(array $featured, array $games)
265 {
266     $connectMenu = [];
267     foreach ($featured as $age => $packages) {
268         $connectAge = [
269             'age'     => $age,
270             'entries' => [],
271         ];
272
273         $columnNum  = 0;
274         $columnData = null;
275         $heightSum  = 10;
276         foreach ($packages as $package => $height) {
277             if ($heightSum + $height > 6) {
278                 $columnNum++;
279                 $heightSum = 0;
280                 if ($columnData) {
281                     $connectAge['entries'][] = $columnData;
282                 }
283                 $columnData = [
284                     'column'        => $columnNum,
285                     'columnentries' => [],
286                 ];
287             }
288             if (!isset($games[$package])) {
289                 throw new \Exception('Unknown featured game: ' . $package);
290             }
291             $columnData['columnentries'][] = [
292                 'gameID'    => $games[$package]->id,
293                 'thumbnail' => $height,
294                 'column'    => $columnNum,
295             ];
296             $heightSum += $height;
297         }
298         if (count($columnData['columnentries'])) {
299             $connectAge['entries'][] = $columnData;
300         }
301
302         $connectMenu[] = $connectAge;
303     }
304
305     return $connectMenu;
306 }