> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 向排行榜添加上下文数据

> 为每个 PlayFab 排行榜行附加自定义元数据，以便与玩家分数和排名一起跟踪比赛上下文、设备详细信息或反作弊信号。

在本教程中，我们将解释如何在更新排行榜时添加上下文信息。游戏工作室可能希望添加有关玩家获得分数的上下文的一些详细信息，或者游戏中可能有与该分数相关的功能。

现在我们将从之前的示例（一款街机游戏）切换到一款竞争激烈的射击游戏。在此示例中，游戏取得了巨大成功，但也面临一个挑战。有太多关于人们使用外部组件“作弊”游戏的抱怨，这些组件可以消除武器后坐力并改善瞄准辅助。因此，我们现在的任务是跟踪所有已使用组件的更多信息以及排名比赛的网络信息。

## 创建排行榜

为演示如何处理这种场景，我们采用[排行榜的更多用法](/services/playfab/community/leaderboards/doing-more-with-leaderboards)页面的排行榜定义。基于此定义，我们将看看如何为添加到排行榜的每一行添加元数据。

```C# theme={null}
public static async Task CreateLeaderboardDefinitionAsync(PlayFabAuthenticationContext context, string leaderboardName)
{
    PlayFabProgressionInstanceAPI leaderboardsAPI = new PlayFabProgressionInstanceAPI(context);
    CreateLeaderboardDefinitionRequest leaderboardDefinitionRequest = new CreateLeaderboardDefinitionRequest()
    {
        AuthenticationContext = context,
        Name = leaderboardName,
        SizeLimit = 1000,
        EntityType = "title_player_account",
        VersionConfiguration = new VersionConfiguration()
        {
            MaxQueryableVersions = 1,
            ResetInterval = ResetInterval.Manual,
        },
        Columns = new List<LeaderboardColumn>()
        {
            new LeaderboardColumn()
            {
                Name = "Eliminations",
                SortDirection = LeaderboardSortDirection.Descending,
            },
            new LeaderboardColumn()
            {
                Name = "Assists",
                SortDirection = LeaderboardSortDirection.Descending,
            }
            new LeaderboardColumn()
            {
                Name = "Deaths",
                SortDirection = LeaderboardSortDirection.Ascending,
            }         
        }
    };

    PlayFabResult<PlayFab.LeaderboardsModels.EmptyResponse> createLbDefinitionResult = await leaderboardsAPI.CreateLeaderboardDefinitionAsync(leaderboardDefinitionRequest);
}
```

### 向每一行添加元数据

我们想知道玩家是否有任何影响游戏玩法的未经授权的硬件，或任何其他可以增强玩家表现的恶意软件。反作弊系统能够获取所有这些信息，但由于玩家数量巨大，我们将专注于对高分加上外部组件的元数据进行标记检查。

要执行此操作，我们将使用 UpdateLeaderboard API。这里我们可以查看 SDK 示例：

```C# theme={null}
public static async Task UpdateLeaderboardForPlayer(PlayFabAuthenticationContext context, string leaderboardName, string entityId, int score)
{
    PlayFabProgressionInstanceAPI leaderboardsAPI = new PlayFabProgressionInstanceAPI(context);
    UpdateLeaderboardEntriesRequest updateLeaderboardRequest = new UpdateLeaderboardEntriesRequest()
    {
        Entries = new List<LeaderboardEntryUpdate>()
        {
            new LeaderboardEntryUpdate()
            {
                EntityId = entityId,
                Scores = new List<string> { score.ToString(), (score + 1).ToString(), (score + 2).ToString() },
                Metadata = "AntiCheat Data",
            }
        },
        AuthenticationContext = context,
        LeaderboardName = leaderboardName,
    };

    PlayFabResult<PlayFab.LeaderboardsModels.EmptyResponse> updateResult = await leaderboardsAPI.UpdateLeaderboardEntriesAsync(updateLeaderboardRequest);
}
```

在这里，我们使用 `LeaderboardEntryUpdate` 中的 `Metadata` 参数添加对开发者有意义的信息。在这种情况下，我们用它来存储反作弊数据。通过此示例，我们现在能够跟踪来自顶级玩家的所有相关信息，使游戏管理员能够更好地决定要封禁谁。

## 结论

在本教程中，我们学习了如何执行以下操作：

* 创建多列排行榜。
* 向排行榜的每一行添加元数据。

## 另请参阅

* [排行榜的更多用法](/services/playfab/community/leaderboards/doing-more-with-leaderboards)。
* [创建基本排行榜](/services/playfab/community/leaderboards/create-basic-leaderboard)。
* [限制 ](/services/playfab/community/leaderboards/limits-leaderboards)。
* [配额 ](/services/playfab/community/leaderboards/quota-leaderboards)。
* [组排行榜](/services/playfab/community/leaderboards/group-leaderboards)。
* [手动分级](/services/playfab/community/leaderboards/manual-tiers)。
* [按统计信息对玩家排名](/services/playfab/community/leaderboards/leaderboards-linked-to-stats)
* [向排行榜添加上下文数据](/services/playfab/community/leaderboards/metadata-leaderboards)
* [API 参考](/services/playfab/community/leaderboards/api-reference)
* [排行榜与 CloudScript](/services/playfab/community/leaderboards/leaderboards-cloudscript)。
* [排行榜与 PlayStream](/services/playfab/community/leaderboards/leaderboards-with-playstream-and-telemetry)


## Related topics

- [组排行榜](/zh-CN/services/playfab/community/leaderboards/group-leaderboards.md)
- [API 排行榜参考](/zh-CN/services/playfab/community/leaderboards/api-reference.md)
- [使用 Azure Functions 的排行榜](/zh-CN/services/playfab/community/leaderboards/leaderboards-cloudscript.md)
- [创建基础排行榜](/zh-CN/services/playfab/community/leaderboards/create-basic-leaderboard.md)
- [季节性排行榜](/zh-CN/services/playfab/community/leaderboards/seasonal-leaderboards.md)
