Do not crash when only few games exist
[stouyapi.git] / bin / filters.php
index 04a998f2b985259ba00874281faafc1653109d81..107714d7ca5d5afbfe73354771225aea4b12f3e3 100644 (file)
@@ -65,6 +65,17 @@ function filterByPlayers($origGames, $numOfPlayers)
     return $filtered;
 }
 
+function filterBySearchWord($origGames, $searchWord)
+{
+    $filtered = [];
+    foreach ($origGames as $game) {
+        if (stripos($game->title, $searchWord) !== false) {
+            $filtered[] = $game;
+        }
+    }
+    return $filtered;
+}
+
 function filterLastUpdated($origGames, $limit)
 {
     $games = array_values($origGames);
@@ -90,4 +101,38 @@ function filterBestRated($origGames, $limit)
 
     return array_slice($games, 0, $limit);
 }
+
+function filterMostDownloaded($origGames, $limit)
+{
+    $games = array_values($origGames);
+    usort(
+        $games,
+        function ($gameA, $gameB) {
+            return $gameB->rating->count - $gameA->rating->count;
+        }
+    );
+
+    return array_slice($games, 0, $limit);
+}
+
+function filterRandom($origGames, $limit)
+{
+    $randKeys = array_rand($origGames, min(count($origGames), $limit));
+    $games = [];
+    foreach ($randKeys as $key) {
+        $games[] = $origGames[$key];
+    }
+    return $games;
+}
+
+function sortByTitle($games)
+{
+    usort(
+        $games,
+        function ($gameA, $gameB) {
+            return strcasecmp($gameA->title, $gameB->title);
+        }
+    );
+    return $games;
+}
 ?>