Ejemplo n.º 1
0
 /**
  * Get Session of Player
  * @param array $keyPair
  * @return Session|null
  */
 public function getSession($keyPair = [])
 {
     $pairs = array_merge(['player' => null, 'name' => null, 'uuid' => null], $keyPair);
     foreach ($pairs as $key => $val) {
         if ($val != null && $val != '') {
             if ($key == 'player' && $val instanceof Player) {
                 /* @var $val Player */
                 return $this->getSession(['uuid' => $val->getUUID()]);
             }
             $filename = $this->options['cache_folder_sessions'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == 'name') {
                 if (file_exists($filename) || $this->hasPaid($val)) {
                     $uuid = $this->getUUID($val);
                     return $this->getSession(['uuid' => $uuid]);
                 }
             } elseif ($key == 'uuid') {
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Session($content, $this);
                     }
                 }
                 $response = $this->fetch('session', $key, $val);
                 if ($response['success'] == true) {
                     $SESSION = new Session(['record' => $response['session'], 'extra' => $content['extra']], $this);
                     $SESSION->setExtra(['filename' => $filename]);
                     $this->setCache($filename, $SESSION);
                     return $SESSION;
                 }
             }
         }
     }
     if ($this->getCacheTime() < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME);
         return $this->getSession($pairs);
     }
     return null;
 }
 /**
  * Get Session of Player
  * @param array $pairs
  * @return Session|null
  */
 public function getSession($pairs = [])
 {
     foreach ($pairs as $key => $val) {
         if ($val != null && $val != '') {
             if ($key == KEYS::SESSION_BY_PLAYER_OBJECT && $val instanceof Player) {
                 /* @var $val Player */
                 return $this->getSession([KEYS::SESSION_BY_UUID => $val->getUUID()]);
             }
             $filename = $this->options['cache_folder_sessions'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == KEYS::SESSION_BY_NAME) {
                 if (file_exists($filename) || $this->hasPaid($val)) {
                     $uuid = $this->getUUID($val);
                     return $this->getSession([KEYS::SESSION_BY_UUID => $uuid]);
                 }
             } elseif ($key == KEYS::SESSION_BY_UUID) {
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Session($content, $this);
                     }
                 }
                 $response = $this->fetch(API_REQUESTS::SESSION, $key, $val);
                 if ($response['success'] == true) {
                     $SESSION = new Session(['record' => $response['session'], 'extra' => $content['extra']], $this);
                     if (!is_array($SESSION->getRecord())) {
                         $SESSION->JSONArray['record'] = [];
                     }
                     $SESSION->JSONArray['record']['uuid'] = (string) $val;
                     $SESSION->setExtra(['filename' => $filename]);
                     $this->setCache($filename, $SESSION);
                     return $SESSION;
                 }
             }
         }
     }
     if ($this->getCacheTime() < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME);
         return $this->getSession($pairs);
     }
     return null;
 }