/**
  * @param array $pairs
  * @return Player|null
  */
 public function getPlayer($pairs = [])
 {
     foreach ($pairs as $key => $val) {
         if ($val != null && $val != '') {
             if ($key == KEYS::PLAYER_BY_UNKNOWN || $key == KEYS::PLAYER_BY_NAME) {
                 return $this->getPlayer([KEYS::PLAYER_BY_UUID => $this->getUUIDFromVar($val)]);
             }
             $filename = $this->options['cache_folder_player'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == KEYS::PLAYER_BY_UUID) {
                 $val = Utilities::ensureNoDashesUUID($val);
                 if (InputType::getType($val) != InputType::UUID) {
                     continue;
                 }
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Player($content, $this);
                     }
                 }
                 $response = $this->fetch(API_REQUESTS::PLAYER, $key, $val);
                 if ($response['success'] == true) {
                     $PLAYER = new Player(['record' => $response['player'], 'extra' => $content['extra']], $this);
                     $PLAYER->setExtra(['filename' => $filename]);
                     $PLAYER->handleNew();
                     $this->setCache($filename, $PLAYER);
                     return $PLAYER;
                 }
             }
         }
     }
     if ($this->getCacheTime(CACHE_TIMES::PLAYER) < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME, CACHE_TIMES::PLAYER);
         return $this->getPlayer($pairs);
     }
     return null;
 }
Example #2
0
 /**
  * @param array $keyPair
  * @return Player|null
  */
 public function getPlayer($keyPair = [])
 {
     $pairs = array_merge(['name' => null, 'uuid' => null, 'unknown' => null], $keyPair);
     foreach ($pairs as $key => $val) {
         if ($key == 'uuid') {
             $val = str_replace("-", "", $val);
         }
         if ($val != null && $val != '') {
             $filename = $this->options['cache_folder_player'] . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $this->getCacheFileName($val) . '.json';
             if ($key == 'uuid') {
                 if (!strlen($val) == 32) {
                     continue;
                 }
                 $content = $this->getCache($filename);
                 if ($content != null) {
                     $timestamp = array_key_exists('timestamp', $content) ? $content['timestamp'] : 0;
                     if (time() - $this->getCacheTime() < $timestamp) {
                         return new Player($content, $this);
                     }
                 }
                 $response = $this->fetch('player', $key, $val);
                 if ($response['success'] == true) {
                     $PLAYER = new Player(['record' => $response['player'], 'extra' => $content['extra']], $this);
                     $PLAYER->setExtra(['filename' => $filename]);
                     $PLAYER->handleNew();
                     $this->setCache($filename, $PLAYER);
                     return $PLAYER;
                 }
             } else {
                 if ($key == 'name') {
                     if (file_exists($filename) || $this->hasPaid($val)) {
                         $uuid = $this->getUUID($val);
                         return $this->getPlayer(['uuid' => $uuid]);
                     }
                 } else {
                     if ($key == 'unknown') {
                         $this->debug('Determining type.', false);
                         $type = InputType::getType($val);
                         if ($type == InputType::USERNAME) {
                             $this->debug('Input is username, fetching UUID.', false);
                             $uuid = $this->getUUID($val);
                         } else {
                             if ($type == InputType::UUID) {
                                 $uuid = $val;
                             } else {
                                 return null;
                             }
                         }
                         return $this->getPlayer(['uuid' => $uuid]);
                     }
                 }
             }
         }
     }
     if ($this->getCacheTime() < self::MAX_CACHE_TIME) {
         $this->setCacheTime(self::MAX_CACHE_TIME);
         return $this->getPlayer($pairs);
     }
     return null;
 }