Compare commits

..

4 Commits

Author SHA1 Message Date
slawkens
1a6ef4125e [WIP] Use array with index instead of switch 2025-05-16 20:18:40 +02:00
slawkens
606fb0673c Option to extend the highscores with hooks 2025-05-16 15:03:03 +02:00
slawkens
1e9b10d648 Fix twig variables: logged + account_logged being not set directly after login 2025-05-15 19:11:20 +02:00
slawkens
7c92d1c197 Start v1.5.1-dev 2025-05-14 15:11:20 +02:00
6 changed files with 100 additions and 85 deletions

View File

@@ -26,7 +26,7 @@
if (version_compare(phpversion(), '8.1', '<')) die('PHP version 8.1 or higher is required.');
const MYAAC = true;
const MYAAC_VERSION = '1.5';
const MYAAC_VERSION = '1.5.1-dev';
const DATABASE_VERSION = 45;
const TABLE_PREFIX = 'myaac_';
define('START_TIME', microtime(true));

View File

@@ -138,6 +138,9 @@ $ots = POT::getInstance();
$eloquentConnection = null;
require_once SYSTEM . 'database.php';
$twig->addGlobal('logged', false);
$twig->addGlobal('account_logged', new \OTS_Account());
// verify myaac tables exists in database
if(!defined('MYAAC_INSTALL') && !$db->hasTable('myaac_account_actions')) {
throw new RuntimeException('Seems that the table myaac_account_actions of MyAAC doesn\'t exist in the database. This is a fatal error. You can try to reinstall MyAAC by visiting ' . (IS_CLI ? 'http://your-ip.com/' : BASE_URL) . 'install');

View File

@@ -95,3 +95,8 @@ else {
}
$hooks->trigger(HOOK_ACCOUNT_LOGIN_POST);
if($logged) {
$twig->addGlobal('logged', true);
$twig->addGlobal('account_logged', $account_logged);
}

View File

@@ -58,64 +58,57 @@ if($vocation !== 'all') {
}
}
$skill = POT::SKILL__LEVEL;
if(is_numeric($list))
{
$list = (int) $list;
if($list >= POT::SKILL_FIRST && $list <= POT::SKILL__LAST)
$skill = $list;
$categories = [
'experience' => 'Experience',
'magic' => 'Magic',
'shield' => 'Shielding',
'distance' => 'Distance',
'club' => 'Club',
'sword' => 'Sword',
'axe' => 'Axe',
'fist' => 'Fist',
'fishing' => 'Fishing',
];
if(setting('core.highscores_frags')) {
$categories['frags'] = 'Frags';
}
else
{
switch($list)
{
case 'fist':
$skill = POT::SKILL_FIST;
break;
case 'club':
$skill = POT::SKILL_CLUB;
break;
case 'sword':
$skill = POT::SKILL_SWORD;
break;
case 'axe':
$skill = POT::SKILL_AXE;
break;
case 'distance':
$skill = POT::SKILL_DIST;
break;
case 'shield':
$skill = POT::SKILL_SHIELD;
break;
case 'fishing':
$skill = POT::SKILL_FISH;
break;
case 'level':
case 'experience':
$skill = POT::SKILL_LEVEL;
break;
case 'magic':
$skill = POT::SKILL__MAGLEVEL;
break;
case 'frags':
if(setting('core.highscores_frags'))
$skill = SKILL_FRAGS;
break;
case 'balance':
if(setting('core.highscores_balance'))
$skill = SKILL_BALANCE;
break;
$categories['balance'] = 'Balance';
$skill = POT::SKILL__LEVEL;
$skillNameToId = [
'fist' => POT::SKILL_FIST,
'club' => POT::SKILL_CLUB,
'sword' => POT::SKILL_SWORD,
'axe' => POT::SKILL_AXE,
'distance' => POT::SKILL_DIST,
'shield' => POT::SKILL_SHIELD,
'fishing' => POT::SKILL_FISH,
'magic' => POT::SKILL__MAGLEVEL,
];
if(setting('core.highscores_frags')) {
$skillNameToId['frags'] = SKILL_FRAGS;
}
if(setting('core.highscores_balance')) {
$skillNameToId['balance'] = SKILL_BALANCE;
}
$skill = $skillNameToId[$list];
$args = ['list' => $list, 'skill' => $skill, 'categories' => $categories];
$hooks->triggerFilter(HOOK_FILTER_HIGHSCORES_LIST, $args);
$list = $args['list'];
$skill = $args['skill'];
$categories = $args['categories'];
if (!isset($categories[$list])) {
$skill = null;
}
$promotion = '';
@@ -162,8 +155,18 @@ $query->join('accounts', 'accounts.id', '=', 'players.account_id')
->selectRaw('accounts.country, players.id, players.name, players.account_id, players.level, players.vocation' . $outfit . $promotion)
->orderByDesc('value');
if ($skill == SKILL_FRAGS) {
$skillName = 'Frags';
}
else if($skill == SKILL_BALANCE) {
$skillName = 'Balance';
}
else {
$skillName = getSkillName($skill);
}
if (empty($highscores)) {
if ($skill >= POT::SKILL_FIRST && $skill <= POT::SKILL_LAST) { // skills
if ($skill && $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',
@@ -193,20 +196,37 @@ if (empty($highscores)) {
{
$query
->addSelect('players.balance as value');
} else {
if ($skill == POT::SKILL__MAGLEVEL) {
}
else if ($skill == POT::SKILL__MAGLEVEL) {
$query
->addSelect('players.maglevel as value', 'players.maglevel')
->orderBy('manaspent');
} else { // level
} else if ($skill == POT::SKILL__LEVEL) {
$query
->addSelect('players.level as value', 'players.experience')
->orderBy('experience');
->orderBy('experience', 'desc');
$list = 'experience';
}
else if ($skill) {
$args = [
'list' => $list,
'skill' => $skill,
'skillName' => $skillName,
'query' => $query
];
$hooks->triggerFilter(HOOK_FILTER_HIGHSCORES, $args);
$list = $args['list'];
$skill = $args['skill'];
$skillName = $args['skillName'];
$query = $args['query'];
}
else {
$query = null;
}
$highscores = $query->get()->map(function($row) {
$highscores = ($query ? $query->get()->map(function($row) {
$tmp = $row->toArray();
$tmp['online'] = $row->online_status;
$tmp['vocation'] = $row->vocation_name;
@@ -214,7 +234,7 @@ if (empty($highscores)) {
unset($tmp['online_table']);
return $tmp;
})->toArray();
})->toArray() : []);
}
if ($highscoresTTL > 0 && $cache->enabled() && $needReCache) {
@@ -239,9 +259,11 @@ foreach($highscores as $id => &$player)
$player['link'] = getPlayerLink($player['name'], false);
$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['rank'] = $offset + $i;
}
else {
@@ -263,24 +285,6 @@ 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(setting('core.highscores_frags')) {
$types['frags'] = 'Frags';
}
if(setting('core.highscores_balance'))
$types['balance'] = 'Balance';
if ($highscoresTTL > 0 && $cache->enabled()) {
echo '<small>*Note: Highscores are updated every' . ($highscoresTTL > 1 ? ' ' . $highscoresTTL : '') . ' minute' . ($highscoresTTL > 1 ? 's' : '') . '.</small><br/><br/>';
}
@@ -290,11 +294,12 @@ $twig->display('highscores.html.twig', [
'highscores' => $highscores,
'list' => $list,
'skill' => $skill,
'skillName' => ($skill == SKILL_FRAGS ? 'Frags' : ($skill == SKILL_BALANCE ? 'Balance' : getSkillName($skill))),
'skillName' => $skillName,
'levelName' => ($skill != SKILL_FRAGS && $skill != SKILL_BALANCE ? 'Level' : ($skill == SKILL_BALANCE ? 'Balance' : 'Frags')),
'vocation' => $vocation !== 'all' ? $vocation : null,
'vocationId' => $vocationId,
'types' => $types,
'categories' => $categories,
'types' => $categories, // leave for compatibility with outdated twigs
'linkPreviousPage' => $linkPreviousPage,
'linkNextPage' => $linkNextPage,
]);

View File

@@ -98,6 +98,8 @@ define('HOOK_INSTALL_FINISH', ++$i);
define('HOOK_INSTALL_FINISH_END', ++$i);
// hook filters
define('HOOK_FILTER_HIGHSCORES_LIST', ++$i);
define('HOOK_FILTER_HIGHSCORES', ++$i);
define('HOOK_FILTER_ROUTES', ++$i);
define('HOOK_FILTER_TWIG_DISPLAY', ++$i);
define('HOOK_FILTER_TWIG_RENDER', ++$i);

View File

@@ -9,7 +9,7 @@
<td>
<label for="skillFilter">Choose a Skill</label>
<select onchange="location = this.value;" id="skillFilter">
{% for link, name in types %}
{% for link, name in categories %}
<option value="{{ getLink('highscores') }}/{{ link|urlencode }}{% if vocation is not null %}/{{ vocation|lower|urlencode }}{% endif %}" class="size_xs" {% if list is not null and list == link %}selected{% endif %}>{{ name }}</option>
{% endfor %}
</select>
@@ -102,7 +102,7 @@
</tr>
<tr bgcolor="{{ config.lightborder }}">
<td>
{% for link, name in types %}
{% for link, name in categories %}
<a href="{{ getLink('highscores') }}/{{ link|urlencode }}{% if vocation is not null %}/{{ vocation|urlencode }}{% endif %}" class="size_xs">{{ name }}</a><br/>
{% endfor %}
</td>