Sign In Register

How can we help you today?

Start a new topic
Answered

Get the ShortCode of the latest partitioned Time-Based Leaderboard

Hi,


I created three time based leaderboards, day, week, month, based on this forum post:


https://support.gamesparks.net/support/discussions/topics/1000080581


However, I haven't yet been able to find a good solution on how to get the latest partition ShortCode for making Requests. I'm using  Unity/C#  and I thought about "constructing" on the client side the ShortCode of the latest partition for each type of leaderboard partition (day, week, month), in order to use it in a LeaderboardDataRequest and an AroundMeLeaderboardRequest.


Per the above post, there are 3 auto-generated attributes:


day_autogen : ${format(today, 'yyyyMMdd')}

week_autogen : ${format(today, 'yyyyww')}

month_autogen : ${format(today, 'yyyyMM')}


Since these attributes are well formatted, it's easy to get the current day and month of the client, by using something like:  

//Get current day and month as integers, and then convert to string,
//making sure that a '0' is added at the front of single digits
DateTime utcNow = DateTime.UtcNow;
int currentDay = utcNow.Day;
int currentMonth = utcNow.Month 

 However, getting the current week of the year is more complicated, because the Calendar.GetWeekOfYear Method (DateTime, CalendarWeekRule, DayOfWeek) will have to be used, and this:

 is dependent on the locale of the client, which may be different with the locale of the GS server auto-generating the relative attribute

- is not fully compliant with ISO 8601, which I assume is what the GS server would be using when partitioning the leaderboards.


This would result in a different short code being generated for the "week leaderboard" on the client, from that actually generated by the server.


Additionally, on a new day, week, month, when a new partition could have been generated, the  short codes generated on the client, could result in the relative leaderboard not existing yet, i.e. if a new score has not yet been submitted, in order for the system to create the new partition.


I've considered using a ListLeaderboardsRequest on the client, before making any Leaderboard Data Requests, and then:

- filtering out the day, week month partitions

- shorting the filtered results for each type, in order to find the latest partition and its ShortCode


Although this should/would work, and would guarantee getting a valid partition name to make the data requests, well it just seems "too heavy"... especially, after many partitions become available. It just seems like too much bandwidth and computing power usage, especially since it would have to be performed by each client, when the required ShortCode could be available from the server for all clients.


I considered using Properties in CloudCode but couldn't figure out how to do it. Although such a solution might be possible, it's really beyond my current level of proficiency on the matter.


I've been thinking that ideally, either a server event should be triggered whenever a new partition gets created, with the latest partition ShortCode passed as an argument, or something like a GetLatestPartitionShortCode should be provided, or both.


In any case, as things are, what is the best practice for getting the latest partition ShortCode?


Thanks in advance,


Best Regards


Panos


Best Answer

Hi Panos, 


Apologies for the delay here. I believe I have a solution for you. I have managed to construct the "yyyyww" part of the partition shortcode. Here is the function that I used : 


image


Let me know if this works for you.

Happy to help - Patrick. 




Answer

Hi Panos, 


Apologies for the delay here. I believe I have a solution for you. I have managed to construct the "yyyyww" part of the partition shortcode. Here is the function that I used : 


image


Let me know if this works for you.

Happy to help - Patrick. 



Hi Patrick,


Thank you for your reply.


I was aware of the first part of the GetIso8601WeekOfYear method (up to line 209), as it was in the relative MSDN blog:

https://blogs.msdn.microsoft.com/shawnste/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net/


Please clarify why you add +1 at the end of line 212.  I might be wrong about this, but this seems to mean that 'week' can never have the value of 1, since if the GetWeekOfYear method returns 1, 'week' will be 2.  So, as I understand it, the value of 'week' will range from 2 to 54, rather than 1 to 53.  Is this GS-specific or something?


Additionally, it seems that the GetIso8601WeekOfYear(DateTime time) method should be called in the client with DateTime.UtcNow as the DateTime argument in order to have consistent results. Please confirm.


This part of the response is quite helpful. However, I think there are some "catches" in doing it like this. More specifically, I was wondering if it can be done in a better, safer way, because there are cases where computing the partition ShortCodes like this could result in an exception being thrown. I would appreciate if you could respond to the remaining part of my post, which is the most important part of the post i.e. the part after the text:


"This would result in a different short code being generated for the "week leaderboard" on the client, from that actually generated by the server."


i.e. what if the ShortCode (day/week/month) computed in the client does not result in an existing partition? In such a situation, an error should/would be thrown, and then what? Computing 'backwards' new day/week/month seems rather an odd way to do it and quite inefficient. Yet, it seems like a valid assumption that such an error could occur, and it would need to be handled in the client somehow.


It is very important for me to handle this properly, this is why I have included my thoughts on the matter and would really appreciate your feedback:

i.e. instead of computing day/week/month partition short codes on the client, and having to handle an exception if the partition doesn't exist for the computed short code, maybe it's preferable to use a ListLeaderboardsRequest and get all existing leaderboards, and then "filter out and sort" to get the last leaderboard.


Ideally, I would prefer to have the client 'fetch' the last day/week/month partition from the server, rather than computing sort codes or even using ListLeaderboardsRequest. However, I don't know if it's possible to do it. I elaborate on this in my original post.


Thanks again,


Panos

Hi Panos, 


I believe that our Support Team is dealing with this via email. If you have any further questions please feel free to log a ticket. 


Best Regards, Patrick. 

OK, thanks!

Sorry to resurrect this thread, but I had been using the provided solution for my leaderboards and it was working fine up until yesterday. Now the calculated short code is offset by one week compared to the one created on GS when posting a score. I suppose it has to do with the +1 at the end of line 212 that Panos was also concerned about, although it was working correctly in 2017 but not in 2018.


I had gone through various other threads dealing with similar issues since there is no out-of-the-box solution, but wasn't able to find a better solution than the one provided in this post. Is there any way someone would be willing to clarify this issue? It was mentioned that the Support Team emailed Panos directly, but if there is a better solution it would be nice to share it with the rest of us facing similar issues.


Happy New Year!

Hi Ivano, 


I have just tested this and was returned the correct timestamp. The current week is 201801. In y test this I was returned this as the shortcode for my weekly partitioned leaderboard: "leaderboardShortCode": "LB_WEEKLY.week_autogen.201801"


partitionedIf you remove the +1 from your weekly timestamp generation in Unity this will resolve the issue. 


Happy to help - Patrick. 

Login to post a comment