// You MUST import matching package in HTTP server and other serversimport"github.com/Diarkis/diarkis/matching"funcmain() {// You MUST call this to enable matching package (MatchMaker) matching.Setup("absolute path to a configuration file or an empty string")// other things to do here diarkis.Start()}
// Match making item property to be searched byprops :=make(map[string]int)props["level"] =30props["rank"] =7// Unique name of the match-making definition to add the room tomatchingName :="levelAndRank"// roomMetadata will be retrieved as the search results.data :=make(map[string]interface{})data["roomID"] = roomIDdata["roomType"] =2data["roomName"] ="Room of level 30 and rank 7"// TTL of the room in match-making. TTL is in secondsttl :=int64(30)// relayLimit is the number of server nodes to send the data to at a time:// Greater the value, the faster and more CPU intense it isrelayLimit :=2matching.Add(matchingName, roomID, props, roomMetadata, ttl, relayLimit)
// match-making definition name to remove items frommatchingName :="levelAndRank"// Unique ID list of items to be removedidlist := []string{ roomIDA, roomIDB }// relayLimit is the number of server nodes to send the data to at a time:// Greater the value, the faster and more CPU intense it isrelayLimit :=2matching.Remove(matchingName, idlist, relayLimit)
/*** muse import the following:** github.com/Diarkis/diarkis/matching* github.com/Diarkis/diarkis/room* github.com/Diarkis/diarkis/util**/matchingNames := []string{ "levelAndRank" }// Search conditionsconds :=make(map[string]int)conds["level"] =14conds["rank"] =3// How many search results you requirehowmany :=10matching.Search(matchingNames, conds, howmany, func(err error, results []interface{}) {if err !=nil {// Handle errorreturn }// 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 foundfunctryToJoin(next func(error)) {if index ==len(results) {// We are tried all rooms we found, but none worked// try matching.Search againdone(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 MatchMakerfor _, 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 roomsdone(nil) }) }// This function will be called when everything is finishedfuncdone(err error) {if err !=nil {// Handle errorreturn } logger.Debug("We are done!") }})