Example #1
0
 /**
  * get Guild of Player
  * @param array $keyPair
  * @return Guild|null
  */
 public function getGuild($keyPair = [])
 {
     $pairs = array_merge(['player' => null, 'byUuid' => null, 'byPlayer' => null, 'byName' => null, 'id' => null], $keyPair);
     foreach ($pairs as $key => $val) {
         if ($val != null && $val != '') {
             if ($key == 'player' && $val instanceof Player) {
                 /* @var $val Player */
                 return $this->getGuild(['byUuid' => $val->getUUID()]);
             }
             $filename = $this->options['cache_folder_guild'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == 'byPlayer' || $key == 'byName' || $key == 'byUuid') {
                 if ($key == 'byPlayer') {
                     $uuid = $this->getUUID($val);
                     return $this->getGuild(['byUuid' => $uuid]);
                 }
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         if (isset($content['guild'])) {
                             return $this->getGuild(['id' => $content['guild']]);
                         }
                         continue;
                     }
                 }
                 $response = $this->fetch('findGuild', $key, $val, 5);
                 if ($response['success'] == true) {
                     $content = ['timestamp' => time(), 'guild' => $response['guild']];
                     $this->setFileContent($filename, json_encode($content));
                     return $this->getGuild(['id' => $response['guild']]);
                 }
             }
             if ($key == 'id') {
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Guild($content, $this);
                     }
                 }
                 $response = $this->fetch('guild', $key, $val);
                 if ($response['success'] == true) {
                     $GUILD = new Guild(['record' => $response['guild'], 'extra' => $content['extra']], $this);
                     $GUILD->setExtra(['filename' => $filename]);
                     $this->setCache($filename, $GUILD);
                     return $GUILD;
                 }
             }
         }
     }
     if ($this->getCacheTime() < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME);
         return $this->getGuild($pairs);
     }
     return null;
 }
 /**
  * get Guild of Player
  * @param array $pairs
  * @return Guild|null
  */
 public function getGuild($pairs = [])
 {
     foreach ($pairs as $key => $val) {
         if ($val != null && $val != '') {
             if ($key == KEYS::GUILD_BY_PLAYER_OBJECT || $key == KEYS::GUILD_BY_PLAYER_NAME) {
                 return $this->getGuild([KEYS::GUILD_BY_PLAYER_UUID => $this->getUUIDFromVar($val)]);
             }
             $filename = $this->options['cache_folder_guild'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == KEYS::GUILD_BY_NAME || $key == KEYS::GUILD_BY_PLAYER_UUID) {
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         if (isset($content['guild'])) {
                             return $this->getGuild([KEYS::GUILD_BY_ID => $content['guild']]);
                         }
                         continue;
                     }
                 }
                 $response = $this->fetch(API_REQUESTS::FIND_GUILD, $key, $val, 5000);
                 if ($response['success'] == true) {
                     $content = ['timestamp' => time(), 'guild' => $response['guild']];
                     $this->setFileContent($filename, json_encode($content));
                     return $this->getGuild([KEYS::GUILD_BY_ID => $response['guild']]);
                 }
             }
             if ($key == KEYS::GUILD_BY_ID) {
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Guild($content, $this);
                     }
                 }
                 $response = $this->fetch(API_REQUESTS::GUILD, $key, $val);
                 if ($response['success'] == true) {
                     $GUILD = new Guild(['record' => $response['guild'], 'extra' => $content['extra']], $this);
                     $GUILD->setExtra(['filename' => $filename]);
                     $GUILD->handleNew();
                     $this->setCache($filename, $GUILD);
                     return $GUILD;
                 }
             }
         }
     }
     if ($this->getCacheTime(CACHE_TIMES::GUILD) < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME, CACHE_TIMES::GUILD);
         return $this->getGuild($pairs);
     }
     return null;
 }