Highscores revamp a bit

* Show real rank, if 2 or more players have the same skill, show them with same rank
* New setting: highscores_online_status
* Additional fields passed to twig: updatedAt, totalResults, page, baseLink
This commit is contained in:
slawkens
2025-07-22 18:18:29 +02:00
parent 1566deb84a
commit d8132d4d76
3 changed files with 49 additions and 11 deletions

View File

@@ -146,17 +146,24 @@ $cache = Cache::getInstance();
if ($cache->enabled() && $highscoresTTL > 0) { if ($cache->enabled() && $highscoresTTL > 0) {
$tmp = ''; $tmp = '';
if ($cache->fetch($cacheKey, $tmp)) { if ($cache->fetch($cacheKey, $tmp)) {
$highscores = unserialize($tmp); $data = unserialize($tmp);
$totalResults = $data['totalResults'];
$highscores = $data['highscores'];
$updatedAt = $data['updatedAt'];
$needReCache = false; $needReCache = false;
} }
} }
$offset = ($page - 1) * $configHighscoresPerPage; $offset = ($page - 1) * $configHighscoresPerPage;
$query->join('accounts', 'accounts.id', '=', 'players.account_id') $query->withOnlineStatus()
->withOnlineStatus()
->whereNotIn('players.id', setting('core.highscores_ids_hidden')) ->whereNotIn('players.id', setting('core.highscores_ids_hidden'))
->notDeleted() ->notDeleted()
->where('players.group_id', '<', setting('core.highscores_groups_hidden')) ->where('players.group_id', '<', setting('core.highscores_groups_hidden'));
$totalResultsQuery = clone $query;
$query
->join('accounts', 'accounts.id', '=', 'players.account_id')
->limit($limit) ->limit($limit)
->offset($offset) ->offset($offset)
->selectRaw('accounts.country, players.id, players.name, players.account_id, players.level, players.vocation' . $outfit . $promotion) ->selectRaw('accounts.country, players.id, players.name, players.account_id, players.level, players.vocation' . $outfit . $promotion)
@@ -215,17 +222,24 @@ if (empty($highscores)) {
return $tmp; return $tmp;
})->toArray(); })->toArray();
$updatedAt = time();
$totalResults = $totalResultsQuery->count();
} }
if ($highscoresTTL > 0 && $cache->enabled() && $needReCache) { if ($highscoresTTL > 0 && $cache->enabled() && $needReCache) {
$cache->set($cacheKey, serialize($highscores), $highscoresTTL * 60); $cache->set($cacheKey, serialize(
[
'totalResults' => $totalResults,
'highscores' => $highscores,
'updatedAt' => $updatedAt,
]
), $highscoresTTL * 60);
} }
$show_link_to_next_page = false; $show_link_to_next_page = false;
$i = 0; $i = 0;
$settingHighscoresVocation = setting('core.highscores_vocation');
foreach($highscores as $id => &$player) foreach($highscores as $id => &$player)
{ {
if(++$i <= $configHighscoresPerPage) if(++$i <= $configHighscoresPerPage)
@@ -239,10 +253,22 @@ foreach($highscores as $id => &$player)
$player['link'] = getPlayerLink($player['name'], false); $player['link'] = getPlayerLink($player['name'], false);
$player['flag'] = getFlagImage($player['country']); $player['flag'] = getFlagImage($player['country']);
if($settingHighscoresOutfit) { $player['outfit'] = '<img style="position:absolute;margin-top:' . (in_array($player['looktype'], setting('core.outfit_images_wrong_looktypes')) ? '-15px;margin-left:5px' : '-45px;margin-left:-25px') . ';" src="' . $player['outfit_url'] . '" alt="" />';
$player['outfit'] = '<img style="position:absolute;margin-top:' . (in_array($player['looktype'], setting('core.outfit_images_wrong_looktypes')) ? '-15px;margin-left:5px' : '-45px;margin-left:-25px') . ';" src="' . $player['outfit_url'] . '" alt="" />';
if ($skill != POT::SKILL__LEVEL) {
if (isset($lastValue) && $lastValue == $player['value']) {
$player['rank'] = $lastRank;
}
else {
$player['rank'] = $offset + $i;
}
$lastRank = $player['rank'] ;
$lastValue = $player['value'];
}
else {
$player['rank'] = $offset + $i;
} }
$player['rank'] = $offset + $i;
} }
else { else {
unset($highscores[$id]); unset($highscores[$id]);
@@ -263,6 +289,8 @@ if($show_link_to_next_page) {
$linkNextPage = getLink('highscores') . '/' . $list . ($vocation !== 'all' ? '/' . $vocation : '') . '/' . ($page + 1); $linkNextPage = getLink('highscores') . '/' . $list . ($vocation !== 'all' ? '/' . $vocation : '') . '/' . ($page + 1);
} }
$baseLink = getLink('highscores') . '/' . $list . ($vocation !== 'all' ? '/' . $vocation : '') . '/';
$types = array( $types = array(
'experience' => 'Experience', 'experience' => 'Experience',
'magic' => 'Magic', 'magic' => 'Magic',
@@ -297,4 +325,8 @@ $twig->display('highscores.html.twig', [
'types' => $types, 'types' => $types,
'linkPreviousPage' => $linkPreviousPage, 'linkPreviousPage' => $linkPreviousPage,
'linkNextPage' => $linkNextPage, 'linkNextPage' => $linkNextPage,
'totalResults' => $totalResults,
'page' => $page,
'baseLink' => $baseLink,
'updatedAt' => $updatedAt,
]); ]);

View File

@@ -1080,6 +1080,12 @@ Sent by MyAAC,<br/>
'desc' => 'Show player vocation under his nickname?', 'desc' => 'Show player vocation under his nickname?',
'default' => true, 'default' => true,
], ],
'highscores_online_status' => [
'name' => 'Display Online Status',
'type' => 'boolean',
'desc' => 'Show player status as red (offline) or green (online)',
'default' => false,
],
'highscores_frags' => [ 'highscores_frags' => [
'name' => 'Display Top Frags', 'name' => 'Display Top Frags',
'type' => 'boolean', 'type' => 'boolean',

View File

@@ -66,7 +66,7 @@
<td> <td>
<a href="{{ player.link }}"> <a href="{{ player.link }}">
<span style="color: {% if player.online > 0 %}green{% else %}red{% endif %}">{{ player.name }}</span> <span {% if setting('core.highscores_online_status') %}style="color: {% if player.online > 0 %}green{% else %}red{% endif %}"{% endif %}>{{ player.name }}</span>
</a> </a>
{% if setting('core.highscores_vocation') %} {% if setting('core.highscores_vocation') %}
<br/><small>{{ player.vocation }}</small> <br/><small>{{ player.vocation }}</small>