by-age and by-letter categories
[stouyapi.git] / bin / filters.php
1 <?php
2 function filterByAge($origGames, $age)
3 {
4     $filtered = [];
5     foreach ($origGames as $game) {
6         if ($age == $game->contentRating) {
7             $filtered[] = $game;
8         }
9     }
10     return $filtered;
11 }
12
13 function filterByGenre($origGames, $genre)
14 {
15     $filtered = [];
16     foreach ($origGames as $game) {
17         if (array_search($genre, $game->genres) !== false) {
18             $filtered[] = $game;
19         }
20     }
21     return $filtered;
22 }
23
24 function filterByLetter($origGames, $letter)
25 {
26     $filtered = [];
27     foreach ($origGames as $game) {
28         $gameLetter = strtoupper($game->title{0});
29         if (!preg_match('#^[A-Z]$#', $gameLetter)) {
30             $gameLetter = 'Other';
31         }
32         if ($letter == $gameLetter) {
33             $filtered[] = $game;
34         }
35     }
36     return $filtered;
37 }
38
39 function filterByPackageNames($origGames, $packageNames)
40 {
41     $names = array_flip($packageNames);
42     $filtered = [];
43     foreach ($origGames as $game) {
44         if (isset($names[$game->packageName])) {
45             $filtered[] = $game;
46         }
47     }
48     return $filtered;
49 }
50
51 function filterByPlayers($origGames, $numOfPlayers)
52 {
53     $filtered = [];
54     foreach ($origGames as $game) {
55         if (array_search($numOfPlayers, $game->players) !== false) {
56             $filtered[] = $game;
57         }
58     }
59     return $filtered;
60 }
61
62 function filterLastUpdated($origGames, $limit)
63 {
64     $games = array_values($origGames);
65     usort(
66         $games,
67         function ($gameA, $gameB) {
68             return strtotime($gameB->latestRelease->date) - strtotime($gameA->latestRelease->date);
69         }
70     );
71
72     return array_slice($games, 0, $limit);
73 }
74
75 function filterBestRated($origGames, $limit)
76 {
77     $games = array_values($origGames);
78     usort(
79         $games,
80         function ($gameA, $gameB) {
81             return $gameB->rating->average - $gameA->rating->average;
82         }
83     );
84
85     return array_slice($games, 0, $limit);
86 }
87 ?>