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