Diarkis MatchMaker と Diarkis Room を使って検索する実装例を紹介します。
マッチメイキングでの検索方法
どのマッチメイキング定義で検索するかを選択する必要があります。複数のマッチメイキング定義名を渡すことで、複数のマッチメイキング定義を検索することができます。検索の順序は、search 関数に渡されたマッチメイキング定義の配列の順序と同じになります。
NOTE: この処理は HTTP, TCP, UDP/RUDP のどこにでも実装することが可能です。
/**
* muse import the following:
*
* github.com/Diarkis/diarkis/matching
* github.com/Diarkis/diarkis/room
* github.com/Diarkis/diarkis/util
*
*/
matchingNames := []string{ "levelAndRank" }
// Search conditions
conds := make(map[string]int)
conds["level"] = 14
conds["rank"] = 3
// How many search results you require
howmany := 10
matching.Search(matchingNames, conds, howmany, func(err error, results []interface{}) {
if err != nil {
// Handle error
return
}
// util.Waterfall executes a list of functions in order
operations := make([]func(error), len(results))
for i := range results {
operations[i] = tryToJion
}
utill.Waterfall(operations, done)
index := 0
// We try the rooms we found
func tryToJoin(next func(error)) {
if index == len(results) {
// We are tried all rooms we found, but none worked
// try matching.Search again
done(nil)
return
}
roomID := results[index]["roomID"].(string)
room.Join(roomID, userData, ver, cmd, func(err error, members []string, owner string, createdTime int64, props map[string]interface{}) {
if err != nil {
// this room was no good, remove from MatchMaker
for _, matchingName := range {
// we assume roomID is used as a unique ID with matching.Addd
matching.Remove(matchingName, roomID)
}
// We have failed to join the room - try the next room
index++
next(nil)
return
}
// We have joined the room successfully - skip the rest of the rooms
done(nil)
})
}
// This function will be called when everything is finished
func done(err error) {
if err != nil {
// Handle error
return
}
logger.Debug("We are done!")
}
})