Optimize $player->isOnline() function, thanks @gesior

This commit is contained in:
slawkens 2025-01-17 22:52:00 +01:00
parent eda773cb55
commit 7c3ebf70fa

View File

@ -108,6 +108,8 @@ class OTS_Player extends OTS_Row_DAO
POT::SKILL_SHIELD => array('value' => 0, 'tries' => 0),
POT::SKILL_FISH => array('value' => 0, 'tries' => 0)
);
private static array $playersOnline;
/**
* Magic PHP5 method.
*
@ -763,21 +765,29 @@ class OTS_Player extends OTS_Row_DAO
$this->data['deleted'] = (int) $deleted;
}
public function isOnline()
{
if($this->db->hasTable('players_online')) // tfs 1.0
{
$query = $this->db->query('SELECT `player_id` FROM `players_online` WHERE `player_id` = ' . $this->data['id']);
return $query->rowCount() > 0;
public function isOnline()
{
if($this->db->hasTable('players_online')) {// tfs 1.0
if (!isset(self::$playersOnline)) {
self::$playersOnline = [];
$query = $this->db->query('SELECT `player_id` FROM `players_online`');
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $item) {
self::$playersOnline[$item['player_id']] = true;
}
}
return isset(self::$playersOnline[$this->data['id']]);
}
if( !isset($this->data['online']) )
{
throw new E_OTS_NotLoaded();
}
if( !isset($this->data['online']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['online'] == 1;
}
return $this->data['online'] == 1;
}
public function getCreated()
{