# Cloud Code C# モジュールによる Leaderboards へのアクセス

> Refer to code samples that access Leaderboards with Cloud Code C# Modules.

Cloud Code を使用し [Cloud Code C# モジュール](/cloud-code/modules/overview.md) を介することで、Leaderboards 機能にアクセスできます。

プレイヤーが Cloud Code モジュールを呼び出す例について考えてみましょう。以下のコードスニペットは、プレイヤーのスコアを送信し、そのエントリー (ランクなど) を取得し、さらにリーダーボードの上位スコアを取得します。

C#:

```cs
using Microsoft.Extensions.DependencyInjection;
using Unity.Services.CloudCode.Apis;
using Unity.Services.CloudCode.Core;
using Unity.Services.Leaderboards.Model;

public class LeaderboardsExample
{
    [CloudCodeFunction("LeaderboardsExample")]
    public async Task<LeaderboardResult> LeaderboardsExampleAsync(
        IExecutionContext context,
        IGameApiClient apiClient,
        string leaderboardId,
        string playerId,
        int score)
    {
        string accessToken = context.AccessToken;
        Guid projectId = Guid.Parse(context.ProjectId);

        var addScoreResult = await apiClient.Leaderboards.AddLeaderboardPlayerScoreAsync(
            context, accessToken, projectId, leaderboardId, playerId, new LeaderboardScore(score));
        var getScoreResult = await apiClient.Leaderboards.GetLeaderboardPlayerScoreAsync(
            context, accessToken, projectId, leaderboardId, playerId);
        var getScoresResult = await apiClient.Leaderboards.GetLeaderboardScoresAsync(
            context, accessToken, projectId, leaderboardId);

        return new LeaderboardResult()
        {
            AddScoreResult = addScoreResult.StatusCode.ToString(),
            GetScoreResult = getScoreResult.Data,
            GetScoresResult = getScoresResult.Data
        };
    }
}

public class LeaderboardResult
{
    public string AddScoreResult { get; set; }
    public LeaderboardEntryWithUpdatedTime GetScoreResult { get; set; }
    public LeaderboardScoresPage GetScoresResult { get; set; }
}

public class ModuleConfig : ICloudCodeSetup
{
    public void Setup(ICloudCodeConfig config)
    {
        config.Dependencies.AddSingleton(GameApiClient.Create());
    }
}
```

プレイヤースコープのエンドポイントへの管理レベルのアクセスも Cloud Code を介して利用可能です。これにより、複数のプレイヤーのスコアや、Cloud Code モジュールを呼び出したプレイヤー以外のプレイヤーのスコアを更新できます。これを行うには、アクセストークンの代わりに、サービストークンを Cloud Code モジュールに渡します。

C#:

```cs
public class LeaderboardsExample
{
    [CloudCodeFunction("LeaderboardsExample")]
    public async Task<LeaderboardResult> LeaderboardsExampleAsync(
        IExecutionContext context,
        IGameApiClient apiClient,
        string leaderboardId,
        string playerId,
        int score)
    {
        string serviceToken = context.ServiceToken;
        Guid projectId = Guid.Parse(context.ProjectId);

        var addScoreResult = await apiClient.Leaderboards.AddLeaderboardPlayerScoreAsync(
            context, serviceToken, projectId, leaderboardId, playerId, new LeaderboardScore(score));
        var getScoreResult = await apiClient.Leaderboards.GetLeaderboardPlayerScoreAsync(
            context, serviceToken, projectId, leaderboardId, playerId);
        var getScoresResult = await apiClient.Leaderboards.GetLeaderboardScoresAsync(
            context, serviceToken, projectId, leaderboardId);

        return new LeaderboardResult()
        {
            AddScoreResult = addScoreResult.StatusCode.ToString(),
            GetScoreResult = getScoreResult.Data,
            GetScoresResult = getScoresResult.Data
        };
    }
}
```

## 追加リソース##additional-resources

* [Unity Dashboard](https://dashboard.unity3d.com/)
* [Cloud Code ドキュメント](/cloud-code.md)
* [Unity Authentication](/authentication/.md)
