Unify domain names in README
[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, $remove = false)
14 {
15     $filtered = [];
16     foreach ($origGames as $game) {
17         if ($remove) {
18             if (array_search($genre, $game->genres) === false) {
19                 $filtered[] = $game;
20             }
21         } else {
22             if (array_search($genre, $game->genres) !== false) {
23                 $filtered[] = $game;
24             }
25         }
26     }
27     return $filtered;
28 }
29
30 function filterByLetter($origGames, $letter)
31 {
32     $filtered = [];
33     foreach ($origGames as $game) {
34         $gameLetter = strtoupper($game->title[0]);
35         if (!preg_match('#^[A-Z]$#', $gameLetter)) {
36             $gameLetter = 'Other';
37         }
38         if ($letter == $gameLetter) {
39             $filtered[] = $game;
40         }
41     }
42     return $filtered;
43 }
44
45 function filterByPackageNames($origGames, $packageNames)
46 {
47     $names = array_flip($packageNames);
48     $filtered = [];
49     foreach ($origGames as $game) {
50         if (isset($names[$game->packageName])) {
51             $filtered[$names[$game->packageName]] = $game;
52         }
53     }
54     //keep original order
55     ksort($filtered, SORT_NUMERIC);
56     return $filtered;
57 }
58
59 function filterByPlayers($origGames, $numOfPlayers)
60 {
61     $filtered = [];
62     foreach ($origGames as $game) {
63         if (array_search($numOfPlayers, $game->players) !== false) {
64             $filtered[] = $game;
65         }
66     }
67     return $filtered;
68 }
69
70 function filterBySearchWord($origGames, $searchWord)
71 {
72     $filtered = [];
73     foreach ($origGames as $game) {
74         if (stripos($game->title, $searchWord) !== false) {
75             $filtered[] = $game;
76         }
77     }
78     return $filtered;
79 }
80
81 function filterLastAdded($origGames, $limit)
82 {
83     $games = array_values($origGames);
84     usort(
85         $games,
86         function ($gameA, $gameB) {
87             return strtotime($gameB->firstRelease->date) - strtotime($gameA->firstRelease->date);
88         }
89     );
90
91     return array_slice($games, 0, $limit);
92 }
93
94 function filterLastUpdated($origGames, $limit)
95 {
96     $games = array_values($origGames);
97     usort(
98         $games,
99         function ($gameA, $gameB) {
100             return strtotime($gameB->latestRelease->date) - strtotime($gameA->latestRelease->date);
101         }
102     );
103
104     return array_slice($games, 0, $limit);
105 }
106
107 function filterBestRated($origGames, $limit)
108 {
109     $games = array_values($origGames);
110     usort(
111         $games,
112         function ($gameA, $gameB) {
113             return ($gameB->rating->rank - $gameA->rating->rank) * 100;
114         }
115     );
116
117     return array_slice($games, 0, $limit);
118 }
119
120 function filterBestRatedGames($origGames, $limit)
121 {
122     $noApps = filterByGenre($origGames, 'App', true);
123     $noAppsNoEmus = filterByGenre($noApps, 'Emulator', true);
124
125     return filterBestRated($noAppsNoEmus, $limit);
126 }
127
128 function filterMostDownloaded($origGames, $limit)
129 {
130     $games = array_values($origGames);
131     usort(
132         $games,
133         function ($gameA, $gameB) {
134             return $gameB->rating->count - $gameA->rating->count;
135         }
136     );
137
138     return array_slice($games, 0, $limit);
139 }
140
141 function filterRandom($origGames, $limit)
142 {
143     $randKeys = array_rand($origGames, min(count($origGames), $limit));
144     $games = [];
145     foreach ($randKeys as $key) {
146         $games[] = $origGames[$key];
147     }
148     return $games;
149 }
150
151 function sortByTitle($games)
152 {
153     usort(
154         $games,
155         function ($gameA, $gameB) {
156             return strcasecmp($gameA->title, $gameB->title);
157         }
158     );
159     return $games;
160 }
161 ?>