From cf2c5e36bc3109ec0e7c30365fb40f41157fe17f Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 2 Nov 2020 23:34:29 +0100 Subject: [PATCH] Feature/better highscores (#141) * Move highscores to twig * Add highscores frags for TFS 1.x * Change $config to config() * Cache highscores The most asked and long awaited feature? :> * Fix highscores_per_page and rename configurable * Fix next page link (some typo) * Fix too many players being shown * Fix when changing config.highscores_per_page * Update system/pages/highscores.php Co-authored-by: whiteblXK Co-authored-by: whiteblXK --- config.php | 5 +- system/pages/highscores.php | 382 ++++++++++++-------------- system/templates/highscores.html.twig | 105 +++++++ 3 files changed, 278 insertions(+), 214 deletions(-) create mode 100644 system/templates/highscores.html.twig diff --git a/config.php b/config.php index 6f8063ae..37ef08df 100644 --- a/config.php +++ b/config.php @@ -204,13 +204,14 @@ $config = array( // highscores page 'highscores_vocation_box' => true, // show 'Choose a vocation' box on the highscores (allowing peoples to sort highscores by vocation)? 'highscores_vocation' => true, // show player vocation under his nickname? - 'highscores_frags' => false, // show 'Frags' tab (best fraggers on the server)? Only 0.3 + 'highscores_frags' => false, // show 'Frags' tab (best fraggers on the server)? 'highscores_balance' => false, // show 'Balance' tab (richest players on the server) 'highscores_outfit' => true, // show player outfit? 'highscores_country_box' => false, // doesnt work yet! (not implemented) 'highscores_groups_hidden' => 3, // this group id and higher won't be shown on the highscores 'highscores_ids_hidden' => array(0), // this ids of players will be hidden on the highscores (should be ids of samples) - 'highscores_length' => 100, // how many records per page on highscores + 'highscores_per_page' => 100, // how many records per page on highscores + 'highscores_cache_ttl' => 15, // how often to update highscores from database in minutes (default 15 minutes) // characters page 'characters' => array( // what things to display on character view page (true/false in each option) diff --git a/system/pages/highscores.php b/system/pages/highscores.php index 79211925..39eacb04 100644 --- a/system/pages/highscores.php +++ b/system/pages/highscores.php @@ -11,25 +11,30 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Highscores'; -if($config['account_country'] && $config['highscores_country_box']) +$configHighscoresCountryBox = config('highscores_country_box'); +if(config('account_country') && $configHighscoresCountryBox) require SYSTEM . 'countries.conf.php'; $list = isset($_GET['list']) ? $_GET['list'] : ''; $_page = isset($_GET['page']) ? $_GET['page'] : 0; -$vocation = isset($_GET['vocation']) ? $_GET['vocation'] : NULL; +$vocation = isset($_GET['vocation']) ? $_GET['vocation'] : 'all'; $add_sql = ''; -$config_vocations = $config['vocations']; -if($config['highscores_vocation_box'] && isset($vocation)) + +$configHighscoresVocationBox = config('highscores_vocation_box'); +$configVocations = config('vocations'); +$configVocationsAmount = config('vocations_amount'); + +if($configHighscoresVocationBox && $vocation !== 'all') { - foreach($config['vocations'] as $id => $name) { + foreach($configVocations as $id => $name) { if(strtolower($name) == $vocation) { $add_vocs = array($id); - $i = $id + $config['vocations_amount']; - while(isset($config['vocations'][$i])) { + $i = $id + $configVocationsAmount; + while(isset($configVocations[$i])) { $add_vocs[] = $i; - $i += $config['vocations_amount']; + $i += $configVocationsAmount; } $add_sql = 'AND `vocation` IN (' . implode(', ', $add_vocs) . ')'; @@ -45,7 +50,7 @@ $skill = POT::SKILL__LEVEL; if(is_numeric($list)) { $list = (int) $list; - if($list >= POT::SKILL_FIRST && $list <= SKILL__LAST) + if($list >= POT::SKILL_FIRST && $list <= POT::SKILL__LAST) $skill = $list; } else @@ -90,12 +95,12 @@ else break; case 'frags': - if($config['highscores_frags'] && $config['otserv_version'] == TFS_03) + if(config('highscores_frags')) $skill = SKILL_FRAGS; break; case 'balance': - if($config['highscores_balance']) + if(config('highscores_balance')) $skill = SKILL_BALANCE; break; } @@ -115,7 +120,10 @@ if($db->hasColumn('players', 'deletion')) $outfit_addons = false; $outfit = ''; -if($config['highscores_outfit']) { + +$configHighscoresOutfit = config('highscores_outfit'); + +if($configHighscoresOutfit) { $outfit = ', lookbody, lookfeet, lookhead, looklegs, looktype'; if($db->hasColumn('players', 'lookaddons')) { $outfit .= ', lookaddons'; @@ -123,82 +131,85 @@ if($config['highscores_outfit']) { } } -$offset = $_page * $config['highscores_length']; -if($skill >= POT::SKILL_FIRST && $skill <= POT::SKILL_LAST) { // skills - if($db->hasColumn('players', 'skill_fist')) {// tfs 1.0 - $skill_ids = array( - POT::SKILL_FIST => 'skill_fist', - POT::SKILL_CLUB => 'skill_club', - POT::SKILL_SWORD => 'skill_sword', - POT::SKILL_AXE => 'skill_axe', - POT::SKILL_DIST => 'skill_dist', - POT::SKILL_SHIELD => 'skill_shielding', - POT::SKILL_FISH => 'skill_fishing', - ); +$configHighscoresPerPage = config('highscores_per_page'); +$limit = $configHighscoresPerPage + 1; - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,vocation' . $promotion . $outfit . ', ' . $skill_ids[$skill] . ' as value FROM accounts,players WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 AND players.group_id < '.$config['highscores_groups_hidden'].' '.$add_sql.' AND accounts.id = players.account_id ORDER BY ' . $skill_ids[$skill] . ' DESC LIMIT 101 OFFSET '.$offset)->fetchAll(); - } - else - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',value,level,vocation' . $promotion . $outfit . ' FROM accounts,players,player_skills WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 AND players.group_id < '.$config['highscores_groups_hidden'].' '.$add_sql.' AND players.id = player_skills.player_id AND player_skills.skillid = '.$skill.' AND accounts.id = players.account_id ORDER BY value DESC, count DESC LIMIT 101 OFFSET '.$offset)->fetchAll(); -} -else if($skill == SKILL_FRAGS && $config['otserv_version'] == TFS_03) // frags -{ - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,vocation' . $promotion . $outfit . ',COUNT(`player_killers`.`player_id`) as value' . - ' FROM `accounts`, `players`, `player_killers` ' . - ' WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 AND players.group_id < '.$config['highscores_groups_hidden'].' '.$add_sql.' AND players.id = player_killers.player_id AND accounts.id = players.account_id' . - ' GROUP BY `player_id`' . - ' ORDER BY value DESC' . - ' LIMIT 101 OFFSET '.$offset)->fetchAll(); -} -else if($skill == SKILL_BALANCE) // balance -{ - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,balance as value,vocation' . $promotion . $outfit . ' FROM accounts,players WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 AND players.group_id < '.$config['highscores_groups_hidden'].' '.$add_sql.' AND accounts.id = players.account_id ORDER BY value DESC LIMIT 101 OFFSET '.$offset)->fetchAll(); -} -else -{ - if($skill == POT::SKILL__MAGLEVEL) { - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',maglevel,level,vocation' . $promotion . $outfit . ' FROM accounts, players WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 '.$add_sql.' AND players.group_id < '.$config['highscores_groups_hidden'].' AND accounts.id = players.account_id ORDER BY maglevel DESC, manaspent DESC LIMIT 101 OFFSET '.$offset)->fetchAll(); - } - else { // level - $skills = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,experience,vocation' . $promotion . $outfit . ' FROM accounts, players WHERE players.id NOT IN (' . implode(', ', $config['highscores_ids_hidden']) . ') AND players.' . $deleted . ' = 0 '.$add_sql.' AND players.group_id < '.$config['highscores_groups_hidden'].' AND accounts.id = players.account_id ORDER BY level DESC, experience DESC LIMIT 101 OFFSET '.$offset)->fetchAll(); - $list = 'experience'; +$needReCache = true; +$cacheKey = 'highscores_' . $skill . '_' . $vocation . '_' . $_page . '_' . $configHighscoresPerPage; + +$cache = Cache::getInstance(); +if ($cache->enabled()) { + $tmp = ''; + if ($cache->fetch($cacheKey, $tmp)) { + $highscores = unserialize($tmp); + $needReCache = false; } } -?> - - - - - - - - -
-

Ranking for on


-
- - - - - - - - - - - - - - - - -= POT::SKILL_FIRST && $skill <= POT::SKILL_LAST) { // skills + if ($db->hasColumn('players', 'skill_fist')) {// tfs 1.0 + $skill_ids = array( + POT::SKILL_FIST => 'skill_fist', + POT::SKILL_CLUB => 'skill_club', + POT::SKILL_SWORD => 'skill_sword', + POT::SKILL_AXE => 'skill_axe', + POT::SKILL_DIST => 'skill_dist', + POT::SKILL_SHIELD => 'skill_shielding', + POT::SKILL_FISH => 'skill_fishing', + ); -$show_link_to_next_page = false; -$i = 0; + $highscores = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,vocation' . $promotion . $outfit . ', ' . $skill_ids[$skill] . ' as value FROM accounts,players WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 AND players.group_id < ' . config('highscores_groups_hidden') . ' ' . $add_sql . ' AND accounts.id = players.account_id ORDER BY ' . $skill_ids[$skill] . ' DESC LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } else + $highscores = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',value,level,vocation' . $promotion . $outfit . ' FROM accounts,players,player_skills WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 AND players.group_id < ' . config('highscores_groups_hidden') . ' ' . $add_sql . ' AND players.id = player_skills.player_id AND player_skills.skillid = ' . $skill . ' AND accounts.id = players.account_id ORDER BY value DESC, count DESC LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } else if ($skill == SKILL_FRAGS) // frags + { + if ($db->hasTable('player_killers')) { + $highscores = $db->query('SELECT accounts.country, players.id, players.name' . $online . ',level, vocation' . $promotion . $outfit . ', COUNT(`player_killers`.`player_id`) as value' . + ' FROM `accounts`, `players`, `player_killers` ' . + ' WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 AND players.group_id < ' . config('highscores_groups_hidden') . ' ' . $add_sql . ' AND players.id = player_killers.player_id AND accounts.id = players.account_id' . + ' GROUP BY `player_id`' . + ' ORDER BY value DESC' . + ' LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } else { + $db->query("SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));"); + + $highscores = $db->query('SELECT `a`.country, `p`.id, `p`.name' . $online . ',`p`.level, vocation' . $promotion . $outfit . ', COUNT(`pd`.`killed_by`) as value + FROM `players` p + LEFT JOIN `accounts` a ON `a`.`id` = `p`.`account_id` + LEFT JOIN `player_deaths` pd ON `pd`.`killed_by` = `p`.`name` + WHERE `p`.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') + AND `p`.' . $deleted . ' = 0 + AND `p`.group_id < ' . config('highscores_groups_hidden') . ' ' . $add_sql . ' + AND `pd`.`unjustified` = 1 + GROUP BY `killed_by` + ORDER BY value DESC + LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } + } else if ($skill == SKILL_BALANCE) // balance + { + $highscores = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,balance as value,vocation' . $promotion . $outfit . ' FROM accounts,players WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 AND players.group_id < ' . config('highscores_groups_hidden') . ' ' . $add_sql . ' AND accounts.id = players.account_id ORDER BY value DESC LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } else { + if ($skill == POT::SKILL__MAGLEVEL) { + $highscores = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',maglevel,level,vocation' . $promotion . $outfit . ' FROM accounts, players WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 ' . $add_sql . ' AND players.group_id < ' . config('highscores_groups_hidden') . ' AND accounts.id = players.account_id ORDER BY maglevel DESC, manaspent DESC LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + } else { // level + $highscores = $db->query('SELECT accounts.country, players.id,players.name' . $online . ',level,experience,vocation' . $promotion . $outfit . ' FROM accounts, players WHERE players.id NOT IN (' . implode(', ', config('highscores_ids_hidden')) . ') AND players.' . $deleted . ' = 0 ' . $add_sql . ' AND players.group_id < ' . config('highscores_groups_hidden') . ' AND accounts.id = players.account_id ORDER BY level DESC, experience DESC LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(); + $list = 'experience'; + } + } +} + +if ($cache->enabled() && $needReCache) { + $cache->set($cacheKey, serialize($highscores), config('highscores_cache_ttl') * 60); +} $online_exist = false; if($db->hasColumn('players', 'online')) $online_exist = true; $players = array(); -foreach($skills as $player) { +foreach($highscores as $player) { $players[] = $player['id']; } @@ -209,158 +220,105 @@ if($db->hasTable('players_online') && count($players) > 0) { } } -foreach($skills as $player) -{ - if(isset($is_online)) { - $player['online'] = (isset($is_online[$player['id']]) ? 1 : 0); - } else { - if(!isset($player['online'])) { - $player['online'] = 0; - } - } +$show_link_to_next_page = false; +$i = 0; - if(++$i <= $config['highscores_length']) +$configHighscoresVocation = config('highscores_vocation'); + +foreach($highscores as $id => &$player) +{ + if(isset($is_online)) { + $player['online'] = (isset($is_online[$player['id']]) ? 1 : 0); + } else { + if(!isset($player['online'])) { + $player['online'] = 0; + } + } + + if(++$i <= $configHighscoresPerPage) { if($skill == POT::SKILL__MAGIC) $player['value'] = $player['maglevel']; - else if($skill == POT::SKILL__LEVEL) + else if($skill == POT::SKILL__LEVEL) { $player['value'] = $player['level']; -echo ' - '; - if($config['account_country']) - echo ''; -echo ' - '; - if($config['highscores_outfit']) - echo ''; + $player['experience'] = number_format($player['experience']); + } -echo ' - - '; + } - if($skill == POT::SKILL__LEVEL) - echo ''; + $tmp = 'Unknown'; + if(isset($configVocations[$player['vocation']])) { + $tmp = $configVocations[$player['vocation']]; + } - echo ''; + $player['vocation'] = $tmp; + } + + + $player['link'] = getPlayerLink($player['name'], false); + $player['flag'] = getFlagImage($player['country']); + if($configHighscoresOutfit) { + $player['outfit'] = ''; + } + $player['rank'] = $offset + $i; } - else + else { + unset($highscores[$id]); $show_link_to_next_page = true; + break; + } } if(!$i) { - $extra = ($config['highscores_outfit'] ? 1 : 0); - echo ''; + $extra = ($configHighscoresOutfit ? 1 : 0); + echo ''; } -?> -
#RankOutfitNamePoints
' . getFlagImage($player['country']) . '' . ($offset + $i) . '. - - ' . $player['name'] . ' - '; - if($config['highscores_vocation']) { - if(isset($player['promotion'])) { - if((int)$player['promotion'] > 0) - $player['vocation'] += ($player['promotion'] * $config['vocations_amount']); - } - - $tmp = 'Unknown'; - if(isset($config['vocations'][$player['vocation']])) { - $tmp = $config['vocations'][$player['vocation']]; - } - echo '
' . $tmp . ''; + if($configHighscoresVocation) { + if(isset($player['promotion'])) { + if((int)$player['promotion'] > 0) { + $player['vocation'] += ($player['promotion'] * $configVocationsAmount); } -echo ' -
-
'.$player['value'].'
-
' . number_format($player['experience']) . '
No records yet.
No records yet.
- - 0) - echo ''; +$linkPreviousPage = ''; +if($_page > 0) { + $linkPreviousPage = getLink('highscores') . '/' . $list . ($vocation !== 'all' ? '/' . $vocation : '') . '/' . ($_page - 1); +} //link to next page if any result will be on next page -if($show_link_to_next_page) - echo ''; - -//end of page -echo '
Previous Page
Next Page
-
- '; -/* -if($config['highscores_country_box']) -{ - echo - ' - - - - - - -
Choose a country
- [ALL]
'; - for($i = 1; $i < count($config_vocations); $i++) - echo '' . $config_vocations[$i] . '
'; - echo ' -
'; -}*/ - -echo ' - - - - - - - -
Choose a skill
'; - $types = array( - 'experience' => 'Experience', - 'magic' => 'Magic', - 'shield' => 'Shielding', - 'distance' => 'Distance', - 'club' => 'Club', - 'sword' => 'Sword', - 'axe' => 'Axe', - 'fist' => 'Fist', - 'fishing' => 'Fishing', - ); - - if($config['highscores_frags']) { - $types['frags'] = 'Frags'; - } - if(isset($config['highscores_balance']) && $config['highscores_balance']) - $types['balance'] = 'Balance'; - - foreach($types as $link => $name) { - echo '' . $name . '
'; - } - -echo '

'; - -if($config['highscores_vocation_box']) -{ - echo - ' - - - - - - -
Choose a vocation
- [ALL]
'; - for($i = 1; $i <= $config['vocations_amount']; $i++) { - echo '' . $config_vocations[$i] . '
'; - } - echo ' -
'; +$linkNextPage = ''; +if($show_link_to_next_page) { + $linkNextPage = getLink('highscores') . '/' . $list . ($vocation !== 'all' ? '/' . $vocation : '') . '/' . ($_page + 1); } -?> -
+ + +$types = array( + 'experience' => 'Experience', + 'magic' => 'Magic', + 'shield' => 'Shielding', + 'distance' => 'Distance', + 'club' => 'Club', + 'sword' => 'Sword', + 'axe' => 'Axe', + 'fist' => 'Fist', + 'fishing' => 'Fishing', +); + +if(config('highscores_frags')) { + $types['frags'] = 'Frags'; +} +if(config('highscores_balance')) + $types['balance'] = 'Balance'; + +/** @var Twig\Environment $twig */ +$twig->display('highscores.html.twig', [ + 'highscores' => $highscores, + 'list' => $list, + 'skill' => $skill, + 'skillName' => ($skill == SKILL_FRAGS ? 'Frags' : ($skill == SKILL_BALANCE ? 'Balance' : getSkillName($skill))), + 'levelName' => ($skill != SKILL_FRAGS && $skill != SKILL_BALANCE ? 'Level' : ($skill == SKILL_BALANCE ? 'Balance' : 'Frags')), + 'vocation' => $vocation !== 'all' ? $vocation : null, + 'types' => $types, + 'linkPreviousPage' => $linkPreviousPage, + 'linkNextPage' => $linkNextPage, +]); diff --git a/system/templates/highscores.html.twig b/system/templates/highscores.html.twig new file mode 100644 index 00000000..3aa4a983 --- /dev/null +++ b/system/templates/highscores.html.twig @@ -0,0 +1,105 @@ + + + + + + + + +
+

Ranking for {{ skillName }}{% if vocation is not null %} ({{ vocation }}){% endif %} on {{ config.lua.serverName }}


+
+ + + {% if config.account_country %} + + {% endif %} + + + + {% if config.highscores_outfit %} + + {% endif %} + + + + + + {% if skill == constant('POT::SKILL__LEVEL') %} + + {% endif %} + + + {% set row = 0 %} + {% for player in highscores %} + + {% set row = row + 1 %} + + {% if config.account_country %} + + {% endif %} + + + + {% if config.highscores_outfit %} + + {% endif %} + + + + + + {% if skill == constant('POT::SKILL__LEVEL') %} + + {% endif %} + + {% endfor %} +
#RankOutfitName{{ levelName }}Points
{{ player.flag|raw }}{{ player.rank }}.{{ player.outfit|raw }} + + {{ player.name }} + + {% if config.highscores_vocation %} +
{{ player.vocation }} + {% endif %} +
+
{{ player.value }}
+
{{ player.experience }}
+ + {% if linkPreviousPage|length > 0 %} + + {% endif %} + + {% if linkNextPage|length > 0 %} + + {% endif %} +
Previous Page
Next Page
+
+ + + + + + + +
Choose a skill
+ {% for link, name in types %} + {{ name }}
+ {% endfor %} +
+
+ {% if config.highscores_vocation_box %} + + + + + + + +
Choose a vocation
+ [ALL]
+ {% for i in 1..config.vocations_amount %} + {{ config.vocations[i]}}
+ {% endfor %} +
+ {% endif %} +
\ No newline at end of file