获取游戏管理的排行榜

目录

本主题介绍如何获取游戏管理的排行榜的值。

获取完整的排行榜

使用 XblLeaderboardGetLeaderboardAsync 返回给定排行榜的排行榜值。 结果按排名降序返回,从排行榜的顶部开始。 查询由游戏管理的统计数据支持的排行榜时,请务必将 queryType 字段设置为 XblLeaderboardQueryType::TitleManagedStatBackedGlobalXblLeaderboardQueryType::TitleManagedStatBackedSocial

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 the 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";
                }
            }
        }
    }
};

// XblLeaderboardQuery is set up to query for all leaderboard items here
// see example code in sections below showing how to run other queries
XblLeaderboardQuery leaderboardQuery = {}; 
pal::strcpy(leaderboardQuery.scid, sizeof(leaderboardQuery.scid), scid.c_str()) 
leaderboardQuery.statName = statName.c_str(); 
leaderboardQuery.queryType = XblLeaderboardQueryType::TitleManagedStatBackedGlobal;


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

参考

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

可通过在创建 XblLeaderboardQuery 时指定查询条件来从排行榜查询中获取筛选结果。 若要获取指定排名的排行榜值,请使用以上示例中的代码,但将创建 XblLeaderboardQuery 的行替换为以下代码:

C API

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

参考

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

若要获取指定玩家的排行榜值,请使用以上示例中的代码,但将创建 XblLeaderboardQuery 的行替换为以下代码:

C API

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

参考