var(errInternal=runtime.NewError("internal server error",13))funcInitModule(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,initializerruntime.Initializer)error{initializer.RegisterBeforeRt("MatchmakerAdd",func(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,in*rtapi.Envelope)(*rtapi.Envelope,error){message,ok:=in.Message.(*rtapi.Envelope_MatchmakerAdd)if!ok{returnnil,errInternal}// If the string properties contains a region value of "europe", modify it to "europe-west"
ifvalue,ok:=message.MatchmakerAdd.StringProperties["region"];ok&&value=="europe"{message.MatchmakerAdd.StringProperties["region"]="europe-west"}returnin,nil})returnnil}
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionInitModule(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,initializer: nkruntime.Initializer){initializer.registerRtBefore("MatchmakerAdd",beforeMatchmakerAdd)}constbeforeMatchmakerAdd : nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeMatchmakerAdd>=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,envelope: nkruntime.EnvelopeMatchmakerAdd):nkruntime.EnvelopeMatchmakerAdd|void{constregion=envelope.matchmakerAdd.stringProperties["region"];// If the string properties contain a region value of "europe", modify it to "europe-west"
if(region&®ion=="europe"){envelope.matchmakerAdd.stringProperties["region"]="europe-west";}returnenvelope;}
Server
1
2
3
4
5
6
7
8
9
10
nk.register_rt_before(function(context,payload)localregion=payload.matchmaker_add.string_properties["region"]-- If the string properties contain a region value of "europe", modify it to "europe-west"ifregion=="europe"thenpayload.matchmaker_add.string_properties["region"]="europe-west"endreturnpayloadend,"MatchmakerAdd")
var(errInternal=runtime.NewError("internal server error",13))funcInitModule(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,initializerruntime.Initializer)error{initializer.RegisterBeforeRt("MatchmakerAdd",func(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,in*rtapi.Envelope)(*rtapi.Envelope,error){message,ok:=in.Message.(*rtapi.Envelope_MatchmakerAdd)if!ok{returnnil,errInternal}// Force min count to be 4 and max count to be 8
message.MatchmakerAdd.MinCount=4message.MatchmakerAdd.MaxCount=8returnin,nil})returnnil}
Server
1
2
3
4
5
6
7
8
9
10
11
functionInitModule(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,initializer: nkruntime.Initializer){initializer.registerRtBefore("MatchmakerAdd",beforeMatchmakerAdd)}constbeforeMatchmakerAdd : nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeMatchmakerAdd>=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,envelope: nkruntime.EnvelopeMatchmakerAdd):nkruntime.EnvelopeMatchmakerAdd|void{// Force min count to be 4 and max count to be 8
envelope.matchmakerAdd.minCount=4envelope.matchmakerAdd.maxCount=8returnenvelope;}
Server
1
2
3
4
5
6
7
nk.register_rt_before(function(context,payload)-- Force min count to be 4 and max count to be 8payload.matchmaker_add.min_count=4payload.matchmaker_add.max_count=8returnpayloadend,"MatchmakerAdd")
var(errInternal=runtime.NewError("internal server error",13))funcInitModule(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,initializerruntime.Initializer)error{initializer.RegisterBeforeRt("MatchmakerAdd",func(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,in*rtapi.Envelope)(*rtapi.Envelope,error){message,ok:=in.Message.(*rtapi.Envelope_MatchmakerAdd)if!ok{returnnil,errInternal}// Force the count multiple to be in multiples of 5
message.MatchmakerAdd.CountMultiple=&wrapperspb.Int32Value{Value:5}returnin,nil})returnnil}
Server
1
2
3
4
5
6
7
8
9
10
functionInitModule(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,initializer: nkruntime.Initializer){initializer.registerRtBefore("MatchmakerAdd",beforeMatchmakerAdd)}constbeforeMatchmakerAdd : nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeMatchmakerAdd>=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,envelope: nkruntime.EnvelopeMatchmakerAdd):nkruntime.EnvelopeMatchmakerAdd|void{// Force the count multiple to be in multiples of 5
envelope.matchmakerAdd.countMultiple=5;returnenvelope;}
Server
1
2
3
4
5
6
nk.register_rt_before(function(context,payload)-- Force the count multiple to be in multiples of 5payload.matchmaker_add.count_multiple=5returnpayloadend,"MatchmakerAdd")
Code snippet for this language cURL has not been found. Please choose another language to show equivalent examples.
Code snippet for this language REST has not been found. Please choose another language to show equivalent examples.
在将用户添加到匹配程序时,还可以使用 before 挂钩以权威方式控制查询:
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var(errInternal=runtime.NewError("internal server error",13))funcInitModule(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,initializerruntime.Initializer)error{initializer.RegisterBeforeRt("MatchmakerAdd",func(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,in*rtapi.Envelope)(*rtapi.Envelope,error){message,ok:=in.Message.(*rtapi.Envelope_MatchmakerAdd)if!ok{returnnil,errInternal}// Force the matchmaking request to use the * query
message.MatchmakerAdd.Query="*"returnin,nil})returnnil}
Server
1
2
3
4
5
6
7
8
9
10
functionInitModule(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,initializer: nkruntime.Initializer){initializer.registerRtBefore("MatchmakerAdd",beforeMatchmakerAdd)}constbeforeMatchmakerAdd : nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeMatchmakerAdd>=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,envelope: nkruntime.EnvelopeMatchmakerAdd):nkruntime.EnvelopeMatchmakerAdd|void{// Force the matchmaking request to use the * query
envelope.matchmakerAdd.query="*";returnenvelope;}
Server
1
2
3
4
5
6
nk.register_rt_before(function(context,payload)-- Force the matchmaking request to use the * querypayload.matchmaker_add.query="*"returnpayloadend,"MatchmakerAdd")
letquery="+properties.region:europe +properties.rank:5";constminCount=2;constmaxCount=4;conststringProperties={region:"europe"};constnumericProperties={rank:8};varticket=awaitsocket.addMatchmaker(query,minCount,maxCount,stringProperties,numericProperties);// ... if no match is found within a certain time, request a new ticket with looser criteria
query="+properties.region:europe +properties.rank:>=3 +properties.rank:<=7";varnewTicket=awaitsocket.addMatchmaker(query,minCount,maxCount,stringProperties,numericProperties);
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
varquery="+properties.region:europe +properties.rank:5";varstringProperties=newDictionary<string,string>(){{"region","europe"}};varnumericProperties=newDictionary<string,int>(){{"rank",8}};varmatchmakerTicket=awaitsocket.AddMatchmakerAsync(query,2,4,stringProperties,numericProperties);// ... if no match is found within a certain time, request a new ticket with looser criteriaquery="+properties.region:europe +properties.rank:>=3 +properties.rank:<=7";varnewMatchmakerTicket=awaitsocket.AddMatchmakerAsync(query,2,4,stringProperties,numericProperties);
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
autosuccessCallback=[](constNMatchmakerTicket&ticket){std::cout<<"Matchmaker ticket: "<<ticket.ticket<<std::endl;};int32_tminCount=2;int32_tmaxCount=4;stringquery="+properties.region:europe +properties.rank:5";NStringMapstringProperties;NStringDoubleMapnumericProperties;stringProperties.emplace("region","europe");numericProperties.emplace("rank",8.0);rtClient->addMatchmaker(minCount,maxCount,query,stringProperties,numericProperties,successCallback);// ... if no match is found within a certain time, request a new ticket with looser criteria
query="+properties.region:europe +properties.rank:>=3 +properties.rank:<=7";rtClient->addMatchmaker(minCount,maxCount,query,stringProperties,numericProperties,successCallback);
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Stringquery="+properties.region:europe +properties.rank:5";intminCount=2;intmaxCount=4;Map<String,String>stringProperties=newHashMap<String,String>(){{put("region","europe");}};Map<String,Double>numericProperties=newHashMap<String,Double>(){{put("rank",8.0);}};MatchmakerTicketmatchmakerTicket=socket.addMatchmaker(query,minCount,maxCount,stringProperties,numericProperties).get();// ... if no match is found within a certain time, request a new ticket with looser criteriaquery="+properties.region:europe +properties.rank:>=3 +properties.rank:<=7";MatchmakerTicketnewMatchmakerTicket=socket.addMatchmaker(query,minCount,maxCount,stringProperties,numericProperties).get();
func_ready():# First, setup the socket as explained in the authentication section.socket.connect("received_matchmaker_matched",self,"_on_matchmaker_matched")func_on_matchmaker_matched(p_matched:NakamaRTAPI.MatchmakerMatched):print("Received MatchmakerMatched message: %s"%[p_matched])print("Matched opponents: %s"%[p_matched.users])
var(errUnableToCreateMatch=runtime.NewError("unable to create match",13))funcInitModule(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,initializerruntime.Initializer)error{iferr:=initializer.RegisterMatchmakerMatched(func(ctxcontext.Context,loggerruntime.Logger,db*sql.DB,nkruntime.NakamaModule,entries[]runtime.MatchmakerEntry)(string,error){matchId,err:=nk.MatchCreate(ctx,"lobby",map[string]interface{}{"invited":entries})iferr!=nil{return"",errUnableToCreateMatch}returnmatchId,nil});err!=nil{logger.Error("unable to register matchmaker matched hook: %v",err)returnerr}returnnil}
// Register the matchmaker matched handler (the party leader and party members should all do this)
socket.onmatchmakermatched=(matched)=>{socket.joinMatch(null,matched.token);};// Create a party as the party leader
constparty=awaitsocket.createParty(true,2);// Accept any incoming party requests
socket.onpartyjoinrequest=(request)=>{request.presences.forEach(presence=>{awaitsocket.acceptPartyMember(request.party_id,presence);});};// As the leader of the party, add the entire party to the matchmaker
constticket=awaitsocket.addMatchmakerParty(party.party_id,"*",3,4,null,null);
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Register the matchmaker matched handler (the party leader and party members should all do this)socket.ReceivedMatchmakerMatched+=asyncmatched=>awaitsocket.JoinMatchAsync(matched);// Create a party as the party leadervarparty=awaitsocket.CreatePartyAsync(true,2);// Accept any incoming party requestssocket.ReceivedPartyJoinRequest+=asyncrequest=>{foreach(varpresenceinrequest.Presences){awaitsocket.AcceptPartyMemberAsync(request.PartyId,presence);}};// As the leader of the party, add the entire party to the matchmakervarticket=awaitsocket.AddMatchmakerPartyAsync(party.Id,"*",3,4);
-- Register the matchmaker matched handler (the party leader and party members should all do this)socket.on_matchmaker_matched(function(matched)pprint("Received:",matched);socket.match_join(matched.token)end)-- Accept any incoming party requestssocket.on_party_join_request(function(request)fori,presenceinipairs(request.presences)dosocket.party_accept(request.party_id,presence)endend)-- Create a party as the party leaderlocalopen=truelocalmax_players=2localparty=socket.party_create(open,max_players)-- As the leader of the party, add the entire party to the matchmakerlocalmin_players=3localmax_players=4localquery="*"localticket=socket.party_matchmaker_add(party.party_id,min_players,max_players,query)
Code snippet for this language C++/Unreal/Cocos2d-x has not been found. Please choose another language to show equivalent examples.
Code snippet for this language Java/Android has not been found. Please choose another language to show equivalent examples.
Code snippet for this language cURL has not been found. Please choose another language to show equivalent examples.
Code snippet for this language REST has not been found. Please choose another language to show equivalent examples.