Leaderboard: show logged-in user only his place

I have a number of users that each has a number of points. The data type looks something like this in the database:

User Points
Charles 120
Paula 90
Brutus 145

I can have a repeating group sorted by Points to get a leaderboard like this:

  1. Brutus (145 points)
  2. Charles (120 points)
  3. Paula (90 points)

My question is, if Paula is logged in, I want her to only see that she is at number 3 in the leaderboard. How can I achieve this?

1 Like

Hello @daniel.svedberg
You can try to add a condition when this cell user is current user →
text = Current cell index.

Hey @expertnocode, thank you!
So I should set a condition on the repeating group so it only outputs the text when it hits the current user?

The process for the repeating group would look something like this?

  1. Brutus (do nothing, move on)
  2. Charles (do nothing, move on)
  3. Paula (aha, this is the current logged-in user, output current cells index)

@adamhholmes this would have to be a contender for an interesting Bubble challenge :slight_smile:

@daniel.svedberg I understand that you want to find the current user’s leaderboard position, without displaying all of the others. The simple solution is to, yes, find ALL leaderboard positions, and conditionally hide them if it’s not the Current User, but that is WU heavy and requires returning all position data to the client. Maybe you want to display ‘#3’ in the User’s dashboard, for example.

The problem is, Bubble doesn’t give us a great way to find a Thing’s position in a list. What you’d want to do (and Bubble won’t let you) is Sort a list, then retrieve the index of the row where User = Current User. Bubble doesn’t let us get the index - and as far as I can tell, most of the workarounds require returning all of the other data (e.g unique IDs) and using some Regex or Split by to work out the position in the list.

If that’s fine for you, then go for it, but Bubble makes it harder than it needs to be!

This should work… (but haven’t actually tried it)

Search for users [where points > Current User's points]: grouped by points: count: +1

That would first find all users with more points than the current user (i.e. who are higher in the leaderboard).

Then it would establish how many actual positions above the current user there are (assuming multiple users with the same points would share the same position).

Then just add 1 to get the current user’s position.

e.g.

1 - Brutus: 145
2 - Charles: 120
2 - Sam: 120
3 - Paula: 90
3 - Max: 90
4 - James: 85
5 - John: 80
6 - Emma: 40
7 - Tony: 30
7 - Mary: 30

When John logs in that would give:

Search for users with more points than John (> 80):

Brutus: 145
Charles: 120
Sam: 120
Paula: 90
Max: 90
James: 85

Grouped by points:

145
120
90
85

count = 4

plus 1 = 5 (John is in 5th place on the leader board)

The only data returned here should be the final number (count) - or possibly the list of groupings, but those are very light anyway.

6 Likes

Works like a charm.
Just have to define your own rule for equality. To know if you do the group by thing or not. And have John in the 5th or the 7th place (with this example).

1 Like

If only the rank by operator gave an ability to select a data field to rank by

Screen Shot 2024-09-09 at 9.24.39 PM

Great workaround, love it!

2 Likes

Yeah, I reckon it’s 0.3 WU search/group by, possibly + 0.2 WU for count (though who knows whether Bubble charges for that as surely that’s already in the browser). Max 0.5 WU if the docs are right. Pretty damn good, and a simple solution with no hacky workarounds.

1 Like

Nicer one ! :muscle:

1 Like

@adamhholmes You my good sir are a gentleman and a scholar! Thank you! That did the trick :partying_face:

1 Like

Just my opinion…

but, I like Bubble because it can do a lot.

However, for some things, I don’t understand why you wouldn’t use third-party software.

For example, Referral Hero is like $200 a month and does a ton of stuff that would take a whole lot of work units to do. Leaderboards and everything else.

Why not use what’s out there and incorporate it into your Bubble app?

It’s like scheduling. Why not use Calendly or something like that and save yourself a ton of work units?

Maybe I’m wrong. Just what I’ve noticed so far.

If anyone has any thoughts I’d love to hear them.

1 Like

For me personally, I try to avoid any 3rd party platforms originally due to extra fees, but now that WUs are involved that is not necessarily the first reason, but is still a consideration. For example a 3rd party database, may cost $99/month, but the WU tier that I need to upgrade may only be $30/month or equal to the $99/month 3rd party database.

So, if the costs are equal, it then comes down to complexity of the build in Bubble and also the reliance on more than one provider, which could mean, problems with Bubble make app not function and problems with 3rd party platform make the app not function. In addition to that, it is the need to learn a 3rd party platform as well that gives me pause in implementing more 3rd party tools into the Bubble app. And lastly, I don’t like having to check in so many different places for things, and so to have to check 3rd party accounts to see things in addition to seeing them in Bubble is something I like to avoid, especially during debugging.

I can understand there are probably examples of 3rd party systems that can be incorporated that are free, not complicated etc. but still, I prefer to be reliant on less providers as it feels a bit ‘safer’.

I imagine as well, that if the time were to come when a commercial application of mine was taking off, and the need for a 3rd party tool to do something Bubble either is too costly for or not as efficient, then I would bite the bullet and do what needs to be done. I imagine for most on Bubble, they have yet to reach that point, so they may continue to implement the KISS principle in getting their MVPs out the door.

1 Like

This is great, I would just add that if your “points” shouldn’t be public to other users and you just want the position on the leaderboard, you’d be best off running this in a backend workflow that ignores privacy rules. That way the search is done server-side and you’re not exposing any points to other users. It probably also makes the most sense to update this once per day rather than every time the page loads.

Are you sure about that?

Solution above will cost max 0.5 WU per run.

Updating a rank field on each user once will cost at least 1 WU per user per day, when 98% of users won’t even be logging in that day.

Don’t do that.

If you really want to cache it, then do it when the user logs in, for the first time today, and save their position in a field and reference that everywhere else. But, as with many WU optimisations, probably not worth it given the poorer user experience it results in.

1 Like

99.99% of leaderboards and ladders I’ve seen do not calculate themselves on demand. Even in the majority of video games the ELO will update instantly but the “Top 10” typically doesn’t update on a per-match basis. That would frankly be bizarre. A leaderboard that calculates once per day or once per week is much more common, YMMV.

Yeah, calculating this once per day (for every single User in the database) will not only cost way more in WU, but also (potentially) make the data so inaccurate as to be completely irrelevant.

1 Like

Why are we calculating this for every single user, even if they’re inactive or haven’t contributed in the last day? And it would be inaccurate by one day at the most.

I don’t have enough context to say if that’s a problem, but in many cases it won’t be.

Thanks for the feedback.

I understand what you mean about 3rd party software.

Even someone like AirBnb though uses 3rd party software. They use Grow Surf for their referrals. You just include the script and the whole thing runs on its own. Emails, fraud prevention, etc.

I could use Ruby on Rails like Morning Brew, but then again I asked myself why, when I could take 15 minutes, include some script, and have something that ran on its own.

I spent hours over a few days learning how to build a referral program for my app…when I was done i decided by the time I added all the features a 3rd party program had it would take more work to just maintain the program than what it was probably worth. Plus, the added cost in WUs. Referral programs can grow big really fast if they’re done right.

My goal is to just have an app that can pretty much run on its own. Yes, I know apps always need maintenance, but I’m trying to minimize that :slightly_smiling_face:

The only 3rd party software I was also considering that requires using their admin panel is tawk.to, and that’s if I decide to add a chat feature. It’s free, but I’m still considering it.

Maybe someday when I get more experienced with Bubble some of the added features won’t be so complicated for me to figure out.

Well, how else could you do it, if not for every user? In order to calculate a Users position you have to establish every other user’s score/position regardless of whether they’ve been active in the past day or not. (if someone else has been active on a given day and I haven’t, they may have overtaken me in the leaderboard, so you’ll have to update my position as well as everyone else’s).

Exactly - so if users are earning points on an hourly basis, it will be wildly inaccurate to the point of being useless.