Sign In Register

How can we help you today?

Start a new topic
Answered

Sum and Maximum leaderboard

 Hello,
I have been trying for long to set up a leaderboard, kind of a tricky one, and I just cannot manage to do so. I need some help please.
The leaderboard has to do the following:

- For a week, every time a player submits a score, it is added to the previous score.
- At the end of the week the leaderboard resets, and so do the scores, so the scores will start from 0 next week.
These two things are not the problem, after some conversations with you guys I know how to manage them. The tricky part is that I want that leaderboard to do something else:

- To store the total score for the week only if it is higher than the total score from last week.


For example:
- Week1: player A scores 5 on monday, and 2 on Friday --------> Player A would appear in the leaderboar with score = 7

- Week2: player A scores 1 on monday, and 1 on Friday --------> Player A would appear in the leaderboar with score = 7

- Week3: player A scores 4 on monday, and 4 on Friday --------> Player A would appear in the leaderboar with score = 8


I will keep trying, but if someone knows how to do this, I would really appreciate the help.
Thank you!


Best Answer

Hi Javier,


I've done up a rough example for how you can implement this. When partitioning you leaderboard by week it will use the timestamp from the start of the week to create the partition, so what you can do is set up an event that takes in a score attribute, then within that event get the timestamp for the start of the previous week, construct you shortCode from that, then get the entry for the current player from last weeks leaderboard partition. You can then compare them so if the new score is <= the oldScore, repost the oldScore using a SparkRequest and vice versa.


   

var newScore = Spark.getData().score

//get the timestamp of the start of last week. 
var now = new Date();
var weekOffset = now.getDay();
if(weekOffset == 0){
    weekOffset = 6;
}
else weekOffset--;
now.setDate(now.getDate()-weekOffset);
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);

//get the timestamp for the start of last week
var startOfLastWeek = now.getTime() - 604800000

//construct the shortCode for retrieving the entry from last week
var lastWeek_shortCode = "yourLbSchortCode"+startOfLastWeek

//get the entry from last weeks leaderboard
var entry = Spark.getLeaderboards().getLeaderboard(lastWeek_shortCode).getEntriesFromPlayer(Spark.getPlayer().getPlayerId(), 1).next()

//get the score from last weeks entry
var oldScore = entry.getAttribute("score");

//compare them
if(newScore <= oldScore){
    
    //SparkRequest here to post the oldScore to this weeks leaderboardPartition
}
else
{
    //SparkRequest here to post the newScore to this weeks leaderboardPartition
}

  

Does that sound it will work for you ? Let me know if you have any further questions.


Thanks,

Liam


Hi Javier,


Just to confirm, you only want to add a score this week if its higher than the total score from the previous week ? So at most you will only ever want to go back one week to check the previous score ? It looks like in week two a lower score was posted so you want to repost the high score from last week in its place, then add on scores as normal from then ? Are you currently partitioning your leaderboard by week ? We should be able to help you get this working if you can just confirm the points above.


Thanks,

Liam

Hi Liam,

Thanks for the response. Yes exactly, by going back one week and posting the biggest one, I assure that the player always has the biggest score he/she did in any week until the present time, which is what I want. Also yes, if a player had 7 last week and he does 2 this week, he stays with 7...so it is like he scored 7 this week. Then if next week he scores 8, he stays with 8.
Yes, I have it partitioned by week (actually now I have it partitioned by day, in order to be able to do daily tests).

Thanks,
Javi

 

Answer

Hi Javier,


I've done up a rough example for how you can implement this. When partitioning you leaderboard by week it will use the timestamp from the start of the week to create the partition, so what you can do is set up an event that takes in a score attribute, then within that event get the timestamp for the start of the previous week, construct you shortCode from that, then get the entry for the current player from last weeks leaderboard partition. You can then compare them so if the new score is <= the oldScore, repost the oldScore using a SparkRequest and vice versa.


   

var newScore = Spark.getData().score

//get the timestamp of the start of last week. 
var now = new Date();
var weekOffset = now.getDay();
if(weekOffset == 0){
    weekOffset = 6;
}
else weekOffset--;
now.setDate(now.getDate()-weekOffset);
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);

//get the timestamp for the start of last week
var startOfLastWeek = now.getTime() - 604800000

//construct the shortCode for retrieving the entry from last week
var lastWeek_shortCode = "yourLbSchortCode"+startOfLastWeek

//get the entry from last weeks leaderboard
var entry = Spark.getLeaderboards().getLeaderboard(lastWeek_shortCode).getEntriesFromPlayer(Spark.getPlayer().getPlayerId(), 1).next()

//get the score from last weeks entry
var oldScore = entry.getAttribute("score");

//compare them
if(newScore <= oldScore){
    
    //SparkRequest here to post the oldScore to this weeks leaderboardPartition
}
else
{
    //SparkRequest here to post the newScore to this weeks leaderboardPartition
}

  

Does that sound it will work for you ? Let me know if you have any further questions.


Thanks,

Liam

Hi Liam,

Yes, it sounds good. I think I made it work somehow else, but it implies more leaderboards and more events. Your solution seems better, I'll try and let you know. Thank you!

Javi

 

Hi Liam,

When you calculate the variable newScore like this: var newScore = Spark.getData().score ... isn't that the score you just posted to the leaderboard, instead of the sum of the scores posted this week?
If it is like that then it won't work, because what I want to compare is the sum of the scores posted this week with the sum of the scores posted last week. But I think your code will compare the score just posted (the one that triggered the event) with the sum of the scores posted last week.
I think the difference between the two scores you are comparing is that you are getting the oldScore from the leaderboard (which is ok because that way you get the sum of the last week scores), but then you are getting the newScore from the event (which is not ok because that way you don't get the sum).
Please correct me if I'm wrong, and sorry for the trouble.

Thank you!
Javi

 

Hi Javi,


Yes "var newScore = Spark.getData().score" would be the score just posted, in the same script you could add a line to get the start of this week (which you could use to check the current weeks snapshot)


 

//get the timestamp of the start of last week. 
var now = new Date();
var weekOffset = now.getDay();
if(weekOffset == 0){
    weekOffset = 6;
}
else weekOffset--;
now.setDate(now.getDate()-weekOffset);
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);
 
//get the timestamp for the start of last week
var startOfLastWeek = now.getTime() - 604800000

//get the start of the current week
var startOfCurrentWeek = now.getTime()

 

You could then use the "startOfCurrentWeek" variable to construct the shortCode and check this weeks entry also, if one exists check that "newScore" + the entry from this weeks leaderboard is greater than the entry from last week etc... How does that sound ? Let me know if you have any further questions.


Thanks,

Liam

 

 Hi Liam,

It sounds very good. I'll run some tests these days and I'll let you know.
Thank you very much!

Javi

Hi Javi,


No problem, let me know how you get on and if you have any further questions just let me know.


Thanks,

Liam

Login to post a comment