基于事件的排行榜的示例代码

本主题提供基本排行榜显示的示例代码、获取指定的结果页、跳到排行榜中的指定玩家,以及获取排行榜的下一页。

本主题涵盖以下内容:

获取排行榜前列的用户

使用 XblLeaderboardGetLeaderboardAsync 返回特定排行榜的排行榜值,从排行榜最上方开始。 查询基于事件的用户统计信息支持的排行榜时,请确保将 queryType 字段设置为 XblLeaderboardQueryType::UserStatBacked

基本排行榜显示(平面 C API)

平面 C API

auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblLeaderboardGetLeaderboardResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        leaderboardBuffer.resize(resultSize);
        XblLeaderboardResult* leaderboard{};

        hr = XblLeaderboardGetLeaderboardResult(asyncBlock, resultSize, leaderboardBuffer.data(), &leaderboard, nullptr);

        if (SUCCEEDED(hr))
        {
            // Use XblLeaderboardResult in result.
            for (auto row = 0u; row < leaderboard->rowsCount; ++row)
            {
                std::stringstream rowText;
                rowText << leaderboard->rows[row].xboxUserId << "\t";

                for (auto column = 0u; column < leaderboard->rows[row].columnValuesCount; ++column)
                {
                    rowText << leaderboard->rows[row].columnValues[column] << "\t";
                }
            }
        }
    }
};

// Instantiate and configure the leaderboard query.
XblLeaderboardQuery leaderboardQuery = {}; 
pal::strcpy(leaderboardQuery.scid, sizeof(leaderboardQuery.scid), scid.c_str()) 
leaderboardQuery.leaderboardName = leaderboardName.c_str(); 
leaderboardQuery.queryType = XblLeaderboardQueryType::UserStatBacked;
// For more leaderboard query options, replace the 4-line code block 
// that precedes this comment as indicated in other examples.

HRESULT hr = XblLeaderboardGetLeaderboardAsync(
    xboxLiveContext,
    leaderboardQuery,
    asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}

有关详细信息,请参阅以下内容:

获取排行榜上指定排名周围的用户

将上一个基本排行榜查询(平面 C API)示例中实例化和配置排行榜查询的 4 行代码块替换为以下代码块,以从排行榜查询中获取指定的结果页。

获取结果的指定页面 (C)

平面 C API

XblLeaderboardQuery leaderboardQuery = {};
pal::strcpy(leaderboardQuery.scid, sizeof(leaderboardQuery.scid), scid.c_str());
leaderboardQuery.leaderboardName = leaderboardName.c_str();
leaderboardQuery.skipResultToRank = 100;
leaderboardQuery.maxItems = 100;
leaderboardQuery.queryType = XblLeaderboardQueryType::UserStatBacked;

有关详细信息,请参阅 XblLeaderboardQuery

获取排行榜上指定玩家周围的用户

将上一个基本排行榜查询(平面 C API)示例中实例化和配置排行榜查询的 4 行代码块替换为以下代码块,以获取有关指定播放器的排行榜。

平面 C API

XblLeaderboardQuery leaderboardQuery = {};
pal::strcpy(leaderboardQuery.scid, sizeof(leaderboardQuery.scid), scid.c_str());
leaderboardQuery.leaderboardName = leaderboardName.c_str();
leaderboardQuery.skipToXboxUserId = xboxUserId;
leaderboardQuery.maxItems = 100;
leaderboardQuery.queryType = XblLeaderboardQueryType::UserStatBacked;

有关详细信息,请参阅 XblLeaderboardQuery

获取排行榜的下一页

若要获取上一排行榜结果的下一页,请调用 XblLeaderboardResultGetNextAsync,如以下示例代码所示。

平面 C API

auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblLeaderboardResultGetNextResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        std::vector<char> buffer(resultSize, 0);
        XblLeaderboardResult* result{};

        hr = XblLeaderboardResultGetNextResult(asyncBlock, resultSize, buffer.data(), &result, nullptr);
        // Use result to read the leaderboard results.
    }
};

HRESULT hr = XblLeaderboardResultGetNextAsync(
    xboxLiveContext,
    leaderboardResult,
    maxItems,
    asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}

有关详细信息,请参阅以下内容: