Fix building with php8 and without config file
[stouyapi.git] / bin / filters.php
index 04a998f2b985259ba00874281faafc1653109d81..b225c9bd8f90fa8b2d1b81492d1481c186de7327 100644 (file)
@@ -31,7 +31,7 @@ function filterByLetter($origGames, $letter)
 {
     $filtered = [];
     foreach ($origGames as $game) {
-        $gameLetter = strtoupper($game->title{0});
+        $gameLetter = strtoupper($game->title[0]);
         if (!preg_match('#^[A-Z]$#', $gameLetter)) {
             $gameLetter = 'Other';
         }
@@ -48,9 +48,11 @@ function filterByPackageNames($origGames, $packageNames)
     $filtered = [];
     foreach ($origGames as $game) {
         if (isset($names[$game->packageName])) {
-            $filtered[] = $game;
+            $filtered[$names[$game->packageName]] = $game;
         }
     }
+    //keep original order
+    ksort($filtered, SORT_NUMERIC);
     return $filtered;
 }
 
@@ -65,6 +67,30 @@ 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 filterLastAdded($origGames, $limit)
+{
+    $games = array_values($origGames);
+    usort(
+        $games,
+        function ($gameA, $gameB) {
+            return strtotime($gameB->firstRelease->date) - strtotime($gameA->firstRelease->date);
+        }
+    );
+
+    return array_slice($games, 0, $limit);
+}
+
 function filterLastUpdated($origGames, $limit)
 {
     $games = array_values($origGames);
@@ -90,4 +116,46 @@ function filterBestRated($origGames, $limit)
 
     return array_slice($games, 0, $limit);
 }
+
+function filterBestRatedGames($origGames, $limit)
+{
+    $noApps = filterByGenre($origGames, 'App', true);
+    $noAppsNoEmus = filterByGenre($noApps, 'Emulator', true);
+
+    return filterBestRated($noAppsNoEmus, $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;
+}
 ?>