# Cloud Code による Economy Game Overrides

> Learn how to use Game Overrides with Cloud Code to personalize your in-game Economy for specific player groups.

[Game Overrides](/game-overrides.md) を使用して、ゲーム内の Economy をパーソナライズすることができます。例えば、ごく短期間のシーズンセールを提供したり、熟練プレイヤーの購入のゲーム内報酬を増やしたりします。

## Game Overrides と設定のキャッシュの Economy での機能##how-game-overrides-and-config-caching-work-with-economy

設定割り当てハッシュが Cloud Code と Economy SDK でどのように使用されるかについては、[設定のキャッシュ](./config-caching.md) を参照してください。

Economy が、プレイヤーにどの Override が適用されるかを認識するには、どの Override がクライアントデバイスにあるかを把握する必要があります。Economy は、設定の一部として `configAssignmentHash` を返します。`configAssignmentHash` を Economy に渡すと、Economy がデバイスにあるのと同じ設定を使用し、そのプレイヤーに対して一貫したユーザー体験を提供できるようになります。

Economy SDK 2.0.0 以降は、Economy API とやり取りする際に、`configAssignmentHash` を自動的に Economy に渡します。Economy SDK で Game Overrides を使用する方法については、Economy SDK ガイドの [Game Overrides](./sdk/game-overrides.md) を参照してください。

Economy SDK 1.0.0 および Cloud Code 内の Economy SDK は、`configAssignmentHash` を自動的に Economy に渡すことはありません。Economy が `configAssignmentHash` をパラメーターとして受け取らない場合でも、クライアントデバイス設定との一貫性を維持する方法があります。ただし、一部の珍しいケースでは、この設定がクライアントデバイス上の設定との一貫性を持たない場合があります。

## Cloud Code での Economy Game Overrides の使用##using-economy-game-overrides-with-cloud-code

Cloud Code を使用してゲームロジックをクライアントデバイスから切り離すもう 1 つの方法は、Cloud Code を使用してゲーム内経済を操作することです。Cloud Code から Economy を操作するときは、`configAssignmentHash` を Cloud Code スクリプトに渡してから、それを Economy に渡す必要があります。SDK には、クライアントデバイス上の `configAssignmentHash` を取得するメソッドがあります。すべてのステップは以下のとおりです。

### Cloud Code の設定##cloud-code-setup

1. `configAssignmentHash` をパラメーターとして [Cloud Code スクリプトを設定](/cloud-code.md#use-cloud-code-with-modules-and-scripts) します。


   **Frame:**
   ![パラメーターが追加された Add a Script (スクリプトを追加する) ダイアログのスクリーンショット。](/api/media?file=/economy/media/images/config-assignment-hash.jpg)

2. [Economy SDK 2.2](https://cloud-code-sdk-documentation.cloud.unity3d.com/economy/v2.2) 以降を使用して、Economy を呼び出すスクリプトを Cloud Code に追加します。

   ```js title="JavaScript"
   /*
    *  -------- Example Cloud Code Script --------
    *
    *  Ensure consistency when using Game Overrides with Economy
    *
    * --------------------------------------------
    */
   const { CurrenciesApi } = require("@unity-services/economy-2.2");

   /*
    * CommonJS wrapper for the script. It receives a single argument, which can be destructured into:
    *  - params: Object containing the parameters provided to the script, accessible as object properties
    *  - context: Object containing the projectId, environmentId, environmentName, playerId and accessToken properties.
    *  - logger: Logging client for the script. Provides debug(), info(), warning() and error() log levels.
    */
   module.exports = async ({ params, context, logger }) => {
     // Log an info message with the parameters provided to the script and the invocation context
     logger.info("Script parameters: " + JSON.stringify(params));
     logger.info("Authenticated within the following context: " + JSON.stringify(context));

     const { projectId, playerId, accessToken } = context;
     const { currencyId, configAssignmentHash } = params;

     const currencies = new CurrenciesApi({ accessToken });
     const increment = await currencies.incrementPlayerCurrencyBalance({ projectId, playerId, currencyId, configAssignmentHash, currencyModifyBalanceRequest: { currencyId, amount: 10 } });

     logger.info("Increment data result: " + JSON.stringify(increment.data));

     // Return the JSON result to the client
     return {
       newBalance: increment.data.balance,
     };
   };
   ```

### エディターの設定##editor-setup

1. Economy SDK 2.0.0 以降から [設定を取得](./sdk/configuration.md) します。

   ```cs title="C#"
   string currencyID = "GOLD";
   CurrencyDefinition goldCurrencyDefinition = EconomyService.Instance.Configuration.GetCurrency(currencyID);
   ```

2. 以下を呼び出します。

   ```cs title="C#"
   EconomyService.Instance.Configuration.GetConfigAssignmentHash();
   ```

   (Economy SDK から)。

3. ステップ 2 の `configAssignmentHash` を [Cloud Code](/cloud-code/modules/how-to-guides/unity-services-integration.md) に渡します。

   ```cs title="C#"
   await CloudCode.CallEndpointAsync<NewBalance>("AddCurrency", new IncrementBalanceParam(currencyId, configAssignmentHash));
   ```

### 全体例##full-example

```cs title="C#"
public async Task<int> IncrementCurrency()
{
   var goldCurrencyId = "GOLD";
   EconomyService.Instance.Configuration.GetCurrency(goldCurrencyId);
    var configAssignemntHash = EconomyService.Instance.Configuration.GetConfigAssignmentHash();
    var newBalance = await CallIncrementCurrencyEndpoint(goldCurrencyId, configAssignemntHash);
     return newBalance;
}

public async Task<int> CallIncrementCurrencyEndpoint(string currencyId, string configAssignmentHash)
{
   var newBalanceResult = await CloudCode.CallEndpointAsync<NewBalance>(
                    "AddCurrency", new IncrementBalanceParam(currencyId, configAssignmentHash));

   return newBalanceResult.newBalance;
}

public class NewBalance
{
   public int newBalance;
}

[Serializable]
public class IncrementBalanceParam
{
    public string currencyId;
   public string configAssignmentHash;

   public IncrementBalanceParam(string currencyId, string configAssignmentHash)
   {
       this.currencyId = currencyId;
       this.configAssignmentHash = configAssignmentHash;
   }

   public override string ToString()
   {
      return $"{{\"currencyId\": @currencyId, \"configAssignmentHash\": @configAssignmentHash }}";
   }
}
```

### 注意事項##pitfalls

[設定のキャッシュの注意事項](./config-caching.md#pitfalls) を参照
