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

# 動作與規則快速入門

> 使用觸發程序、條件和動作建置您的第一個 PlayStream 規則，以即時回應玩家事件，並推動自動化的 LiveOps 回應。

# 動作與規則快速入門

PlayStream 規則可讓您即時回應某一種類型 PlayStream 事件的子集。

## 需求

* [Game Manager 快速入門](/zh-TW/services/playfab/live-service-management/gamemanager/quickstart)
* [使用玩家統計資料](/zh-TW/services/playfab/community/leaderboards/tournaments-leaderboards/using-player-statistics) (包含可協助您完成本教學課程範例的資訊)。

相關術語的快速詞彙表：

* **PlayStream**：構成 PlayFab 事件管線的功能群組。
  * **PlayStream 事件**是描述有關玩家或遊戲之事件的 JSON 格式字串。
  * **PlayStream 事件**的大小上限取決於您的自動化功能層級。

* **規則**：即時回應某一種類型的 PlayStream 事件，執行額外的邏輯。
  * 由以下項目組成：一個**事件觸發程序**、選擇性的**條件**清單，以及選擇性的**動作**清單。

* **觸發程序**：啟動此規則的事件名稱。

* **條件**：規則和其他 PlayStream 功能的內容篩選條件。
  * 對 PlayStream 事件 JSON 物件執行非常輕量的評估，並略過不符合需求的事件。

* **動作**：要在適當實體上執行的情境式工作。

規則只包含一個觸發程序、選擇性的條件清單，且通常至少有一個動作 (非必要，但沒有動作的話就相當無用)。

觸發程序、條件和動作也是其他系統的一部分：[大量動作](/zh-TW/services/playfab/data-analytics/acting-data/action-rules-bulk-actions-for-an-entire-player-segment)和[錦標賽排行榜 (已淘汰)](/zh-TW/services/playfab/community/leaderboards/tournaments-leaderboards/using-resettable-statistics-and-leaderboards)。

## 範例案例：計算來自用戶端的自訂事件

在以下範例中，我們從用戶端張貼以下自訂事件。

```csharp theme={null}
public void WriteEvent() {
    PlayFabClientAPI.WritePlayerEvent(new WriteClientPlayerEventRequest {
        EventName = "ForumPostEvent",
        Body = new Dictionary<string, object> {
            { "Subject", "My First Post" },
            { "Body", "My awesome Post." }
        }
    }, LogSuccess, LogFailure);
}
```

在此案例中，用戶端會手動回報自訂的 `ForumPostEvent` (目前不對應 PlayFab 或支援的合作夥伴中的任何自動事件)

我們將使用 PlayStream 規則來計算玩家以此方式回報論壇文章的次數。

**需求**：您的遊戲必須*已經*在張貼此類型的事件，Game Manager GUI 才會允許您建立以其觸發的規則。

前往您的 **Game Manager**：

* 瀏覽至您的 **Title**。
* 選取 **Automation**。
* 前往 **Rules**。
* 選取 **New Rule**。

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/data-analytics/acting-data/tutorials/game-manager-automation-new-rule.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=bd467d6b0caea0e4a258bf1c6b2a7cd8" alt="Game Manager - automation - new rule" width="897" height="1036" data-path="images/playfab/data-analytics/acting-data/tutorials/game-manager-automation-new-rule.png" />

讓我們逐一評估此範例中的 **New Rule**：

* **`title.6195.ForumPostEvent`**
  * `6195` 是本教學課程中使用的遊戲。您的 titleId 將會與您自己的遊戲相符。
  * `ForumPostEvent` 是此範例案例程式碼中使用的事件名稱。
  * 只有當您最近在遊戲中張貼過至少一個 `ForumPostEvent` 時，才會出現此行。
* 此範例中特別使用的 **Conditions** 用途很有限。
  * 不過，它*確實*示範了當 **Body** 字典中的 **Body** 索引鍵對應至 **Invalid string** 時，如何篩選我們的自訂事件。
  * 篩選條件是選擇性的，應從您的規則中移除不相關的玩家。
* 此案例中所選的 **Actions** 會將統計資料計數器遞增一。
  * **需求**：必須在 Game Manager 的 Leaderboards 下定義 **Forum Posts** 統計資料。

在 Game Manager **PlayStream Debugger** (如下所示) 中，您可以觀察觸發程序生效的情形。

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/data-analytics/acting-data/tutorials/game-manager-playstream-debugger-event-trigger.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=7fbbc21d4c1f53db11e54c647d9610fa" alt="Game Manager - PlayStream - debugger - event trigger" width="452" height="213" data-path="images/playfab/data-analytics/acting-data/tutorials/game-manager-playstream-debugger-event-trigger.png" />

如您所見，自訂的 `ForumPostEvent` 會自動觸發我們在新規則的 **Actions** 下設定的統計資料。

## 進階：CloudScript 動作

**PlayFab** 中許多內建動作都相當簡單，可能無法提供您所需的完整功能。因此，我們允許您使用 CloudScript 完全自訂動作邏輯。

讓我們編輯 **Rule**。

* 移除 **Condition**。
* 將 **Action** 設定為 **Execute CloudScript**。
* 選取 **Save Action** 按鈕。

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/data-analytics/acting-data/tutorials/game-manager-automation-edit-rule.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=b69ba07416a1d485dc29b4e69637dfe0" alt="Game Manager - automation - edit rule" width="914" height="802" data-path="images/playfab/data-analytics/acting-data/tutorials/game-manager-automation-edit-rule.png" />

現在，如果我們張貼新的 **ForumPostEvent**，就會看到 CloudScript 執行。

<img src="https://mintcdn.com/microsoft-4404708b/IwSYTY5BEqd_x-HK/images/playfab/data-analytics/acting-data/tutorials/game-manager-playstream-debugger-cloudscript-execution.png?fit=max&auto=format&n=IwSYTY5BEqd_x-HK&q=85&s=f0cdab71d528049e20c131b846a84eb5" alt="Game Manager - PlayStream - debugger - CloudScript execution" width="462" height="167" data-path="images/playfab/data-analytics/acting-data/tutorials/game-manager-playstream-debugger-cloudscript-execution.png" />

撰寫 CloudScript 以回應 PlayStream 規則是一個進階主題，我們的教學課程中有相關說明：[搭配 PlayStream 使用 CloudScript 動作](/zh-TW/services/playfab/data-analytics/acting-data/action-rules-using-cloudscript-actions-with-playstream)。


## Related topics

- [動作與規則](/zh-TW/services/playfab/data-analytics/acting-data/action-rules-overview.md)
- [PlayFab 資料與分析文件](/zh-TW/services/playfab/data-analytics/index.md)
- [Event Sampling 快速入門](/zh-TW/services/playfab/data-analytics/export-data/event-sampling-quickstart.md)
- [使用 Azure Functions 的 PlayFab CloudScript 快速入門指南](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/quickstart.md)
- [玩家自訂屬性快速入門](/zh-TW/services/playfab/live-service-management/game-configuration/segmentation/player-custom-properties-qs.md)
