[Unity] Realtime Database Leaderboard - User position

1,826 views
Skip to first unread message

Miron Alex

unread,
Jan 15, 2018, 1:51:26 PM1/15/18
to Firebase Google Group

Is there any cheaper way to get a user's position in my firebase leaderboard without fetching all the ordered data?

Lets assume I have this database tree:


Leaderboards:

    racetrack1:

        User1:

            Time: 21,333

        User2:

            Time: 25,333   

       User3:

            Time: 88,333

.....


        User5555:

            Time: 27,333

        User2333:

            Time: 28,333   

       User4444:

            Time: 22,333


Is there any way I can get say... User4444's position without fetching all the ordered data and checking the index myself?

Screen Shot 2018-01-15 at 19.27.58.png

Kato Richardson

unread,
Jan 16, 2018, 10:09:27 AM1/16/18
to Firebase Google Group
Miron,

Are we talking about Realtime Database or Firestore here? The short answer is that we aren't storing any hidden data that you can't see. There's no position indicator stored automatically.

As for implementing a counter, leaderboards are traditionally hard to build well. In my opinion, Firestore has yet to bring anything too exciting to the table to address this.

Here's some obvious choices, assuming you aren't over optimizing and actually need something more sophisticated than just loading the top 1K scores and counting the positions manually:
  • Use Functions to store a positional indicator on score changes ( while previous_user_score < mine, flip our positions )
  • Have the client update the position when they change their score (depending on your use case, you may be able to enforce this with rules)
  • Update the score positions on a regular intervals from a server process by simply iterating down the score list and assigning it (probably by writing to a static/separate read-only collection).
  • Maintain an index of the top 100 or 1K scores in memory on a server and query that instead (e.g. Redis)
You'll probably want to be familiar with this thread as well. Frequently changing scores are likely to run into similar limitations since you'll likely be indexing on position, which falls into the purview of a sequential numeric counter.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/32e12a7b-0c33-4803-83a5-4d6ddde46f84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Miron Alex

unread,
Jan 16, 2018, 5:47:20 PM1/16/18
to Firebase Google Group
Thank you for your answer, Kato.

I have built leaderboards with redis before, but I was looking for a solution that will only involve using Firebase to speed up the dev process.
My guess is that adding a position indicator and then changing it for possibly a big number of users on each score update, might be a bit too much in terms of costs.

Considering my previous example and the fact that I'm using indexing on the Time property, could it be possible to only get a range of values using two indexes?
e.g. Get users 5-100 (since the database is already sorted by using indexing)?

Thanks again!

Kato Richardson

unread,
Jan 17, 2018, 11:43:24 AM1/17/18
to Firebase Google Group
I don't see anything in the docs suggesting that's possible. Again, there's no internal position counter maintained by the server, so you'd have to build that in.

☼, Kato


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages