今天我們來了解一下 Game Frontend 這個須由我們實作的部件,在 Open-Match 所設計的流程中,玩家用戶端發送配對請求後,首先接收到配對請求的便是 Game Frontend。此部件內通常會實作使用者認證與獲取使用者個人資料,用以確認可進行配對,與利用個人資料組成配對參數,最後產生配對請求 Tickets 給 Open-Match 核心。
一個配對中的最小單位,代表一次配對請求,裡面含有配對所需要的條件參數,以下為 Ticket proto 定義,其中較為重要的為 SearchFields 與 Assignment。
type Ticket struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Id represents an auto-generated Id issued by Open Match.
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// An Assignment represents a game server assignment associated with a Ticket,
// or whatever finalized matched state means for your use case.
// Open Match does not require or inspect any fields on Assignment.
Assignment *Assignment `protobuf:"bytes,3,opt,name=assignment,proto3" json:"assignment,omitempty"`
// Search fields are the fields which Open Match is aware of, and can be used
// when specifying filters.
SearchFields *SearchFields `protobuf:"bytes,4,opt,name=search_fields,json=searchFields,proto3" json:"search_fields,omitempty"`
// Customized information not inspected by Open Match, to be used by the match
// making function, evaluator, and components making calls to Open Match.
// Optional, depending on the requirements of the connected systems.
Extensions map[string]*any.Any `protobuf:"bytes,5,rep,name=extensions,proto3" json:"extensions,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
// Create time is the time the Ticket was created. It is populated by Open
// Match at the time of Ticket creation.
CreateTime *timestamp.Timestamp `protobuf:"bytes,6,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty"`
}
我們可以利用 SearchFields 滿足 Ticket 所需要的配對參數,SearchFields 裡面包含了兩個 map,分別用來對應字串型別與數字型別參數,如果你原有的配對資料結構為巢狀型,需先進行扁平化後平鋪至 SearchFields 中。
type SearchFields struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Float arguments. Filterable on ranges.
DoubleArgs map[string]float64 `protobuf:"bytes,1,rep,name=double_args,json=doubleArgs,proto3" json:"double_args,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"`
// String arguments. Filterable on equality.
StringArgs map[string]string `protobuf:"bytes,2,rep,name=string_args,json=stringArgs,proto3" json:"string_args,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
// Filterable on presence or absence of given value.
Tags []string `protobuf:"bytes,3,rep,name=tags,proto3" json:"tags,omitempty"`
}
example
此例來說包含了兩個數值型條件,等級(level.lv)與連勝場數(win.streak),一個字串型條件,階級(level.rank),最後有個 "3v3" tag,這些條件最終會被送至 Opne-Match 核心進行媒合。
SearchFields: &pb.SearchFields{
DoubleArgs: map[string]float64{"level.lv": 500, "win.streak": 2},
StringArgs: map[string]string{"level.rank": "master"},
Tags: []string{"3v3"},
}
Ticket 被建立時並不會包含 Assignment 內容,而是在完成配對後,才由 Director 將配對的 game server 位置等資訊放入
type Assignment struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Connection information for this Assignment.
Connection string `protobuf:"bytes,1,opt,name=connection,proto3" json:"connection,omitempty"`
// Customized information not inspected by Open Match, to be used by the match
// making function, evaluator, and components making calls to Open Match.
// Optional, depending on the requirements of the connected systems.
Extensions map[string]*any.Any `protobuf:"bytes,4,rep,name=extensions,proto3" json:"extensions,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
}
在階段 "Assigning Players" 的過程,將配對至相同局數的 ticket 與配對的 game server connection 指派回 Assignment
Assignment: &pb.Assignment{
Connection: fmt.Sprintf("%d.%d.%d.%d:2222", rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256)),
}