From c769962e39fe8dfb72ecd5be1864e145696be794 Mon Sep 17 00:00:00 2001 From: slawkens Date: Thu, 9 Jan 2025 22:40:57 +0100 Subject: [PATCH] Refactor getTopPlayers function * Option to getTopPlayers by balance * use Cache::remember --- system/functions.php | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/system/functions.php b/system/functions.php index 76b09233..4863af8d 100644 --- a/system/functions.php +++ b/system/functions.php @@ -1090,20 +1090,16 @@ function csrfProtect(): void } } -function getTopPlayers($limit = 5) { +function getTopPlayers($limit = 5, $skill = 'level') { global $db; - $cache = Cache::getInstance(); - if($cache->enabled()) { - $tmp = ''; - if($cache->fetch('top_' . $limit . '_level', $tmp)) { - $players = unserialize($tmp); - } + if ($skill === 'level') { + $skill = 'experience'; } - if (!isset($players)) { + return Cache::remember("top_{$limit}_{$skill}", 2 * 60, function () use ($db, $limit, $skill) { $columns = [ - 'id', 'name', 'level', 'vocation', 'experience', + 'id', 'name', 'level', 'vocation', 'experience', 'balance', 'looktype', 'lookhead', 'lookbody', 'looklegs', 'lookfeet' ]; @@ -1115,14 +1111,14 @@ function getTopPlayers($limit = 5) { $columns[] = 'online'; } - $players = Player::query() + return Player::query() ->select($columns) ->withOnlineStatus() ->notDeleted() ->where('group_id', '<', setting('core.highscores_groups_hidden')) ->whereNotIn('id', setting('core.highscores_ids_hidden')) ->where('account_id', '!=', 1) - ->orderByDesc('experience') + ->orderByDesc($skill) ->limit($limit) ->get() ->map(function ($e, $i) { @@ -1134,13 +1130,7 @@ function getTopPlayers($limit = 5) { return $row; })->toArray(); - - if($cache->enabled()) { - $cache->set('top_' . $limit . '_level', serialize($players), 120); - } - } - - return $players; + }); } function deleteDirectory($dir, $ignore = array(), $contentOnly = false) {