대결이 끝날 때마다 플레이어의 점수를 순위표에 기록합니다. 대결이 끝날 때마다 클라이언트는 서버로 RPC 요청을 보내 최종 게임 행위를 처리합니다:
match.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
interfaceMatchEndRequest{// Create a structure to align the payload to
matchId : string;placement: MatchEndPlacement;time : number;towersDestroyed : number;}constrpcHandleMatchEnd: nkruntime.RpcFunction=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,payload: string):string{...// Add gems to wallet, calculate score, etc. etc.
letrequest : MatchEndRequest=JSON.parse(payload);letscore=calculateScore(request.placement==MatchEndPlacement.Winner,request.towersDestroyed,request.time);nk.leaderboardRecordWrite(globalLeaderboard,ctx.userId,ctx.username,score);...}
점수를 계산하는 방법은 사용자와 게임의 메커니즘에 따라 다릅니다. 이 예시와 같이 누가 이겼는지, 승리까지 시간이 얼마나 걸렸는지, 타워를 몇 개 파괴했는지 등을 고려할 수 있습니다. 이러한 모든 정보는 클라이언트에서 페이로드로 전달됩니다.
점수를 계산한 뒤에 표시할 사용자 ID와 사용자 이름과 leaderboardRecordWrite(으)로 숫자를 전달하면 순위표 엔진에서 나머지 작업을 처리합니다.
새로운 순위표 기록을 처리할 수 있도록 서버를 설정했고, 이제 플레이어가 서버에 저장된 항목을 볼 수 있도록 설정합니다. 이 작업은 ListLeaderboardRecordsAsync을(를) 사용하여 클라이언트 측에서 완료할 수 있습니다:
LeaderboardsMenuUI.cs
1
2
3
4
5
6
7
8
9
10
[SerializeField]privateint_recordsPerPage=100;...publicasyncvoidShowGlobalLeaderboards(stringcursor=null){// Fetch all records from the leaderboard "global"IApiLeaderboardRecordListrecords=await_connection.Client.ListLeaderboardRecordsAsync(_connection.Session,"global",ownerIds:null,expiry:null,_recordsPerPage,cursor);SetLeaderboardsCursor(records,ShowGlobalLeaderboards);...}
위의 예시와 같이, SetLeaderboardsCursor은(는) 다음과 같이 표시됩니다:
여기서는 ListLeaderboardRecordsAsync에 의해 반환된 records의 목록에 PrevCursor 및 NextCursor의 함수가 표시되고, 이전 또는 다음 페이지(다음 페이지가 있는 경우)에서 레코드를 얻습니다.
플레이어가 레코드를 쉽게 찾을 수 있도록 해당 함수를 버튼과 연결합니다. PrevCursor와(과) NextCursor 모두 기존의 ListLeaderboardRecordsAsync 호출에서 설정한 페이지 별 레코드를 준수하고, 레코드의 다음 페이지를 유지하도록 records 개체를 업데이트합니다.
이 레코드를 표시할 수 있도록 records에서 레코드마다 foreach 루프를 사용할 수 있습니다: