From 7c3ebf70fa4751af986be7b46ee3530d4875271e Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 17 Jan 2025 22:52:00 +0100 Subject: [PATCH] Optimize $player->isOnline() function, thanks @gesior --- system/libs/pot/OTS_Player.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/system/libs/pot/OTS_Player.php b/system/libs/pot/OTS_Player.php index 62fd3343..7c66c304 100644 --- a/system/libs/pot/OTS_Player.php +++ b/system/libs/pot/OTS_Player.php @@ -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() {