X-Git-Url: https://git.cweiske.de/stouyapi.git/blobdiff_plain/44491ef1b9b52e4ed4a5f245cbbeef123a436b18..ca237543a7420c7d6e8a5f555cda44c8844e7660:/bin/filters.php diff --git a/bin/filters.php b/bin/filters.php index 04a998f..9cac07a 100644 --- a/bin/filters.php +++ b/bin/filters.php @@ -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,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; +} ?>