label 대결 필드를 사용하여 결과를 나열하는 방법에는 일치하는 대결 필터링과 쿼리라는 두 가지가 있습니다.
예를 들어, skill=100-150 레이블이 있는 대결을 나열하려는 경우 일치하는 대결 필터링은 다음과 같습니다:
Server
1
2
3
4
5
6
7
8
9
10
11
12
localnk=require("nakama")locallimit=10localisAuthoritative=truelocallabel="skill=100-150"localmin_size=0localmax_size=4localmatches=nk.match_list(limit,isAuthoritative,label,min_size,max_size)for_,matchinipairs(matches)donk.logger_info(string.format("Match id %s",match.match_id))end
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
limit:=10isAuthoritative:=truelabel:="skill=100-150"min_size:=0max_size:=4ifmatches,err:=nk.MatchList(ctx,limit,isAuthoritative,label,min_size,max_size,"");err!=nil{logger.WithField("err",err).Error("Match list error.")}else{for_,match:=rangematches{logger.Info("Match id %s",match.GetMatchId())}}
Server
1
2
3
4
5
6
7
8
9
10
11
12
functiongetMatchListings(context: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama){constlimit=10constisAuthoritative=true;constlabel="skill=100-150";constminSize=0;constmaxSize=4;constmatches=nk.matchList(limit,isAuthoritative,label,minSize,maxSize,"");matches.forEach(function(match){logger.info("Match id '%s'",match.matchId);});}
여기에서는 레이블의 문자열 값을 기반으로 필터링하므로 결과에는 일치하는skill=100-150 레이블이 있는 대결만 포함됩니다. skill=100 또는 skill=150 레이블이 있는 대결은 반환되지 않습니다.
반환된 결과는 정확히 플레이어가 찾고 있는 것일 수 있지만 일치하는 대결을 전혀 찾지 못하면 좌절할 수 있습니다.
대안은 쿼리를 사용하여 원하는 기준에 따라 필터링된 사용 가능한 대결을 나열하는 것입니다. 이 경우 skill의 범위는 다음과 같습니다:
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
localnk=require("nakama")locallimit=10localisauthoritative=truelocallabel=""localmin_size=0localmax_size=4localquery="+label.skill>=100 +label.skill<=150"localmatches=nk.match_list(limit,isauthoritative,label,min_size,max_size,query)for_,matchinipairs(matches)donk.logger_info(string.format("Match id %s",match.match_id))end
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
limit:=10authoritative:=truelabel:=""minSize:=0maxSize:=4query:="+label.skill>=100 +label.skill<=150"matches,err:=nk.MatchList(ctx,limit,authoritative,label,minSize,maxSize,query)iferr!=nil{logger.WithField("err",err).Error("Match listings error.")return}for_,match:=rangematches{logger.Info("Match id %s",match.MatchId)}
Server
1
2
3
4
5
6
7
8
9
10
11
12
functionfindMatch(context: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama){constlimit=10constisAuthoritative=true;constminSize=0;constmaxSize=4;constquery="+label.skill>=100 +label.skill<=150";varmatches=nk.matchList(limit,isAuthoritative,minSize,maxSize,query);matches.forEach(function(match){logger.info("Match id '%s'",match.matchId);});}
이 방법을 사용하면 결과에 100에서 150 사이의 skill 레이블과 일치하는 모든 대결이 포함됩니다.
functionfindOrCreateMatch(context: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama):string{varmatches=nk.matchList(limit,isAuthoritative,label,minSize,maxSize,"");// If matches exist, sort by match size and return the largest.
if(matches.length>0){matches.sort(function(a,b){returna.size>=b.size?1:-1;});returnmatches[0].matchId;}// If no matches exist, create a new one using the "lobby" module and return it's ID.
varmatchId=nk.matchCreate('supermatch',{});returnJSON.stringify({matchId});}
query:="+label.skill>=100 +label.skill<=150"matches,err:=nk.MatchList(ctx,1,true,"",2,4,query)iferr!=nil{logger.WithField("err",err).Error("List match error.")return}iflen(matches)>0{logger.Info(matches[0].MatchId)}else{matchId,err:=nk.MatchCreate(ctx,"matchname",nil)iferr!=nil{logger.WithField("err",err).Error("Match create error.")return}logger.Info(matchId)}
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
functionfindOrCreateMatch(context: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama){constquery="+label.skill>=100 +label.skill<=150";varmatches=nk.matchList(10,true,"",2,maxSize,query);// If matches exist, sort by match size and return the largest.
if(matches.length>0){logger.info("Match id '%s'",matches[0].matchId);}// If no matches exist, create a new one using the "lobby" module and return it's ID.
varmatchId=nk.matchCreate('supermatch',{});logger.info(matchId);}
minSize 및 maxSize 매개변수를 사용하여 지정된 수의 플레이어가 포함된 대결만 나열할 수 있습니다. 이는 이미 거의 가득 찬 결과를 반환하는 데 유용할 수 있습니다(따라서 대결이 거의 시작할 때임).
예를 들어, 8명의 플레이어가 가입하면 경기가 시작된다고 가정하고 5-7명의 플레이어가 있는 대결만 나열합니다:
Server
1
2
3
4
5
6
7
8
9
10
11
localnk=require("nakama")locallimit=10localisAuthoritative=truelocalmin_size=5localmax_size=7localmatches=nk.match_list(limit,isAuthoritative,min_size,max_size)for_,matchinipairs(matches)donk.logger_info(string.format("Match id %s",match.match_id))end
Server
1
2
3
4
5
6
7
8
9
10
11
12
limit:=10isAuthoritative:=truemin_size:=5max_size:=7ifmatches,err:=nk.MatchList(ctx,limit,isAuthoritative,min_size,max_size,"");err!=nil{logger.WithField("err",err).Error("Match list error.")}else{for_,match:=rangematches{logger.Info("Match id %s",match.GetMatchId())}}
Server
1
2
3
4
5
6
7
8
9
10
11
functiongetMatchListings(context: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama){constlimit=10constisAuthoritative=true;constminSize=5;constmaxSize=7;constmatches=nk.matchList(limit,isAuthoritative,minSize,maxSize,"");matches.forEach(function(match){logger.info("Match id '%s'",match.matchId);});}