Skip to main content
All CollectionsGeneral questionsSection heading
How to get all players' entries on a leaderboard
How to get all players' entries on a leaderboard

leaderboard view, leaderboard entries

Jason Liang avatar
Written by Jason Liang
Updated over 3 years ago

For the case you want to retrieve all the players' entries on a leaderboard, you can write a script to achieve it.

GetGlobalLeaderboardView() the method returns 100 entries, by setting different beforeCount and afterCount to retrieve the players' entries around the current user, due to the current user's score rank is uncertain, so will need to retrieve both ways of beforeCount and afterCount to get all entries, an easy alternative way will be using GetGlobalLeaderboardPage() method with the index parameter instead.

Go ahead to create a cloud code script on brainCloud portal and set the script parameter as following:

{
"leaderboardId": "l2",
"sortOrder": "HIGH_TO_LOW",
"maxNumIndexes" : 10
}

Using a do...while loop to determine if it is required to call the next page.

"use strict";

function main() {

var response = {};
bridge.logDebugJson("Script inputs", data);
var leaderboardId = data.leaderboardId;
var sortOrder = data.sortOrder;
var startIndex = 0;
var endIndex = data.maxNumIndexes;
var leaderboardProxy = bridge.getLeaderboardServiceProxy();
var entryCountFlag = 0;
var aReturnedEntries = [];
var postResult = leaderboardProxy.getGlobalLeaderboardPage(leaderboardId, sortOrder, startIndex, endIndex);
if (postResult.status == 200) {
// Success!
if (postResult.data.leaderboard.length > 0 ){
bridge.logDebug("leaderboard length"+postResult.data.leaderboard.length, postResult.data.leaderboard.length);
var count = 1;
do {
bridge.logInfo("count iteration "+count, count); postResult.data.leaderboard.forEach(item=>aReturnedEntries.push(item));
if (postResult.data.leaderboard.length > data.maxNumIndexes ){
startIndex += (data.maxNumIndexes + 1);
endIndex += (data.maxNumIndexes + 1);
postResult = leaderboardProxy.getGlobalLeaderboardPage(leaderboardId, sortOrder, startIndex, endIndex);
if (postResult.status == 200 && postResult.data.leaderboard.length <= (data.maxNumIndexes)) { postResult.data.leaderboard.forEach(item=>aReturnedEntries.push(item));
}
}
entryCountFlag = postResult.data.leaderboard.length;
count++;
}
while (entryCountFlag > data.maxNumIndexes);
}
}
response.entries = aReturnedEntries;
return response;
}
main();
Did this answer your question?