I’m creating a Facebook game using HTML and Javascript, and I’ve just finished building a leaderboard table which lists every player’s name and rank number. This table is populated with data returned from Facebook's game scores API.
This is working perfectly, but I also want to reward players for improving their rank in the table.
This is how I plan to do this:
- When the game loads, I run a function called
updateTable();
, this populates the leaderboard with the scores and ranks of the players received from an API call to Facebook's database. - When the player starts to play the game, I store a copy of their rank inside a separate hidden div.
- When the game ends, if the player has achieved a new high score, then
it gets entered into the database. After this happens, I run
updateTable();
again to update the leaderboard. - I then run a function called
compareRanks();
, this compares the player’s new rank with the rank that I’ve stored in the hidden div.
If the new ranking is a lower number than the stored rank, then they’ve moved up the leaderboard and I reward them 100 coins for every place they move up.
For example:
Player A starts the game and is ranked 5th (so “5” gets stored in a hidden div). When Player A finishes the game, the leaderboard is updated, and Player A is now ranked 2nd (so the player has jumped 3 places).
To work out what the reward should be, I want to subtract the first variable from the second (5-2 = 3), Player A overtook 3 other players, so their reward will be 3 x 100 gold coins.
The problem I’m having is that when I run compareRanks();
, the new rank keeps showing up as the same number as the stored rank, even though I know that the player has improved their rank.
I’m pretty sure this is due to the new rank being grabbed before updateTable();
has fully interacted with the database. I’ve tested this by separating the functions, by making compareRanks();
run on click of a button, when I did this, I completed a game, improved my rank, waited a few seconds after updateTable();
ran, then clicked the button, and the two ranks showed up differently, which is correct. So I think compareRanks();
just needs to wait for updateTable();
to fully complete before it runs.
This is how my functions are laid out:
updateTable(){
//code here interacts with the database/makes a call using Facebook's API,
//and populates the leaderboard table with the returned data
}
On start of a new game, the player’s current rank is stored in the hidden div.
When the game completes updateTable();
is run again, followed by compareRanks();
:
compareRanks(){
//code here grabs the stored rank from the hidden div
//code here grabs the newly updated rank and compares the two.
}
I’ve read answers about using callbacks, but I couldn’t get them to work. And I’ve tried doing something like this:
updateTable(){
{
//code here interacts with the database/makes a call using Facebook's API,
//and populates the leaderboard table with the returned data
}
compareRanks();
};
But the new rank is still showing up the same as the old rank when compareRanks();
runs. updateTable();
is changing the ranks correctly on the leaderboard when it runs, so I think compareRanks();
is just running before updateTable();
fully completes.
I’d really appreciate any help in fixing this problem, thank you in advance!