dcdaac916ff86728f8435c584460f9acb3582fa2
[gamestick-pjgsapi.git] / bin / generate-apps-cache.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Generate cache file with information about apps
5  *
6  * Needed by:
7  * - www/api/rest/connect/stick/stick/generate.php
8  */
9 require_once __DIR__ . '/functions.php';
10 require_once __DIR__ . '/../config.php';
11
12 $cacheDir = dirname(__FILE__, 2) . '/cache/';
13 if (!is_dir($cacheDir)) {
14     if (!mkdir($cacheDir)) {
15         fwrite(STDERR, "Failed to create cache dir: $cacheDir\n");
16         exit(10);
17     }
18 }
19
20 if (!isset($argv[1])) {
21     fwrite(STDERR, "Pass the path to a directory with game data json files\n");
22     exit(1);
23 }
24 $gamesDir = $argv[1];
25 if (!is_dir($gamesDir)) {
26     fwrite(STDERR, 'Given path is not a directory: ' . $gamesDir . "\n");
27     exit(1);
28 }
29
30 $appsCacheFile         = $cacheDir . 'connect-apps.min.json';
31 $featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
32
33
34 $games = loadGames($gamesDir);
35 loadConfigFeaturedFile();
36 loadConfigPopularTxtFile();
37 //make it "package name => number"
38 array_unshift($GLOBALS['popular'], 'dummy');
39 $GLOBALS['popular'] = array_flip($GLOBALS['popular']);
40 unset($GLOBALS['popular']['dummy']);
41
42
43 $connectGames = [];
44 foreach ($games as $gameData) {
45     $connectGames[] = convertGameDataForConnect($gameData, $GLOBALS['popular']);
46 }
47 file_put_contents(
48     $appsCacheFile,
49     json_encode($connectGames, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
50 );
51
52
53 $featuredMenu = buildFeaturedMenu($GLOBALS['featured'], $games);
54 file_put_contents(
55     $featuredAgesCacheFile,
56     json_encode($featuredMenu, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
57 );
58 ?>