This is the simplest possible single tag CloudDB sample for a top 5 score list.
Sample runs:
Designer:
Two text boxes are used for entry of player name and score.
A button is used to add the score and name to the top 5 list in CloudDB
Scores are lined up with Names in two parallel ListViews.
(Tweak the widths if you want to get fancy.)
The .aia file:
CloudDB_top_5_scores.aia (3.9 KB)
All blocks:
Details of operation:
global variables:
This will hold the high scores table in memory while we work.
This is so I can avoid typos in my long tag.
Screen Initialize:
We will need the high scores table from the cloud later, so we ask for it as soon as possible.
A single table will suffice, if we keep player names and scores together in pairs.
If the first run, we need an empty list as a default, to smooth over adding that first pair.
When the user asks to add a new score, we ask for the latest scores again.
The moment scores arrive, that is an opportunity to check if we have a new score to add to the high scores.
First we grab the incoming value and keep it in our global high scores variable.
I did not bother testing the tag because i use only one tag in this app.
Next, we check to see if there is a name and score available to be added to the high scores table. (non-blank name, and a numeric score.)
If we have a new score to add, we build a pair (score,name) and add it to our in-memory high scores table, at the end, regardless of what's already there.
The top5 value procedure is used to sort and filter the table, making it ready to upload and display. (See below for top5).
We upload the newly sorted top 5 table to CloudDB
To load the ListView Elements, we use one of the new nb191 list blocks, the Make New List From block. The Scores are gotten from column 1 of the high scores table, and the Names are gotten from column 2 of the high scores table.
Finally, we clear the Score text box to signal that it was processed.
I leave the name in the Name text box, in case he replays and wants to store another score.
The top5 value procedure:
This works in two steps, reading from right to left:
- sort the score table by column 1 (the scores) in descending order. (remember how new scores were added, with score in column 1?)
- return just the first 5 rows of the score table (see procedure first)
Getting the first n rows of a table:
The new nb191 slice list block almost fit the bill for this, but it needed protection from extreme cases like short lists.
It also has an unusual index2 parameter , which I accommodate internally to my first procedure.