Really, really stuck making a live-play game

@codeswept,

I would take a slightly different approach that my esteemed Thunker @tatiang. Games are often good candidates for object oriented-ish data models. I would use a JSON Object Hierarchy such as

QuizGame → Games (List) ->Game (Object) → Players (list) → Player (Object) ->Score (property).
To add a game, I would do something like the blocks below. First, chek to make sure there is at least one game (which is mostly for the testing/development phase). Then add a new game to the list of games, retaining the RFDB key in an app variable (stored would probably be better…). Then add the listener to the game node, that way only the game the player joined is being listened to (vs. all of the games in the databse).

Adding a play to an existing game is pretty easy. Just give them the index of the game and add them as a player.

Now you are ready to pull the scores. The key here is the sorting of objects, which Thunkable cannot do. But never fear, DrTed has a prescription for that (More JSON - Sort Object List by Object Property) with the SortObjectList function. Once you copy that function to your screen, you can build this listener.

Debugging listeners can be pretty tricky, since the events happen based on data updates by other users. So robust interpreting of the data (null checks, property checks, etc.) will save you time in the end. After verifiying the listener returned the expected object, sort the players list by the score and loop through the sorted list, adding players to the List Viewer.

One final tip. I found that for testing apps where different users are communicating, I found it easiest to sign into Live test with 2 different Firebase accounts, one on a device and one on the web live test.

Disclaimer: I haven’t tested these blocks, so I’m sure there are a few bugs. But hopefully this will get you started.

Happy Thunking!

P.S. It would probably be a good idea to create an Alias in the Player Object to display in the leader board, again, hopefully this gets you started…

2 Likes