Forum BigDB Sorting and loading ranges on multiple fields

Discussion and help relating to the PlayerIO database solution, BigDB.

Sorting and loading ranges on multiple fields

Postby modegames » January 1st, 2013, 7:58 am

Hi,
I have setup a table for highscores, I am wondering if it is possible to sort and load a range of scores by a date range for example. This is useful for a highscore table based on scores earned in a week or on a day. So far I have found this difficult to achieve using the function loadRange. Currently I load in the maximum scores which is 100 for a date range and just sort these on the client side. Unfortunately this does not give me an accurate result for this, but is fine for now. Any help is appreciated!
modegames
 
Posts: 3
Joined: January 1st, 2013, 12:03 am

Re: Sorting and loading ranges on multiple fields

Postby Henrik » January 1st, 2013, 12:47 pm

No, you can't have multiple ranges when doing an index load, so what you have to do is make one property that is the same for your lookups and use that as the path variable, and then make an index over both properties.

So for daily scores, make a property that contains only the date of the score, not the time.

And for weekly scores, make a property that contains the date of the Monday of that week, not the actual date and time.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Sorting and loading ranges on multiple fields

Postby modegames » January 1st, 2013, 1:16 pm

Ok thanks thats a quite interesting way to solve. How can I update all existing scores with the new data? Currently scores are only posted with a single date/time, not with the day date and the start of week date, ideally I need a simple way to update existing records.
modegames
 
Posts: 3
Joined: January 1st, 2013, 12:03 am

Re: Sorting and loading ranges on multiple fields

Postby Benjaminsen » January 4th, 2013, 11:22 am

By far the simplest way to solve this goal is to store a record for each date/date range you want to query. Then use a two dimensional index to look up.

Eg

Code: Select all
Table: DayScores
Randomkey = {
   userid:userid,
   day:days-since-1-1-1970,
   score:score
}


This would allow you to do a top list for each day, by using day as a path and score as the index value.
The same method can be used for week etc, as you simply write day/week each time a higher day/week score is submitted.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Sorting and loading ranges on multiple fields

Postby modegames » January 4th, 2013, 12:07 pm

Thanks but that was not the question. It was how to update the bigdb table using the date I have, this is to create these new fields for date for day and date for week.

Its not much of an issue now as this week is almost over and flash clients should be submitting these new fields and in theory I can switch it over for next week. Thats given that flash clients have not cached the old swf which is very likely ;) Any help would be useful even if it is to say, no thats not possible.
modegames
 
Posts: 3
Joined: January 1st, 2013, 12:03 am

Re: Sorting and loading ranges on multiple fields

Postby Benjaminsen » January 4th, 2013, 12:27 pm

modegames wrote:Thanks but that was not the question. It was how to update the bigdb table using the date I have, this is to create these new fields for date for day and date for week.

Its not much of an issue now as this week is almost over and flash clients should be submitting these new fields and in theory I can switch it over for next week. Thats given that flash clients have not cached the old swf which is very likely ;) Any help would be useful even if it is to say, no thats not possible.


Oki a few points. For the system to work properly, you would need to create a separate table to host your scores. Even more so you would need a table for each date range you want to cache scores in.

Thinking a bit more about it, the best solution would also be to use database keys that relates to each user, as this would allow you to easily load those objects when you want to submit new scores.

So, looking at the "one score per day" example you would do the following in flash.

1: Get the current day:
Code: Select all
var dayOffset:int = new Date().time/1000/60/60/24


2: Use the day offset + the userid as the key for the current day
Code: Select all
client.bigDB.loadOrCreate("DayScores", client.connectUserId + dayOffset, function(o:Object):void{
   //If day score is undefined, set score to 0;
   o.score = o.score || 0;
   //Set day offset
   o.day = dayOffset;

})


3: Then saving todays score is as simple as:
Code: Select all
function saveDayScore(score:Number):void{
   if(score > o.score){
   o.score = score;
   o.save();
   }
}


However, you really should use the MP server to submit the scores to ensure that date ranges etc are the same.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark


Return to BigDB