How to Use App42 For Saving Extra Game Data and Creating Awesome Leaderboards

App42 Cloud APIs
2 min readFeb 13, 2023

--

Game Leaderboard APIs

What would you do to fetch the leader board with additional profile information of user like first name, display name, age, country etc?

Well, App42 Gaming APIs come truly handy, when we talk about saving the score and displaying the leaderboard in much lesser time. Many developer asked questions while integrating leaderboard about saving additional data and fetching back in the leaderboard. Earlier if you wanted to do this, you have to make additional calls to storage service to save and fetch back these information, however we have now introduced a better way of doing it without making any additional call to storage service. For App42 Leaderboard API documentation, click here.

Below example is explained using C# Unity code however can be done in similar way for other platform too.

Saving additional data of user’s profile

If you want to save the information on profile like name, age & country and display it on the leaderboard, you must save the information first on Storage Service.

[code java]App42API.SetLoggedInUser(userName);  //Logged in user in your app
StorageService storageService = App42API.BuildStorageService();
storageService.InsertJSONDocument(“YOUR_DBNAME”, "PROFILE_COLLECTION",
"{\"displayName\":\”UserDisplayName\”,\"Country\":\"US\",}", new UnityCallBack());
//This will save his profile info
public class UnityCallBack : App42CallBack
{
public void OnSuccess(object response)
{
App42Log.Console("Profile Saved : " + response);
}
public void OnException(Exception e)
{
App42Log.Console("Exception : " + e);
}
}[/code]

Also Read: Gamification: The Ultimate Strategy to Boost User Engagement

Fetching addtional info in leaderboard

Once your user profile is saved, you can easily fetch the information while retrieving the leaderbaord and the user’s score as well.

[code java]ScoreBoardService scoreBoardService= App42API.BuildScoreBoardService();
App42API.SetDbName("YOUR_DBNAME");
Query query= null;
// Pass query if you want to get only selected records, null if you want all JSON objects of user


scoreBoardService.SetQuery("PROFILE_COLLECTION", query);
int maxResults = 20;
scoreBoardService.GetTopNRankers(gameName, maxResults, new UnityCallBack() );
public class UnityCallBack : App42CallBack
{
public void OnSuccess(object response)
{
Game game = (Game) response;
for (int i = 0; i < getTopRanker.GetScoreList().Count; i++)
{
Console.WriteLine("userName is : " + getTopRanker.GetScoreList()[i].GetUserName());
Console.WriteLine("score is : " + getTopRanker.GetScoreList()[i].GetValue());
for (int j = 0; j < getTopRanker.GetScoreList()[i].GetJsonDocList().Count; j++)
{
// Following will return JSON Object saved for user and will have his profile info

Console.WriteLine("get json doc = " + getTopRanker.GetScoreList()[i].GetJsonDocList()[j].GetJsonDoc());
}
}
}
public void OnException(Exception e)
{
App42Log.Console("Exception : " + e);
}
}[/code]

If you need further information on ‘Saving Score & Fetching the Leaderboard’, do view our tutorial .

Originally Published on: https://blogs.shephertz.com/?p=6063

--

--

App42 Cloud APIs

App42 Cloud APIs help to build your apps in days, not months with AI and Gamification. We have the support of 75,000+ customers.