I’ve searched for a solution for making a scoreboard but I can’t find…
I tried many things but nothing works.
My problem is :
- I save in a Firebase an ID and a SCORE.
- When the user plays at another time, I fill a list (or a localDB) to obtain all the score from all the users
- I would like to sort the scores and find which position the current player is.
- Nothing works. it doesn’t work with the list and I tried with a localDB but it doesn’t work anymore). I can’t sort it and find the # of the current player.
For instance, the player A is playing :
I ID I SCORE I
Row1I A I 7 I
Row2I B I 10 I
Row3I C I 3 I
I don’t know how to sort on the SCORE column
I don’t know how to obtain the position of the current player.
I would like to inform the player that there 3 players in the palmares and he is the second best score…
Thank you ++++
Are all the users playing on the same device, or is it online multiplayer with an automatically updating scoreboard? It seems like extra work to bring a localDB into the mix.
Screenshots of blocks and solutions you’ve tried would be greatly appreciate and really aid anyone who wishes to help you out here. Thanks.
Thanks you for your answer.
You’re right, it is a multiplayer with an automatic update of the firebase.
I give you an example with a localDB because the list didn’t work.
I did a lot of testing. This is the last test. I’m completely lost
Now I don’ know how to sort localDB on the score and find the # of the current player.
Hi, I still do not see the need for a Local DB. It’s like trying to drive a car with 4 pedals. Unnecessary extras. Keep the score under the User ID, whether this is provided via external API, Firebase Auth or is just a chosen username from the player. Once the game is created (a lobby is added to the database), add all the players and their relative information to the lobby under a “players” attribute. Set a Firebase listener and every time the data changes, reset the score values for all players.
This is a lot of blocks for me to create for you, though I’m happy to walk you through it. For something like this, it’s best to sit down and figure out a well organised and formatted database. Once everything is organised in the database, you can start pushing to it.
Here is a very simple example of what your database could look like:
- For this example, “JSON” represents the very start of your Firebase tree.
- “sessionID” can be absolutely anything. A good one is using seconds since 1970 to make sure they’re all unique.
- “players” can contain however many players you want.
- “player1, etc” value is their score. You want to set your Firebase listener to the “players” attribute.
- Finally, I threw in “sessionName” to show how you can add more information.
This is not a global standard example, it’s just a simple solution. If you’ve something else in mind, please share it.
I have a simple leader board I have been testing. My realtimeDB has the leaders that always stay sorted.This works fine if you get the most current DB data before updating it. I believe adding a listener like @eoinparkinson suggests would be better. However, this is simple and works for me, because I use the lists elsewhere in my app.
How it works:
I get the data and put each in a list of names and points. Then I insert the updated info at the correct spot in the lists and “break out” of the loop to save time. I then save it all to firebase again. Firebase will only update the info that has changed.
Thanks +++ for your answer and proposition.
i did what you explained. But now how can I know that player 2 Is the best player ?
Do I need to fill a list to work on it (that’a why I created a LocalDBase) or can I sort and work directly on the firebase datas ?
I don’t really know how to work with objects… I will try to adapt your code to my projet …
I tried to adapt pour I have some problem with the variables name and determine which is Data or List.
- is user_coins_app is the score of the current player ?
- are LB_XXX lists and user_XXX only variables ?
Not a problem @franckled.
First let me define some things that will clear up some confusion:
LB_points_list = List of points from the leader board (points and coins are the same in my code)
LB_user_list = List of user names from leader board board
(I get these two lists in the same loop so they coincide with each other. Meaning user names from index 1 of the lists correlates to points of index 1 on the other list.)
App user_name_app = Users name in the database of current user logged in and using the app.
user_coins_app = is the score/points of the current players
App user_rank = gives you the rank of the current user. This is done in the first loop in the function.
App num_leaders = total number of users in the leader board. (See firebase picutre below at the bottom.)
Next lets look at how my database is set up so you can see the formatting since you aren’t strong with objects yet:
Make sure you do this!!!—> When you create your firebase data base make sure to initialize users in the correct order.
Just to start input 2 users and make sure user 1 has more points than user 2. If you don’t do this the code will rank the users wrong.
You asked how to find where a user ranks. It is already done in the blocks. You simply have to set a lablel to see it. For example: