> ## 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 中建立 HighScore 統計資料與排行榜，然後使用 C# 從用戶端提交玩家分數。

本快速入門說明如何建立可追蹤玩家最高分的統計資料，以及如何取得最高分排行榜。這可用於全域排行榜，或搭配[可重設的統計資料](https://docs.xbox.com/zh-TW/services/playfab/community/leaderboards/tournaments-leaderboards/using-resettable-statistics-and-leaderboards.md)針對特定活動或錦標賽進行重設。

## 必要條件

您的玩家已登入 PlayFab。

## 步驟 1 - 建立統計資料與相關聯的排行榜

在 Game Manager 中：

* 前往左側功能表中的 **Leaderboards**。
* 選取 **NEW LEADERBOARD**。
* 在 **Statistic name** 欄位中新增名為 **HighScore** 的 **Leaderboard**。
* 使用提供的下拉式功能表，將 **Reset frequency** 欄位設定為 **Manually**。
* 移至 **Aggregation method** 欄位，並從提供的下拉式功能表中選取 **Maximum** (一律使用最高值)。

## 步驟 2 - 以玩家的最高分更新統計資料

在從用戶端使用 [UpdatePlayerStatistics](xref:titleid.playfabapi.com.client.playerdatamanagement.updateplayerstatistics) 之前，我們必須在 **API Features** 中啟用它。

1. 從遊戲的 Settings 功能表中選取 **Title settings**。
2. 選取 **API Features** 索引標籤。
3. 核取 **Allow client to post player statistics** 方塊。
   注意：一般而言，不應在上線的遊戲中使用此選項，因為它會讓用戶端擁有所提交值的權限。這只適用於不擔心玩家可能竄改統計資料作弊的情況。如果統計資料必須安全，則只應透過伺服器授權的作業 (例如 Cloud Script 或自訂遊戲伺服器) 來更新。
4. 選取畫面底部的 **SAVE**。

![Game Manager - 設定 - API 功能 - 允許用戶端張貼玩家統計資料](https://mintcdn.com/microsoft-4404708b/5IpvKlT-jmAaVkeY/images/playfab/community/leaderboards/tournaments-leaderboards/tutorials/api-features-allow-client-to-post-player-statistics.png?fit=max\&auto=format\&n=5IpvKlT-jmAaVkeY\&q=85\&s=71197d724d092da6b5ff0a4093ad843e)

### C# 程式碼範例 - SubmitScore

在此程式碼範例中，我們會有一個在遊戲結束時呼叫的 `SubmitScore` 函式。

```csharp theme={null}

public void SubmitScore(int playerScore) {
    PlayFabClientAPI.UpdatePlayerStatistics(new UpdatePlayerStatisticsRequest {
        Statistics = new List<StatisticUpdate> {
            new StatisticUpdate {
                StatisticName = "HighScore",
                Value = playerScore
            }
        }
    }, result=> OnStatisticsUpdated(result), FailureCallback);
}

private void OnStatisticsUpdated(UpdatePlayerStatisticsResult updateResult) {
    Debug.Log("Successfully submitted high score");
}

private void FailureCallback(PlayFabError error){
    Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
    Debug.LogError(error.GenerateErrorReport());
}
```

### 步驟 3 - 要求最高分的排行榜

若要取得所有玩過遊戲之玩家的最高分排行榜，您需要呼叫 [GetLeaderboard](xref:titleid.playfabapi.com.client.playerdatamanagement.getleaderboard)。

### C# 程式碼範例 - RequestLeaderboard

在此程式碼範例中，我們會有一個用來取得排行榜的 `RequestLeaderboard` 函式，它會將結果傳遞給 `DisplayLeaderboard` 函式，由該函式填入遊戲中顯示最高分的體驗。

```csharp theme={null}
//Get the players with the top 10 high scores in the game
public void RequestLeaderboard() {
    PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest {
            StatisticName = "HighScore",
            StartPosition = 0,
            MaxResultsCount = 10
    }, result=> DisplayLeaderboard(result), FailureCallback);
}

private void FailureCallback(PlayFabError error){
    Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
    Debug.LogError(error.GenerateErrorReport());
}
```


## Related topics

- [排行榜快速入門](https://docs.xbox.com/zh-TW/services/playfab/community/leaderboards/quickstart-leaderboards.md)
- [錦標賽與排行榜](https://docs.xbox.com/zh-TW/services/playfab/community/leaderboards/tournaments-leaderboards/index.md)
- [推播通知快速入門](https://docs.xbox.com/zh-TW/services/playfab/live-service-management/game-configuration/title-communications/push-notifications/quickstart.md)
- [動作與規則快速入門](https://docs.xbox.com/zh-TW/services/playfab/data-analytics/acting-data/action-rules-quickstart.md)
- [PlayFab Leaderboards 概觀](https://docs.xbox.com/zh-TW/services/playfab/community/leaderboards/index.md)
