Sign In Register

How can we help you today?

Start a new topic
Answered

Handling multiple similar Leaderboards code design

Hi,

      GameSparks  is really great till now and love the workflow. At first I was excited to see it being too strictly typed sdk with latest release. But, now I see some issues in the code maintenance as I need to duplicate the code.


I first created HIGH_SCORES_ALL_TIME leaderboard and finished the handling of it using custom code which i downloaded from editor.

Later I created HIGH_SCORES_WEEKLY,HIGH_SCORES_DAILY and HIGH_SCORES_MONTHLY leaderboards.


Now as everything is strictly typed and can't make use of inheritance or typecase to base classes for code management, its letting me duplicate most of the code :|


 Here is the sample. Can any one help me out if I miss anything to understand in this or its the only way to do it?


if(type == eLeaderboardTypes.HIGH_SCORES_ALL_TIME)
{
LeaderboardDataRequest_HIGH_SCORE_ALL_TIME request = new LeaderboardDataRequest_HIGH_SCORE_ALL_TIME();
request.SetEntryCount(maxCount);
request.SetOffset(offset);
request.SetSocial(requestFriendsOnly);

request.Send((LeaderboardDataResponse_HIGH_SCORE_ALL_TIME response) =>
{
Debug.Log("Received Leaderboard response" + response);
OnReceivingGameSparkLeaderboardData(type,response);
});

}
else if(type == eLeaderboardTypes.HIGH_SCORES_MONTHLY)
{
LeaderboardDataRequest_HIGH_SCORE_MONTHLY request = new LeaderboardDataRequest_HIGH_SCORE_MONTHLY();
request.SetEntryCount(maxCount);
request.SetOffset(offset);
request.SetSocial(requestFriendsOnly);

request.Send((LeaderboardDataResponse_HIGH_SCORE_MONTHLY response) =>
{
Debug.Log("Received Leaderboard response" + response);
OnReceivingGameSparkLeaderboardData(type,response);
});
}
else if(type == eLeaderboardTypes.HIGH_SCORES_WEEKLY)
{
LeaderboardDataRequest_HIGH_SCORE_WEEKLY request = new LeaderboardDataRequest_HIGH_SCORE_WEEKLY();
request.SetEntryCount(maxCount);
request.SetOffset(offset);
request.SetSocial(requestFriendsOnly);

request.Send((LeaderboardDataResponse_HIGH_SCORE_WEEKLY response) =>
{
Debug.Log("Received Leaderboard response" + response);
OnReceivingGameSparkLeaderboardData(type,response);
});
}
else if(type == eLeaderboardTypes.HIGH_SCORES_DAILY)
{
LeaderboardDataRequest_HIGH_SCORE_DAILY request = new LeaderboardDataRequest_HIGH_SCORE_DAILY();
request.SetEntryCount(maxCount);
request.SetOffset(offset);
request.SetSocial(requestFriendsOnly);

request.Send((LeaderboardDataResponse_HIGH_SCORE_DAILY response) =>
{
Debug.Log("Received Leaderboard response" + response);
OnReceivingGameSparkLeaderboardData(type,response);
});
}
else
{
Debug.LogError("This leaderboard is not yet implemented! " + type);
return;
}


Best Answer
I've actually thought of a much neater way to handle this but i replied in a different post.
You can check it out here.

 


1 person has this question

 Hey Ayyappa,


Unfortunately there isn't a shorter way of doing what you want, as the calls are pretty-much as you are using them. I would also be hesitant to suggest improvements, since you are using it correctly, and we would like users to feel comfortable doing things tin whatever way works for them.


However, you did get me thinking, and i come up with something that might help.

Its just a method that calls the request, but you give it an enum with the shortcode name. that way, you can call the function instead of using the if-else stuff you have there.

You might not need all of this functionality, and it doesn't shorten the amount of code you have to write by much, but it will cut down on duplicating the code, and only have the LeaderboardRequestData() call in one place. Then you can call one method with the short-code from wherever you want.


 

class LeaderBoardHelper
	{
		public enum LeaderboardType
		{
			HIGHSCORE_ALLTIME,
			HIGHSCORES_MONTHLY,
			HIGHSCORES_WEEKLY,
			HIGHSCORES_DAILY

		}

		/// <summary>
		/// Returns an array of leaderboard data for a specific leaderboard 
		/// </summary>
		/// <returns>Leaderboard data</returns>
		/// <param name="_lbShortCode">leaderboard short code.</param>
		/// <param name="_entryCount">leaderboard entry count.</param>
		/// <param name="_isSocial">If set to <c>true</c> will only return data of friends in leaderboard</param>
		public static LeaderboardDataResponse._LeaderboardData[] LeaderBoardRequest (LeaderboardType _lbType, int _entryCount, bool _isSocial)
		{
			// create a list of leaderboard data to begin with
			List<LeaderboardDataResponse._LeaderboardData> leaderboardList = new List<LeaderboardDataResponse._LeaderboardData>();
			new GameSparks.Api.Requests.LeaderboardDataRequest()
				.SetLeaderboardShortCode(_lbType.ToString())
					.SetEntryCount(_entryCount)
						.SetDontErrorOnNotSocial(_isSocial)
							.SetDurable (true)
								.Send ((response) => {

						if(!response.HasErrors) // check for errors before we try take the data out
						{
							Debug.Log("Recieved Leaderboard data...");
							// iterate through the response data and we will collect the leaderboard-data into the list we created at the start of the function
							foreach(LeaderboardDataResponse._LeaderboardData i in response.Data)
							{
								leaderboardList.Add(i); // start collecting the data into the list
							}
						}
						else
						{
							Debug.LogError("Error Retrieving Leaderboard data...");
							leaderboardList = null; // setting this to null if we get errors allows us to check when we get the data back
						}
					});
			return leaderboardList.ToArray();
		}
	}

 


(You'd need to rename the enums to whatever your leaderboard short-codes are)


Then you can just call this where you have your code above, and put the type in when you call the method instead of checking each type, since if the type is the same as the short-code, casting it to a string will get you the right leaderboard anyway.


Hope that's of some use to you..

- Sean

Answer
I've actually thought of a much neater way to handle this but i replied in a different post.
You can check it out here.