From: Christian Weiske Date: Fri, 22 Nov 2019 20:34:26 +0000 (+0100) Subject: by-age and by-letter categories X-Git-Tag: v1.0.0~33 X-Git-Url: https://git.cweiske.de/stouyapi.git/commitdiff_plain/7ddab9bf8fe51936011321ffd881adc0e7be5b58?hp=c68e976651389d84861325f9cb5b3c0cdd70a2c5 by-age and by-letter categories --- diff --git a/bin/filters.php b/bin/filters.php index adad0d2..07a00bb 100644 --- a/bin/filters.php +++ b/bin/filters.php @@ -1,4 +1,15 @@ contentRating) { + $filtered[] = $game; + } + } + return $filtered; +} + function filterByGenre($origGames, $genre) { $filtered = []; @@ -10,6 +21,21 @@ function filterByGenre($origGames, $genre) return $filtered; } +function filterByLetter($origGames, $letter) +{ + $filtered = []; + foreach ($origGames as $game) { + $gameLetter = strtoupper($game->title{0}); + if (!preg_match('#^[A-Z]$#', $gameLetter)) { + $gameLetter = 'Other'; + } + if ($letter == $gameLetter) { + $filtered[] = $game; + } + } + return $filtered; +} + function filterByPackageNames($origGames, $packageNames) { $names = array_flip($packageNames); diff --git a/bin/import-game-data.php b/bin/import-game-data.php index 2952aba..6672409 100755 --- a/bin/import-game-data.php +++ b/bin/import-game-data.php @@ -106,7 +106,7 @@ function buildDiscover(array $games) ); $players = [ - 1 => '1 player', + //1 => '1 player', 2 => '2 players', 3 => '3 players', 4 => '4 players', @@ -119,18 +119,20 @@ function buildDiscover(array $games) ); } - $genres = getAllGenres($games); - sort($genres); - $genreChunks = array_chunk($genres, 4); - $first = true; - foreach ($genreChunks as $chunk) { - addDiscoverRow( - $data, $first ? 'Genres' : '', - $chunk + $ages = getAllAges($games); + natsort($ages); + addDiscoverRow($data, 'Content rating', $ages); + foreach ($ages as $num => $title) { + writeJson( + 'api/v1/discover-data/' . categoryPath($title) . '.json', + buildDiscoverCategory($title, filterByAge($games, $title)) ); - $first = false; } + $genres = getAllGenres($games); + sort($genres); + addChunkedDiscoverRows($data, $genres, 'Genres'); + foreach ($genres as $genre) { writeJson( 'api/v1/discover-data/' . categoryPath($genre) . '.json', @@ -138,6 +140,15 @@ function buildDiscover(array $games) ); } + $abc = array_merge(range('A', 'Z'), ['Other']); + addChunkedDiscoverRows($data, $abc, 'Alphabetical'); + foreach ($abc as $letter) { + writeJson( + 'api/v1/discover-data/' . categoryPath($letter) . '.json', + buildDiscoverCategory($letter, filterByLetter($games, $letter)) + ); + } + return $data; } @@ -348,6 +359,19 @@ function buildDetails($game) ]; } +function addChunkedDiscoverRows(&$data, $games, $title) +{ + $chunks = array_chunk($games, 4); + $first = true; + foreach ($chunks as $chunk) { + addDiscoverRow( + $data, $first ? $title : '', + $chunk + ); + $first = false; + } +} + function addDiscoverRow(&$data, $title, $games) { $row = [ @@ -428,7 +452,16 @@ function buildDiscoverGameTile($game) function categoryPath($title) { - return str_replace(['/', '\\', ' '], '_', $title); + return str_replace(['/', '\\', ' ', '+'], '_', $title); +} + +function getAllAges($games) +{ + $ages = []; + foreach ($games as $game) { + $ages[] = $game->contentRating; + } + return array_unique($ages); } function getAllGenres($games)