Sign In Register

How can we help you today?

Start a new topic

How to retrieve the time remaining until a leaderboard resets ?

Does LeaderboardDataRequest return the server time or would I have to write some cloud code to retrieve that ? I've set up daily and weekly leaderboards but I want to display a timer on screen that shows how much time is left until the leaderboard resets. I could use the device's time but that doesn't seem like a good idea in case it's set to the wrong time. If I do have to use cloud code then can you link me to a Unity example as I haven't written any yet. It seems like LeaderboardDataRequest  should return the server time, or time remaining to reset so we don't have to make two request. Thanks!


Hi Peter,


Daily Reset Leaderboards reset at midnight UTC and Weekly Reset ones reset Sunday night at midnight UTC. In your leaderboard data response Cloud Code script you could calculate the time remaining to the end of the day/week and attach it to the response as scriptData. You could then handle it accordinly on the client.


Regards,

Liam

Hi Liam, 

I also have the same requirement. Do you happen to have any cloud code snippet which fetches time remaining to the end of day/week/month?


Thank you,

Nitish

Nevermind, I figured it out. In case anyone else needs the snipped for finding time left in seconds till end of day/week/month, please find it below.

Disclaimer: the snippet is not tested for every edge case, please use it carefully.


var currentMoment = new Date(Date.now());


// returns the unix epoch in seconds at 00:00:00 of next day i.e. at the exact start of next day

function getDailyLbEndTimeInSeconds(currentMoment){

    return Math.round(new Date( currentMoment.getFullYear(), 

                                currentMoment.getMonth(), currentMoment.getDate()+1).getTime())/1000;

}


// returns the unix epoch in seconds at 00:00:00 of 1st day of next week i.e. at the exact start of next monday

function getWeeklyLbEndTimeInSeconds(currentMoment){

    var daysTillSunday = 7 - currentMoment.getDay();

    // Here January is 0 based

    return Math.round(new Date( currentMoment.getFullYear(), 

                                currentMoment.getMonth(), currentMoment.getDate()+daysTillSunday+1).getTime())/1000;

}


// returns the unix epoch in seconds at 00:00:00 of 1st day of next month i.e. at the exact start of next month

function getMonthlyLbEndTimeInSeconds(currentMoment){

    // Here January is 0 based

    return Math.round(new Date( currentMoment.getFullYear(), 

                                currentMoment.getMonth() +1, 1).getTime())/1000;

}



Regards,

Nitish

Login to post a comment