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

# 排行榜快速入门

> PlayFab 排行榜快速入门：在 Game Manager 中创建首个排行榜，并使用你的 TitleId 和开发者密钥配置 C# SDK。

# 排行榜快速入门

在本指南中，我们将了解如何为排行榜服务设置开发环境。我们还将学习如何从我们的网站 [Game Manager](https://developer.playfab.com) 快速创建一个排行榜。

## 先决条件

我们需要一个 PlayFab 帐户才能使用 PlayFab 排行榜服务。有关创建帐户的说明，请参见[身份验证](https://docs.xbox.com/zh-CN/services/playfab/identity/player-identity/authentication.md)

## 创建排行榜

在左侧菜单中选择 Progression。

![PlayFab 排行榜主菜单](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/game-manager-main-menu.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=ed7c9cadb094234b43bfc6cc7fa5ac26)

我们现在在上部菜单中选择 Leaderboards 选项卡。

![PlayFab 排行榜 Game Manager](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/leaderboards-game-manager.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=7b4805585c64e031676a642f7d9ea99e)

然后我们点击“New Leaderboard”按钮并创建我们的排行榜定义。

![PlayFab 排行榜定义](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/leaderboard-definition.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=e91f750f9d4dedcf3639d236c91ad0c4)

在这里我们可以配置排行榜的每个方面。在此处了解可用参数以及如何创建排行榜的更多信息：

* [创建基本排行榜](https://docs.xbox.com/zh-CN/services/playfab/community/leaderboards/create-basic-leaderboard.md)。
* [API 参考](https://docs.xbox.com/zh-CN/services/playfab/community/leaderboards/api-reference.md)

最终结果应该如下所示。

![PlayFab 排行榜用法](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/leaderboard-menu.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=4330a9b1cf43c651dddcc63730517f40)

## 设置环境

在这里我们将学习如何设置开发者环境以使用 C# SDK，但此处所介绍的概念也可以与其他 SDK 或普通 HTTP 请求一起使用。

## 设置 TitleId 和 DeveloperSecretKey

作为标题实体（Title Entity）进行身份验证时，需要 DeveloperSecretKey。

```C# theme={null}
PlayFabSettings.staticSettings.TitleId = ""; // Change this value to your own titleId from PlayFab Game Manager
PlayFabSettings.staticSettings.DeveloperSecretKey = ""; // Change this to your title's secret key from Game Manager
```

## 创建标题并获取密钥

* 登录到 [https://developer.playfab.com/](https://developer.playfab.com/)
* 创建一个标题
  * 在设置下的 API Features 部分找到 Title ID。
* 生成密钥：
  * 点击标题视图左上方标题名称旁边的齿轮图标
  * Title Settings
  * Secret Keys
  * New secret key

以下是在 Game Manager 中找到 Secret Key 部分时的 UI 外观。

![PlayFab 排行榜密钥](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/secret-keys.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=bf4c2dbebc795a0fd49915fb5b64a067)

## 以标题身份登录

对于写入操作，我们建议从游戏服务器使用 TitleEntity，此方法将返回将在我们发出的所有请求中使用的 `AuthenticationContext`。

```C# theme={null}
public static async Task<PlayFabAuthenticationContext> LoginAsTitleEntity()
{
    GetEntityTokenRequest request = new GetEntityTokenRequest()
    {
        Entity = new PlayFab.AuthenticationModels.EntityKey()
        {
            Id = PlayFabSettings.staticSettings.TitleId,
            Type = "title",
        },                
    };

    PlayFabResult<GetEntityTokenResponse> entityTokenResult = await PlayFabAuthenticationAPI.GetEntityTokenAsync(request);

    PlayFabAuthenticationContext authContext = new PlayFabAuthenticationContext
    {
        EntityToken = entityTokenResult.Result.EntityToken
    };
    
    return authContext;
}
```

## 以玩家身份登录（创建玩家）

此方法基于一个标识符创建玩家，该标识符返回类型为 `title_player_account` 的实体。更多信息请见：
[实体快速入门](https://docs.xbox.com/zh-CN/services/playfab/live-service-management/game-configuration/entities/quickstart.md)

```C# theme={null}
private static async Task<PlayFabAuthenticationContext> LoginAsPlayer(string customId = "GettingStartedGuide")
{
    LoginWithCustomIDRequest request = new LoginWithCustomIDRequest { CustomId = customId, CreateAccount = true };

    PlayFabResult<LoginResult> loginResult = await PlayFabClientAPI.LoginWithCustomIDAsync(request);

    return loginResult.Result.AuthenticationContext;
}
```

## 为实体设置 display name 属性

为了让 `DisplayName` 属性作为 "Get Leaderboards APIs" 响应的一部分可用，我们需要对创建的每个实体执行以下代码，以便将实体映射到游戏的自定义显示名称。

```C# theme={null}
private static async Task UpdateEntityDisplayName(PlayFabAuthenticationContext context, string customId)
{
    SetDisplayNameRequest request = new SetDisplayNameRequest()
    {
        AuthenticationContext = context,
        DisplayName = customId,
        Entity = new PlayFab.ProfilesModels.EntityKey()
        {
            Id = context.EntityId,
            Type = context.EntityType,
        },
    };

    PlayFabResult<SetDisplayNameResponse> updateNameResult = await PlayFabProfilesAPI.SetDisplayNameAsync(request);
}
```

## 另请参阅

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


## Related topics

- [锦标赛与排行榜快速入门](https://docs.xbox.com/zh-CN/services/playfab/community/leaderboards/tournaments-leaderboards/quickstart.md)
- [PlayFab 排行榜概述](https://docs.xbox.com/zh-CN/services/playfab/community/leaderboards/index.md)
- [PlayFab 社区文档](https://docs.xbox.com/zh-CN/services/playfab/community/index.md)
- [创建基础排行榜](https://docs.xbox.com/zh-CN/services/playfab/community/leaderboards/create-basic-leaderboard.md)
- [推送通知快速入门](https://docs.xbox.com/zh-CN/services/playfab/live-service-management/game-configuration/title-communications/push-notifications/quickstart.md)
