

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例：尋找跨多個玩家屬性的交集
<a name="match-examples-5"></a>

此範例說明如何使用集合規則來尋找兩個或多個玩家屬性中的交集。使用集合規則時，您可以分別針對單一屬性和多個屬性，使用 `intersection` 操作和 `reference_intersection_count` 操作。

為了說明這項方法，此範例會根據玩家的角色偏好，來評估配對中的玩家。此範例的遊戲是「自由戰」的模式，配對中的所有玩家都是對手。每個玩家都會被要求 (1) 選擇自己的角色，以及 (2) 選擇自己想要對抗的角色。我們需要規則，來確保配對中每個玩家所使用的角色，都在其他所有玩家的偏好對手清單中。

範例規則集利用下列的角色來說明配對：
+ 隊伍結構：一個隊伍 5 個玩家
+ 玩家屬性：
  + *myCharacter*：玩家所選擇的角色。
  + *preferredOpponents*：列出玩家想要對抗的角色。
+ 配對規則：如果每個使用中的角色，出現在每個玩家偏好的對手清單中，即可接受可能的配對。

為了實行配對規則，此範例使用具有下列屬性值的集合規則：
+ 操作 – 使用 `reference_intersection_count`操作來評估測量值中的每個字串清單如何與參考值中的字串清單相交。
+ 測量 – 使用 `flatten` 屬性表達式來建立字串清單，每個字串清單都包含一個玩家的 *myCharacter* 屬性值。
+ 參考值 – 使用 `set_intersection` 屬性表達式來建立所有 *preferredOpponents* 屬性值的字串清單，這些值對配對中的每個玩家都是常見的。
+ 限制 – `minCount` 設定為 1，以確保每個玩家選擇的字元 （測量中的字串清單） 至少符合所有玩家通用的其中一個偏好對手。 （參考值中的字串）。
+ 擴展 – 如果配對未在 15 秒內填滿，請放寬最小交集要求。

此規則的流程如下：

1. 將玩家加入配對候選中。會重新計算參考值 (字串清單)，以納入與新玩家偏好對手清單的交集。會重新計算衡量值 (字串清單)，以將新玩家所選擇的角色新增為字串清單。

1. Amazon GameLift Servers 會確認衡量值 (玩家所選擇的角色) 中的每個字串清單，是否與參考值 (玩家偏好的對手) 中至少一個字串有交集。在此範例中，由於衡量值中的每個字串清單中只包含一個值，因此交集為 0 或 1。

1. 如果衡量值中的任何字串清單並未和參考值的字串清單有交集，則不符此規則，會從候選配對中將此一新玩家移除。

1. 如果未能在 15 秒內完成配對，請放寬對手的配對標準，以補滿配對中剩下的玩家空位。

```
{
    "name": "preferred_characters",
    "ruleLanguageVersion": "1.0",

    "playerAttributes": [{
        "name": "myCharacter",
        "type": "string_list"
    }, {
        "name": "preferredOpponents",
        "type": "string_list"
    }],

    "teams": [{
        "name": "red",
        "minPlayers": 5,
        "maxPlayers": 5
    }],

    "rules": [{
        "description": "Make sure that all players in the match are using a character that is on all other players' preferred opponents list.",
        "name": "OpponentMatch",
        "type": "collection",
        "operation": "reference_intersection_count",
        "measurements": ["flatten(teams[*].players.attributes[myCharacter])"],
        "referenceValue": "set_intersection(flatten(teams[*].players.attributes[preferredOpponents]))",
        "minCount":1
    }],
    "expansions": [{
        "target": "rules[OpponentMatch].minCount",
        "steps": [{
            "waitTimeSeconds": 15,
            "value": 0
        }]
    }]
}
```