@param string $region
@param string $gameId
@param int    $chunkId

@return string

@throws ReplayChunkNotFoundException
489240
public function getGameDataChunkContent($region, $gameId, $chunkId)
    {
        return file_get_contents($this->getGameDataChunkPath($region, $gameId, $chunkId));
    }
1124072
public static ByteBuffer getNextChunk(byte[] schemaBytes, ByteBuffer buf,
                                          CachedByteBufferAllocator resultBufferAllocator) {
        buf.position(buf.position() + 4);//skip partition id
        int length = schemaBytes.length + buf.remaining();

        ByteBuffer outputBuffer = resultBufferAllocator.allocate(length);
        outputBuffer.put(schemaBytes);
        outputBuffer.put(buf);
        outputBuffer.flip();

        return outputBuffer;
    }
743375
public function getKeyframePath($region, $gameId, $keyframeId)
    {
        $filePath = $this->getReplayDirPath($region, $gameId) . '/keyframes/' . $keyframeId;

        if (!is_file($filePath)) {
            throw new ReplayKeyframeNotFoundException('The keyframe #' . $keyframeId . ' is not found');
        }

        return $filePath;
    }
1076431
protected function getChunkContent($filepath, $seek_point, $chunk_size) {
        $handle = fopen($filepath, 'r');
        fseek($handle, $seek_point);
        $chunk = fread($handle, $chunk_size);

        fclose($handle);
        $handle = null;
        return $chunk;
    }
1231318
public function scroll(
        $scrollId,
        $scrollDuration = '5m'
    ) {
        $results = $this->getClient()->scroll(['scroll_id' => $scrollId, 'scroll' => $scrollDuration]);

        return $results;
    }
79636
protected function doDownload($region, $gameId, $encryptionKey, OutputInterface $output = null)
    {
        $hasOutput = null != $output;
        $replay = $this->createReplay($region, $gameId, $encryptionKey);

        // Download metas
        $this->downloadMetas($replay, $output);

        // Validate game criterias based on metas
        $this->isValid($replay, $output);

        // Retrieve last infos to download previous files
        if ($hasOutput) {
            $output->write("Retrieve last infos...\t\t\t");
        }

        $lastChunkInfo = $this->getLastChunkInfos($replay);
        $replay->setLastChunkId($lastChunkInfo['chunkId']);
        $replay->setLastKeyframeId($lastChunkInfo['keyFrameId']);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        // Download previous chunks
        if ($hasOutput) {
            $output->write("Download all previous chunks (" . $replay->getLastChunkId() . ")...\t");
        }

        $this->downloadChunks($replay);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        // Download previous keyframes
        if ($hasOutput) {
            $output->write("Download all previous keyframes (" . $replay->getLastKeyframeId() . ")...\t");
        }

        $this->downloadKeyframes($replay);

        if ($hasOutput) {
            $output->writeln(array('<info>OK</info>', ''));
        }

        // Download current chunks & keyframes
        if ($hasOutput) {
            $output->writeln("Download current game data :");
        }

        $this->downloadCurrentData($replay, $output);

        if ($hasOutput) {
            $output->writeln('');
        }

        // Update metas
        if ($hasOutput) {
            $output->write("Update metas...\t\t\t\t");
        }

        $this->updateMetas($replay);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        return $replay;
    }
177511
private function downloadKeyframe(ReplayInterface $replay, $keyframeId)
    {
        $keyframe = $this->client->downloadKeyframe($replay->getRegion(), $replay->getGameId(), $keyframeId);

        if (false === $keyframe) {
            try {
                if (
                    $keyframeId > $this->findKeyframeByChunkId(
                        $replay->getMetas(),
                        $this->findFirstChunkId($replay->getMetas()
                    ))
                    && isset($replay->getMetas()['pendingAvailableKeyFrameInfo'][0])
                ) {
                    $replay->addDownloadRetry();

                    if ($this->options['replay.download.retry'] == $replay->getDownloadRetry()) {
                        return false;
                    } else {
                        sleep(1);

                        return $this->downloadChunk($replay, $keyframeId);
                    }
                }
            } catch (\RuntimeException $e) {
                // No first chunk id was created, so it's the first download : we do nothing
            }

            // Clear retries
            $replay->resetDownloadRetry();

            return false;
        }

        $pathFolder = $this->getReplayDirPath($replay->getRegion(), $replay->getGameId()) . '/keyframes';
        file_put_contents($pathFolder . '/' . $keyframeId, $keyframe);

        // Update metas
        $metas = $replay->getMetas();
        $metas['pendingAvailableKeyFrameInfo'][] = array(
            'id'           => $keyframeId,
            'receivedTime' => date('M n, Y g:i:s A'),
            'nextChunkId'  => ($keyframeId - 1) * 2 + $metas['startGameChunkId'],
        );

        $replay->setMetas($metas);

        if ($this->options['replay.decoder.enable']) {
            $crypt = new ReplayCrypt($replay);

            if (
                !$this->onReplayFileDecrypted(
                    $replay,
                    self::FILETYPE_KEYFRAME,
                    $keyframeId,
                    $crypt->getBinary($replay, $pathFolder, $keyframeId, $this->options['replay.decoder.save_files'])
                )
            ) {
                return false;
            }
        }

        return true;
    }
1136196
public final void sendSentinel(long txnId, int partitionId) {
        final long initiatorHSId = m_cartographer.getHSIdForSinglePartitionMaster(partitionId);
        sendSentinel(txnId, initiatorHSId, -1, -1, true);
    }
1287138
protected function log($gameId, $message)
    {
        if (null != $this->logger) {
            $this->logger->info('Game #' . $gameId . ': ' . $message);
        }
    }
1040119
@Override
    public boolean replayCompletion(String globalId) {
        final DistributableTransaction tx = getTransactionForID(globalId);

        if (tx != null) {
            tx.replayCompletion();
            return true;
        }

        return false;

    }
656209
public function getGameMetasData($region, $gameId, $token, $clientIp)
    {
        // Setting cache for chunk init
        $cacheName = $this->getCacheName($gameId, $clientIp);
        $this->cache->set($cacheName, 0, 86400); // 1 day

        return $this->observerClient->getMetas($region, $gameId);
    }
318796
public function getLastChunkInfo($region, $gameId, $currentChunkId, &$keyframeId = null)
    {
        $metas = $this->getMetas($region, $gameId);

        // Game loading information
        //$firstChunkId = $replayManager->findFirstChunkId($metas);
        $firstChunkId = $metas['firstChunkId'];

        // A bug appears when endStartupChunkId = 3 and startGameChunkId = 5, the game won't load
        if ($metas['endStartupChunkId'] + 2 == $firstChunkId) {
            $firstChunkId = $metas['startGameChunkId'] + 2;
        }

        $keyframeId = $this->findKeyframeByChunkId($metas, $firstChunkId);

        $lastChunkInfo = array(
            'chunkId'            => $firstChunkId,
            'availableSince'     => 30000,
            'nextAvailableChunk' => 30000,
            'keyFrameId'         => $keyframeId,
            'nextChunkId'        => $firstChunkId,
            'endStartupChunkId'  => $metas['endStartupChunkId'],
            'startGameChunkId'   => $metas['startGameChunkId'],
            'endGameChunkId'     => 0,
            'duration'           => 30000
        );

        // In game information
        if ($firstChunkId != $metas['startGameChunkId'] && $currentChunkId - 1 == $metas['startGameChunkId']) {
            $currentChunkId = $firstChunkId;
        }

        if ($currentChunkId > $metas['startGameChunkId']) {
            if ($currentChunkId > $metas['lastChunkId']) {
                $currentChunkId = $metas['lastChunkId'];
            } elseif ($currentChunkId < $metas['firstChunkId']) {
                $currentChunkId = $metas['firstChunkId'];
            }

            $keyframeId = $this->findKeyframeByChunkId($metas, $currentChunkId);

            $lastChunkInfo['chunkId']            = $currentChunkId;
            $lastChunkInfo['nextChunkId']        = $metas['lastChunkId'];
            $lastChunkInfo['keyFrameId']         = $keyframeId;
            $lastChunkInfo['nextAvailableChunk'] = $currentChunkId == $firstChunkId + 6 ? 30000 : 100; // wait for full loading
        }

        // End game, stop downloading
        if ($currentChunkId == $metas['lastChunkId']) {
            $lastChunkInfo['nextAvailableChunk'] = 90000;
            $lastChunkInfo['endGameChunkId']     = $metas['endGameChunkId'];
        }

        return $lastChunkInfo;
    }
807869
public function getBinary(ReplayInterface $replay, $pathFolder, $fileId, $saveFile = false)
    {
        $path = $pathFolder . '/' . $fileId;
        if (!is_file($path)) {
            return false;
        }

        $decodedKey = base64_decode($replay->getEncryptionKey());
        $decodedKey = $this->decrypt($replay->getGameId(), $decodedKey);

        $keyframeBinary = file_get_contents($path);
        $decodedKeyframeBinary = gzdecode($this->decrypt($decodedKey, $keyframeBinary));

        // File should saved only in development environment.
        // If parsing is needed, you have to do it in the ReplayDownloader::onReplayFileDecrypted() method
        if ($saveFile) {
            file_put_contents($pathFolder . '.decoded/' . $fileId, $decodedKeyframeBinary);
        }

        return $decodedKeyframeBinary;
    }
168751
public function recentGame($gameId)
	{
		if ( ! isset($this->info['recentGames']))
		{
			// no rune pages
			return null;
		}
		$recentGames = $this->info['recentGames'];

		return $recentGames->game($gameId);
	}
1197250
private ByteBuffer getChunk(final int chunkIdx) throws IOException, InterruptedException {
        ByteBuffer chunk = chunkCache[chunkIdx];
        if (chunk == null) {
            final ByteBuffer byteBufferDup = zipFileSlice.physicalZipFile.getByteBuffer(chunkIdx).duplicate();
            chunk = chunkCache[chunkIdx] = byteBufferDup;
        }
        return chunk;
    }
222325
public function getKeyframePath($region, $gameId, $keyframeId)
    {
        $keyframePath = null;

        try {
            $keyframePath = $this->observerClient->getKeyframePath($region, $gameId, $keyframeId);
        } catch (ReplayKeyframeNotFoundException $e) {
            // Log
            $this->log($gameId, sprintf('GET /getKeyFrame/%s/%s/%d/token | ERROR: the keyframe is not found', $region, $gameId, $keyframeId));

            throw $e;
        }

        // Log
        $this->log($gameId, sprintf('GET /getKeyFrame/%s/%s/%d/token', $region, $gameId, $keyframeId));

        return $keyframePath;
    }
1109463
public function getNumberOfPlayers($gameId = null)
    {
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping;
        $rsm->addScalarResult('c', 'c');
        if ($gameId !== null) {
            $game = $this->getServiceManager()
                ->get('playgroundgame_lottery_service')
                ->getGameMapper()
                ->findById($gameId);

            if ($game && $game->getAnonymousAllowed()) {
                $query = $em->createNativeQuery('
                    SELECT COUNT(distinct e.anonymous_identifier) AS c
                    FROM game_entry e
                    WHERE e.game_id = '.((int) $gameId).'
                ', $rsm);
            } else {
                $query = $em->createNativeQuery('
                    SELECT COUNT(distinct e.user_id) AS c
                    FROM game_entry e
                    WHERE e.game_id = '.((int) $gameId).'
                ', $rsm);
            }
        } else {
            $query = $em->createNativeQuery('
                    SELECT COUNT(distinct e.user_id) AS c
                    FROM game_entry e
                ', $rsm);
        }
        
        $count = $query->getSingleScalarResult();
        return $count;
    }