> ## 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.

# Economy v2 中具有固定內容的容器

> 結合虛擬貨幣代幣與在購買時解包固定獎勵的套件組合，在 Economy v2 中重新建立舊版 PlayFab 容器項目。

<Info>
  Economy v2 現已正式推出。如需支援和提供意見反應，請前往 [PlayFab 論壇](https://community.playfab.com)。
</Info>

Economy v1 (舊版) 內建 **Containers** 功能，也就是特殊的目錄項目，開啟時會將其內容授與至玩家的庫存。Economy v2 未將容器納入為原生項目類型，但您可以結合使用虛擬貨幣和套件組合，針對**固定內容容器** (一律授與相同項目的容器) 達成相同的行為。

## 運作方式

此模式使用三個 Economy v2 建置組塊：

1. **一種虛擬貨幣 (或可堆疊項目)**，代表容器本身，例如 "Treasure Chest Token"。
2. **一個套件組合**，價格為 1 單位該貨幣，其中包含容器應授與的項目。
3. **一個購買呼叫**，玩家在其中花費 1 個代幣購買套件組合，套件組合會自動解包並授與項目。

從玩家的角度來看，他們會收到一個代幣 (透過遊戲獎勵、商店購買等方式)，然後「開啟」它以取得其中的項目，這與 V1 容器的運作方式完全相同。

## 先決條件

* 一個 [PlayFab 開發人員帳戶](https://developer.playfab.com/en-us/sign-up)。
* 在 [Game Manager](https://developer.playfab.com/) 中建立的遊戲。
* 至少一個您希望容器授與的已發佈目錄項目。

## 步驟 1：為容器建立虛擬貨幣

建立代表容器的虛擬貨幣。此貨幣的每個單位都相當於一個未開啟的容器。

### Game Manager

1. 前往 **Economy** > **Catalog (V2)** > **Currency**。
2. 選取 **New currency**。
3. 將 **Title** 設定為描述性名稱，例如 `Treasure Chest`。
4. 將 **FriendlyId** 設定為簡短代碼，例如 `tc`。
5. 選取 **Save and publish**。

### REST API

呼叫 [CreateDraftItem](https://learn.microsoft.com/en-us/rest/api/playfab/economy/catalog/create-draft-item)：

```json theme={null}
{
  "Item": {
    "CreatorEntity": {
      "Id": "{{TitleID}}",
      "Type": "title"
    },
    "Type": "currency",
    "AlternateIds": [
      {
        "Type": "FriendlyId",
        "Value": "tc"
      }
    ],
    "Title": {
      "NEUTRAL": "Treasure Chest"
    },
    "Description": {
      "NEUTRAL": "A treasure chest that can be opened to reveal its contents."
    },
    "StartDate": "2023-07-27T00:00:00.000Z"
  },
  "Publish": true
}
```

***

## 步驟 2：建立價格為一個容器代幣的套件組合

建立一個包含您希望容器授與之項目的套件組合。將套件組合的價格設定為剛好一個單位的容器貨幣。

### Game Manager

1. 前往 **Economy** > **Catalog (V2)** > **Bundles**。
2. 選取 **New bundle**。
3. 將 **Title** 設定為描述容器內容的名稱，例如 `Treasure Chest Contents`。
4. 在 **Items** 下，新增您希望容器授與的項目和數量，例如 1 個 **Legendary Sword** 和 100 **Gold**。
5. 在 **Price** 下，將價格設定為 **1** 個容器貨幣 (**Treasure Chest**)。
6. 選取 **Save and publish**。

### REST API

呼叫 [CreateDraftItem](https://learn.microsoft.com/en-us/rest/api/playfab/economy/catalog/create-draft-item)：

```json theme={null}
{
  "Item": {
    "Type": "bundle",
    "Title": {
      "NEUTRAL": "Treasure Chest Contents"
    },
    "StartDate": "2023-07-27T00:00:00.000Z",
    "ItemReferences": [
      {
        "Id": "{{LegendarySwordID}}",
        "Amount": 1
      },
      {
        "Id": "{{GoldCurrencyID}}",
        "Amount": 100
      }
    ],
    "PriceOptions": {
      "Prices": [
        {
          "Amounts": [
            {
              "ItemId": "{{TreasureChestCurrencyID}}",
              "Amount": 1
            }
          ]
        }
      ]
    }
  },
  "Publish": true
}
```

***

當玩家購買此套件組合時，套件組合會[自動解包](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/bundles#purchasing-and-unpacking-bundles)，個別項目會直接進入玩家的庫存。

## 步驟 3：將容器代幣授與玩家

透過任何符合您遊戲設計的機制，將容器貨幣授與玩家：

* **遊戲獎勵**：使用 [AddInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/add-inventory-items)，在完成關卡、任務或成就後授與代幣。
* **商店購買**：在[商店](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/stores)中以另一種虛擬貨幣或真實貨幣販售容器代幣。
* **戰鬥通行證 / 季節獎勵**：將代幣納入為獎勵層級。

使用 `AddInventoryItems` 授與三個 Treasure Chest 代幣的範例：

```json theme={null}
{
  "Entity": {
    "Type": "title_player_account",
    "Id": "{{PlayerID}}"
  },
  "Item": {
    "Id": "{{TreasureChestCurrencyID}}"
  },
  "Amount": 3
}
```

## 步驟 4：開啟容器

當玩家想要開啟容器時，請呼叫 [PurchaseInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/purchase-inventory-items)，以一個容器代幣購買套件組合：

```json theme={null}
{
  "Entity": {
    "Type": "title_player_account",
    "Id": "{{PlayerID}}"
  },
  "Item": {
    "Id": "{{TreasureChestContentsBundleID}}"
  },
  "Amount": 1,
  "PriceAmounts": [
    {
      "ItemId": "{{TreasureChestCurrencyID}}",
      "Amount": 1
    }
  ]
}
```

成功呼叫後，玩家的庫存會隨之更新：扣除一個 Treasure Chest 代幣，並新增套件組合的內容 (一個 Legendary Sword + 100 Gold)。

## 多種容器類型

重複此模式即可建立多種容器類型。例如：

| 容器 | 貨幣 (FriendlyId) | 套件組合內容 |
| - | - | - |
| Treasure Chest | `tc` | 一個 Legendary Sword + 100 Gold |
| Supply Crate | `sc` | 五個 Health Potion + 3 個 Mana Potion |
| Premium Box | `pb` | 一個 Rare Mount + 500 Gold + 50 Gems |

每種容器類型都有自己的貨幣和套件組合。

## 使用商店進行容器購買

使用[商店](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/stores)覆寫容器套件組合的價格。透過使用商店，您可以在不同情境中以不同價格提供相同的容器 (例如，在活動期間提供折扣的「開啟」成本)：

```json theme={null}
{
  "Item": {
    "Type": "store",
    "Title": {
      "NEUTRAL": "Event Store"
    },
    "ItemReferences": [
      {
        "Id": "{{TreasureChestContentsBundleID}}",
        "Amount": 1,
        "PriceOptions": {
          "Prices": [
            {
              "Amounts": [
                {
                  "ItemId": "{{TreasureChestCurrencyID}}",
                  "Amount": 1
                }
              ]
            }
          ]
        }
      }
    ]
  },
  "Publish": true
}
```

然後在購買時傳遞 `StoreId`：

```json theme={null}
{
  "Entity": {
    "Type": "title_player_account",
    "Id": "{{PlayerID}}"
  },
  "Item": {
    "Id": "{{TreasureChestContentsBundleID}}"
  },
  "Amount": 1,
  "PriceAmounts": [
    {
      "ItemId": "{{TreasureChestCurrencyID}}",
      "Amount": 1
    }
  ],
  "StoreId": "{{EventStoreID}}"
}
```

## 限制

此模式適用於具有**固定、確定性內容**的容器。如果您需要**隨機戰利品** (每次開啟容器時內容都不同，類似於搭配掉落表的 V1 容器)，請參閱[掉落表和隨機戰利品](/zh-TW/services/playfab/economy-monetization/economy-v2/tutorials/drop-tables-and-randomized-loot)教學課程。該教學課程使用 [Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview) 來：

1. 從玩家的庫存中減去容器代幣。
2. 執行您的加權隨機選取邏輯。
3. 透過庫存 API 授與產生的項目。

## 另請參閱

* [Economy v2 套件組合](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/bundles)
* [Economy v2 商店](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/stores)
* [虛擬貨幣](/zh-TW/services/playfab/economy-monetization/economy-v2/tutorials/currencies)
* [Economy v2 概觀](/zh-TW/services/playfab/economy-monetization/economy-v2/overview)
* [PurchaseInventoryItems API 參考](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/purchase-inventory-items)
* [AddInventoryItems API 參考](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/add-inventory-items)


## Related topics

- [Economy v2 中的掉落表和隨機戰利品](/zh-TW/services/playfab/economy-monetization/economy-v2/tutorials/drop-tables-and-randomized-loot.md)
- [Economy v2 概觀](/zh-TW/services/playfab/economy-monetization/economy-v2/overview.md)
- [Economy 第 2 版 (V2) 常見問題集](/zh-TW/services/playfab/economy-monetization/economy-v2/faq.md)
- [Economy v2 商店](/zh-TW/services/playfab/economy-monetization/economy-v2/catalog/stores.md)
- [ILampInfo::GetFixedColor 方法](/zh-TW/reference/lighting/lamparray/interfaces/ilampinfo/methods/ilampinfo_getfixedcolor.md)
