/**
  * @param int                  $regionId
  * @param string               $playerName
  * @param null|OutputInterface $output
  * @param null|Browser         $buzz
  */
 public function parsePlayer($regionId, $playerName, OutputInterface $output = null, $buzz = null)
 {
     if (null == $buzz) {
         $buzz = new Browser();
     }
     $hasOutput = null != $output;
     if ($hasOutput) {
         $output->write("Parsing game page...\t\t\t");
     }
     $response = $buzz->get('http://www.lolnexus.com/ajax/get-game-info/' . self::$serverNames[$regionId - 1] . '.json?name=' . urlencode($playerName));
     if (!preg_match('/lrf:\\/\\/spectator [0-9a-zA-Z.:]+ (.*) ([0-9]+) ([A-Z0-9]+) [0-9\\.]+/', $response->getContent(), $matches)) {
         throw new \RuntimeException('Cannot parse LoL Nexus game page, the game may be ended (sometimes LoLNexus\'s cache is bad), please retry.');
     }
     if ($hasOutput) {
         $output->writeln('<info>OK</info>');
     }
     $this->encryptionKey = $matches[1];
     $this->gameId = $matches[2];
     $this->region = $matches[3];
 }
 /**
  * @param ReplayInterface $replay
  * @param OutputInterface $output
  *
  * @return bool
  */
 public function downloadCurrentData(ReplayInterface $replay, OutputInterface $output = null)
 {
     $hasOutput = null != $output;
     $lastInfos = $this->getLastChunkInfos($replay, $replay->getLastChunkId());
     // End stats
     if ($lastInfos['endGameChunkId'] == $replay->getLastChunkId()) {
         return true;
     }
     $downloadableChunkId = $replay->getLastChunkId() + 1;
     if ($hasOutput) {
         $output->write("Downloading chunk\t#" . $downloadableChunkId . "\t\t");
     }
     $this->downloadChunk($replay, $downloadableChunkId);
     $replay->setLastChunkId($downloadableChunkId);
     if ($hasOutput) {
         $output->writeln('<info>OK</info>');
     }
     if ($lastInfos['keyFrameId'] > $replay->getLastKeyframeId()) {
         $downloadableKeyframeId = $replay->getLastKeyframeId() + 1;
         if ($hasOutput) {
             $output->write("Downloading keyframe\t#" . $downloadableKeyframeId . "\t\t");
         }
         $this->downloadKeyframe($replay, $downloadableKeyframeId, $output);
         $replay->setLastKeyframeId($downloadableKeyframeId);
         if ($hasOutput) {
             $output->writeln('<info>OK</info>');
         }
     }
     // Downloading all chunks & keyframes might slow the current chunk
     if ($lastInfos['chunkId'] > $replay->getLastChunkId() || $lastInfos['keyFrameId'] > $replay->getLastKeyframeId()) {
         return $this->downloadCurrentData($replay, $output);
     }
     // Wait for the next available chunk
     usleep(($lastInfos['nextAvailableChunk'] + 500) * 1000);
     // micro, not milli
     // Free memory to avoid memory leak
     gc_collect_cycles();
     // And again
     $this->downloadCurrentData($replay, $output);
 }