Synchronizing player currency (not score) over multiple devices – using a cloud

I was building a racing game which uses a fake currency. The player receives money when winning races, money that he can use to buy new cars or unlock new tracks.

But what if the player was playing during the day on his phone, and at night on a tablet? I wanted the progress to be synchronized.

 

The solution is simple when synchronizing a game that has a score that only goes up (incremented in only one direction).

if (scoreInCloud > scoreInDevice)

{

//download     

    scoreInDevice = scoreInCloud;

}else{

//upload

              scoreInCloud = scoreInDevice;

          }

This says that we always use the biggest score to decide if you upload the score to the cloud or if you download from the cloud.

 

But with currency the problem is trickier. We don’t have “the biggest currency”. The player can have 5000 money on one device, 5000 in the cloud, and 7000 on the tablet. How do you tell the tablet to update the money to 5000?

 

I first thought using timestamps to keep track on which info was the most recent. But that only complicates things, and didn’t solved the problem.

After going back to the drawing board, I came up with this solution: I use a versioning system. (duh)

 

When I upload the money value to the cloud, I also send a version:

Player X has 5000 money (version 3).

On the second device I can have 7000 money with version 2. I do a version comparison to decide if I upload or download to the cloud:

if (versionInCloud > versionInDevice)

{

//download       

    moneyInDevice = moneyInCloud;

} else {

//upload

                  moneyInCloud = moneyInDevice;

            }

Which translates to: get the money from the cloud if the version in the cloud is bigger than the one on the device, and vice versa.

 

Leave a Reply

Your email address will not be published. Required fields are marked *