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
Theres nothing wrong with the way you have it here, but if you want to pretty-up your code i would suggest that you correlate all your LeaderboardDataRequest into a single method. This method would take your count, offset and setSocial, as well as the leaderboard shortcode, and it would also take a callback method. The callback can then be where you can handle the different responses that you might want to handle differently for each leaderboard.

 

public delegate void RequestLBCallback(LeaderboardData data);
void RequestLeaderboard(int score, int count, int offset, bool setSocial, string lbShortCode, SomeCallback)
{
     new GameSparks.Api.Requests.LogEventRequest ()
			.SetEventKey(lbShortCode)
			.SetEventAttribute("score",score) // or whatever attribute you are sending
			.SetEntryCount(count)
			.SetOffset(offset)
			.Send ((response) => {

			if(!response.HasErrors)
			{
				Debug.Log("Score Posted Successfully...");
				SomeCallBack(response.Data);
			}
			else
			{
				Debug.Log("Error Posting Score...");
			}
	});
}

 

But i suspect you might have a more going on with your leaderboard requests.


This is a good way to go about it though. If you do have special things you need to do with your leaderboard data then you can do so with the callback methods. This also allows you to make sure that the data has been returned before you preform any actions like trying to display data (what if the display call happens before the server returns a response for example?), and to eliminate the need to check the leaderboard type by just calling the right leaderboard at the right time.

If you still need to select the right leaderboard type you can always use a switch-case to select the right one, but you shouldnt need to using this method.


Hope that helps,
-Sean

 

1 Comment

Answer
Theres nothing wrong with the way you have it here, but if you want to pretty-up your code i would suggest that you correlate all your LeaderboardDataRequest into a single method. This method would take your count, offset and setSocial, as well as the leaderboard shortcode, and it would also take a callback method. The callback can then be where you can handle the different responses that you might want to handle differently for each leaderboard.

 

public delegate void RequestLBCallback(LeaderboardData data);
void RequestLeaderboard(int score, int count, int offset, bool setSocial, string lbShortCode, SomeCallback)
{
     new GameSparks.Api.Requests.LogEventRequest ()
			.SetEventKey(lbShortCode)
			.SetEventAttribute("score",score) // or whatever attribute you are sending
			.SetEntryCount(count)
			.SetOffset(offset)
			.Send ((response) => {

			if(!response.HasErrors)
			{
				Debug.Log("Score Posted Successfully...");
				SomeCallBack(response.Data);
			}
			else
			{
				Debug.Log("Error Posting Score...");
			}
	});
}

 

But i suspect you might have a more going on with your leaderboard requests.


This is a good way to go about it though. If you do have special things you need to do with your leaderboard data then you can do so with the callback methods. This also allows you to make sure that the data has been returned before you preform any actions like trying to display data (what if the display call happens before the server returns a response for example?), and to eliminate the need to check the leaderboard type by just calling the right leaderboard at the right time.

If you still need to select the right leaderboard type you can always use a switch-case to select the right one, but you shouldnt need to using this method.


Hope that helps,
-Sean