703114a2410fa9e65f88aec8d3c2e499379fa9a8
[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[] = $game;
52         }
53     }
54     return $filtered;
55 }
56
57 function filterByPlayers($origGames, $numOfPlayers)
58 {
59     $filtered = [];
60     foreach ($origGames as $game) {
61         if (array_search($numOfPlayers, $game->players) !== false) {
62             $filtered[] = $game;
63         }
64     }
65     return $filtered;
66 }
67
68 function filterLastUpdated($origGames, $limit)
69 {
70     $games = array_values($origGames);
71     usort(
72         $games,
73         function ($gameA, $gameB) {
74             return strtotime($gameB->latestRelease->date) - strtotime($gameA->latestRelease->date);
75         }
76     );
77
78     return array_slice($games, 0, $limit);
79 }
80
81 function filterBestRated($origGames, $limit)
82 {
83     $games = array_values($origGames);
84     usort(
85         $games,
86         function ($gameA, $gameB) {
87             return $gameB->rating->average - $gameA->rating->average;
88         }
89     );
90
91     return array_slice($games, 0, $limit);
92 }
93 ?>