Fix queue deletion for The_legend_of_ice_soul
[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 filterLastUpdated($origGames, $limit)
82 {
83     $games = array_values($origGames);
84     usort(
85         $games,
86         function ($gameA, $gameB) {
87             return strtotime($gameB->latestRelease->date) - strtotime($gameA->latestRelease->date);
88         }
89     );
90
91     return array_slice($games, 0, $limit);
92 }
93
94 function filterBestRated($origGames, $limit)
95 {
96     $games = array_values($origGames);
97     usort(
98         $games,
99         function ($gameA, $gameB) {
100             return ($gameB->rating->rank - $gameA->rating->rank) * 100;
101         }
102     );
103
104     return array_slice($games, 0, $limit);
105 }
106
107 function filterMostDownloaded($origGames, $limit)
108 {
109     $games = array_values($origGames);
110     usort(
111         $games,
112         function ($gameA, $gameB) {
113             return $gameB->rating->count - $gameA->rating->count;
114         }
115     );
116
117     return array_slice($games, 0, $limit);
118 }
119
120 function filterRandom($origGames, $limit)
121 {
122     $randKeys = array_rand($origGames, min(count($origGames), $limit));
123     $games = [];
124     foreach ($randKeys as $key) {
125         $games[] = $origGames[$key];
126     }
127     return $games;
128 }
129
130 function sortByTitle($games)
131 {
132     usort(
133         $games,
134         function ($gameA, $gameB) {
135             return strcasecmp($gameA->title, $gameB->title);
136         }
137     );
138     return $games;
139 }
140 ?>