Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: improvements of status with multiworld #120

Draft
wants to merge 9 commits into
base: phacUFPE/multiworld_system
Choose a base branch
from
Draft
2 changes: 1 addition & 1 deletion admin/template/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getWorldsStatus($style = ''): string
global $status;
$output = "";
foreach (WORLDS as $world) {
$_s = $status['online'] ? 'success' : 'danger';
$_s = $status[$world['id']]['online'] ? 'success' : 'danger';
$output .= "<span class='badge ms-1 bg-$_s' style='$style'>{$world['name']}</span>";
}
return $output;
Expand Down
24 changes: 14 additions & 10 deletions admin/tools/status.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
global $status;
define('MYAAC_ADMIN', true);

require '../../common.php';
Expand All @@ -7,16 +8,19 @@
require SYSTEM . 'status.php';
require SYSTEM . 'login.php';

$worldId = $_GET['world'] ?? 1;
$_status = $status[$worldId];

if (!admin())
die('Access denied.');
die('Access denied.');

if (!$status['online'])
die('Offline');
if (!$_status['online'])
die('Offline');
?>
<b>Server</b>: <?= $status['server'] . ' ' . $status['serverVersion']; ?><br/>
<b>Version</b>: <?= $status['clientVersion']; ?><br/><br/>
<b>Monsters</b>: <?= $status['monsters']; ?><br/>
<b>Map</b>: <?= $status['mapName']; ?>, <b>author</b>: <?= $status['mapAuthor']; ?>,
<b>size</b>: <?= $status['mapWidth'] . ' x ' . $status['mapHeight']; ?><br/>
<b>MOTD</b>: <?= $status['motd']; ?><br/><br/>
<b>Last check</b>: <?= date("H:i:s", $status['lastCheck']); ?>
<b>Server</b>: <?= $_status['server'] . ' ' . $_status['serverVersion']; ?><br />
<b>Version</b>: <?= $_status['clientVersion']; ?><br /><br />
<b>Monsters</b>: <?= $_status['monsters']; ?><br />
<b>Map</b>: <?= $_status['mapName']; ?>, <b>author</b>: <?= $_status['mapAuthor']; ?>,
<b>size</b>: <?= $_status['mapWidth'] . ' x ' . $_status['mapHeight']; ?><br />
<b>MOTD</b>: <?= $_status['motd']; ?><br /><br />
<b>Last check</b>: <?= date("H:i:s", $_status['lastCheck']); ?>
2 changes: 1 addition & 1 deletion common.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

define('MYAAC', true);
define('MYAAC_VERSION', '0.8.16');
define('DATABASE_VERSION', 35);
define('DATABASE_VERSION', 36);
define('TABLE_PREFIX', 'myaac_');
define('START_TIME', microtime(true));
define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX'));
Expand Down
8 changes: 6 additions & 2 deletions install/includes/schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SET @myaac_database_version = 34;
SET @myaac_database_version = 36;

CREATE TABLE `myaac_account_actions`
(
Expand Down Expand Up @@ -78,8 +78,12 @@ CREATE TABLE `myaac_config`
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`value` VARCHAR(1000) NOT NULL,
`world_id` INT(3) UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
UNIQUE (`name`)
UNIQUE (`name`, `world_id`),
CONSTRAINT `myaac_config_worlds_fk`
FOREIGN KEY (`world_id`) REFERENCES `worlds` (`id`)
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

INSERT INTO `myaac_config` (`name`, `value`) VALUES ('database_version', @myaac_database_version);
Expand Down
17 changes: 10 additions & 7 deletions system/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,15 @@ function getForumBoards()
*
* @param string $name Key.
* @param string &$value Reference where requested data will be set to.
* @param int $worldId optional to identify world_id (changed only on status_) keys.
* @return bool False if value was not found in table, otherwise true.
*/
function fetchDatabaseConfig($name, &$value)
function fetchDatabaseConfig($name, &$value, int $worldId = 1)
{
global $db;
global $db, $TABLE_PREFIX;

$query = $db->query(
'SELECT `value` FROM `' . TABLE_PREFIX . 'config` WHERE `name` = ' . $db->quote($name)
"SELECT `value` FROM `{$TABLE_PREFIX}config` WHERE `name` = {$db->quote($name)} AND `world_id` = {$worldId}"
);
if ($query->rowCount() <= 0) {
return false;
Expand Down Expand Up @@ -389,23 +390,25 @@ function getDatabaseConfig($name)
*
* @param string $name Key name.
* @param string $value Data to be associated with key.
* @param int $worldId optional to identify world_id (changed only on status_) keys.
*/
function registerDatabaseConfig($name, $value)
function registerDatabaseConfig($name, $value, int $worldId = 1): void
{
global $db;
$db->insert(TABLE_PREFIX . 'config', ['name' => $name, 'value' => $value]);
$db->insert(TABLE_PREFIX . 'config', ['name' => $name, 'value' => $value, 'world_id' => $worldId]);
}

/**
* Updates a value in myaac database config.
*
* @param string $name Key name.
* @param string $value New data.
* @param int $worldId optional to identify world_id (changed only on status_) keys.
*/
function updateDatabaseConfig($name, $value)
function updateDatabaseConfig($name, $value, int $worldId = 1): void
{
global $db;
$db->update(TABLE_PREFIX . 'config', ['value' => $value], ['name' => $name]);
$db->update(TABLE_PREFIX . 'config', ['value' => $value], ['name' => $name, 'world_id' => $worldId]);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions system/migrations/36.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
// Add colum world_id and adjusts privileges on myaac_config table
global $db;
$db->exec("ALTER TABLE `myaac_config` ADD `world_id` INT(3) UNSIGNED NOT NULL DEFAULT 1 AFTER `value`");
$db->exec("ALTER TABLE `myaac_config` ADD FOREIGN KEY (`world_id`) REFERENCES `worlds` (`id`) ON DELETE CASCADE");
$db->exec("ALTER TABLE `myaac_config` DROP INDEX `name`");
$db->exec("ALTER TABLE `myaac_config` ADD UNIQUE `unique_name_world` (`name`, `world_id`)");
3 changes: 2 additions & 1 deletion system/pages/online.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php global $db, $config, $twig;
<?php global $db, $config, $twig, $status;
/**
* Online
*
Expand Down Expand Up @@ -110,4 +110,5 @@
'record' => $record,
'vocations' => $vocations,
'world' => $world,
'status' => $world ? $status[$world['id']] : null,
));
2 changes: 1 addition & 1 deletion system/pages/serverinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@
'weeklyFragsToRedSkull' => configLua('weekKillsToRedSkull') ?? null,
'monthlyFragsToRedSkull' => configLua('monthKillsToRedSkull') ?? null,
'world' => $world,
// 'status' => $world ? $status[$world['id']] : null,
'status' => $world ? $status[$world['id']] : null,
]);
153 changes: 77 additions & 76 deletions system/status.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
global $TABLE_PREFIX;
<?php global $db, $TABLE_PREFIX;
/**
* Server status
*
Expand All @@ -11,24 +10,28 @@
*/
defined('MYAAC') or die('Direct access not allowed!');

$status = [];
$status['online'] = false;
$status['players'] = 0;
$status['playersMax'] = 0;
$status['lastCheck'] = 0;
$status['uptime'] = '0h 0m';
$status['monsters'] = 0;

if (config('status_enabled') === false) {
return;
}

$status = [];

$worlds = $db->query("SELECT `id`, `name`, `port_status` FROM `worlds` ORDER BY `id` ASC")->fetchAll(PDO::FETCH_ASSOC);
foreach ($worlds as $w) {
$status[$w['id']] = [
'online' => false,
'players' => 0,
'playersMax' => 0,
'lastCheck' => 0,
'uptime' => '0h 0m',
'monsters' => 0,
'port' => $w['port_status'],
];
}

/** @var array $config */
$statusIp = configLua('ip');
if ($statusProtocolPort = configLua('statusProtocolPort')) {
$config['lua']['loginPort'] = $statusProtocolPort;
$config['lua']['statusPort'] = $statusProtocolPort;
}
$statusProtocolPort = configLua('statusProtocolPort');

// ip check
$statusIp = !empty($config['status_ip'] ?? '') ? $config['status_ip'] : $statusIp;
Expand All @@ -48,110 +51,108 @@
}
}

if ($fetch_from_db) {
// get info from db
/** @var OTS_DB_MySQL $db */
$status_query = $db->query(
"SELECT `name`, `value` FROM `{$TABLE_PREFIX}config` WHERE {$db->fieldName(
'name'
)} LIKE '%status%'"
);
if ($status_query->rowCount() <= 0) {
// empty, just insert it
foreach ($status as $key => $value) {
registerDatabaseConfig('status_' . $key, $value);
}
} else {
foreach ($status_query as $tmp) {
$status[str_replace('status_', '', $tmp['name'])] = $tmp['value'];
// get status timeout from server config
$statusTimeout = configLua('statusTimeout');
$statusTimeout = eval("return {$statusTimeout};") / 1000 + 1;
$statusInterval = @$config['status_interval'];
if ($statusInterval && $statusTimeout < $statusInterval) {
$statusTimeout = $statusInterval;
}

foreach ($status as $worldId => $statusItem) {
if ($fetch_from_db) {
// get info from db
/** @var OTS_DB_MySQL $db */
$status_query = $db->query(
"SELECT `name`, `value` FROM `{$TABLE_PREFIX}config` WHERE {$db->fieldName('name')} LIKE '%status%' AND `world_id` = {$worldId}"
)->fetchAll(PDO::FETCH_ASSOC);
if (count($status_query) > 0) {
foreach ($status_query as $item) {
$statusItem[str_replace('status_', '', $item['name'])] = $item['value'];
}
} else {
// empty, just insert it
foreach ($statusItem as $key => $value) {
registerDatabaseConfig("status_$key", $value, $worldId);
}
}
}
}

if (isset($config['lua']['statustimeout'])) {
$config['lua']['statusTimeout'] = configLua('statustimeout');
}
if ($statusItem['lastCheck'] + $statusTimeout < time()) {
updateStatus($statusItem, $statusIp, $worldId);
}

// get status timeout from server config
$status_timeout = eval('return ' . configLua('statusTimeout') . ';') / 1000 + 1;
$status_interval = @$config['status_interval'];
if ($status_interval && $status_timeout < $config['status_interval']) {
$status_timeout = $config['status_interval'];
}
$status[$worldId] = $statusItem;

if ($status['lastCheck'] + $status_timeout < time()) {
updateStatus($statusIp, $statusProtocolPort);
if ($cache->enabled()) {
$cache->set('status', serialize($status), 120);
}
}

function updateStatus($statusIp, $statusPort): void
function updateStatus(&$_status, $statusIp, $worldId): void
{
global $db, $cache, $config, $status;
global $db, $config;

// get server status and save it to database
$serverInfo = new OTS_ServerInfo($statusIp, $statusPort);
$serverInfo = new OTS_ServerInfo($statusIp, $_status['port']);
$serverStatus = $serverInfo->status();
if (!$serverStatus) {
$status['online'] = false;
$status['players'] = 0;
$status['playersMax'] = 0;
$_status['online'] = false;
$_status['players'] = 0;
$_status['playersMax'] = 0;
} else {
$status['lastCheck'] = time(); // this should be set only if server respond

$status['online'] = true;
$status['players'] = $serverStatus->getOnlinePlayers(); // counts all players logged in-game, or only connected clients (if enabled on server side)
$status['playersMax'] = $serverStatus->getMaxPlayers();
$_status['lastCheck'] = time(); // this should be set only if server respond
$_status['online'] = true;
$_status['players'] = $serverStatus->getOnlinePlayers(); // counts all players logged in-game, or only connected clients (if enabled on server side)
$_status['playersMax'] = $serverStatus->getMaxPlayers();

// for status afk thing
if ($config['online_afk']) {
// get amount of players that are currently logged in-game, including disconnected clients (exited)
if ($db->hasTable('players_online')) {
// tfs 1.x
$query = $db->query('SELECT COUNT(`player_id`) AS `playersTotal` FROM `players_online`;');
$query = $db->query("SELECT COUNT(`player_id`) AS `playersTotal` FROM `players_online` WHERE `world_id` = {$worldId};");
} else {
$query = $db->query(
'SELECT COUNT(`id`) AS `playersTotal` FROM `players` WHERE `online` > 0'
"SELECT COUNT(`id`) AS `playersTotal` FROM `players` WHERE `online` > 0 AND `world_id` = {$worldId};"
);
}

$status['playersTotal'] = 0;
$_status['playersTotal'] = 0;
if ($query->rowCount() > 0) {
$query = $query->fetch();
$status['playersTotal'] = $query['playersTotal'];
$_status['playersTotal'] = $query['playersTotal'];
}
}

$uptime = $status['uptime'] = $serverStatus->getUptime();
$uptime = $_status['uptime'] = $serverStatus->getUptime();
$m = date('m', $uptime);
$m = $m > 1 ? "$m months, " : ($m == 1 ? 'month, ' : '');
$d = date('d', $uptime);
$d = $d > 1 ? "$d days, " : ($d == 1 ? 'day, ' : '');
$h = date('H', $uptime);
$min = date('i', $uptime);
$status['uptimeReadable'] = "{$m}{$d}{$h}h {$min}m";
$_status['uptimeReadable'] = "{$m}{$d}{$h}h {$min}m";

$status['monsters'] = $serverStatus->getMonstersCount();
$status['motd'] = $serverStatus->getMOTD();
$_status['monsters'] = $serverStatus->getMonstersCount();
$_status['motd'] = $serverStatus->getMOTD();

$status['mapAuthor'] = $serverStatus->getMapAuthor();
$status['mapName'] = $serverStatus->getMapName();
$status['mapWidth'] = $serverStatus->getMapWidth();
$status['mapHeight'] = $serverStatus->getMapHeight();
$_status['mapAuthor'] = $serverStatus->getMapAuthor();
$_status['mapName'] = $serverStatus->getMapName();
$_status['mapWidth'] = $serverStatus->getMapWidth();
$_status['mapHeight'] = $serverStatus->getMapHeight();

$status['server'] = $serverStatus->getServer();
$status['serverVersion'] = $serverStatus->getServerVersion();
$status['clientVersion'] = $serverStatus->getClientVersion();
}

if ($cache->enabled()) {
$cache->set('status', serialize($status), 120);
$_status['server'] = $serverStatus->getServer();
$_status['serverVersion'] = $serverStatus->getServerVersion();
$_status['clientVersion'] = $serverStatus->getClientVersion();
}

$tmpVal = null;
foreach ($status as $key => $value) {
if (fetchDatabaseConfig('status_' . $key, $tmpVal)) {
updateDatabaseConfig('status_' . $key, $value);
foreach ($_status as $key => $value) {
if (fetchDatabaseConfig("status_$key", $tmpVal, $worldId)) {
updateDatabaseConfig("status_$key", $value, $worldId);
} else {
registerDatabaseConfig('status_' . $key, $value);
registerDatabaseConfig("status_$key", $value, $worldId);
}
}
}
17 changes: 16 additions & 1 deletion templates/tibiacom/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
//templates\tibiacom\config.ini
if (isset($config['boxes']))
$config['boxes'] = explode(",", $config['boxes']);

function getTotalPlayersOnline()
{
global $status;
$servers = 0;
$players = 0;
foreach ($status as $item) {
if ($item['online'] ?? false) {
$servers++;
$players = $players + ($item['players'] ?? 0);
}
}
return $servers == 0 ? "All Worlds Offline" : ($players > 0 ? "$players Players Online" : "$servers Worlds Online");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
Expand Down Expand Up @@ -533,7 +547,8 @@ class="InfoBarSmallElement">Discord</span></a>
<span class="InfoBarNumbers">
<span class="InfoBarSmallElement">
<a class="InfoBarLinks" href="?worlds">
<?= $status['online'] ? $status['players'] . ' Players Online' : 'Server Offline' ?></a>
<?= getTotalPlayersOnline() ?>
</a>
</span>
</span>
<?php if ($config['collapse_status']) { ?>
Expand Down
Loading
Loading