Lucky Shamrock: saving game state with the Datastore API

// By Leah Culver And Steve Marx • Mar 17, 2014

Update: The Sync and Datastore SDK has been deprecated. Learn more here.

Happy St. Patrick's Day! For the holiday, we created a simple game using the Datastore API to demonstrate saving game state with conflict resolution rules.

Play the Lucky Shamrock game!

The goal of the Lucky Shamrock game is easy, just find the four-leaf clover in the field of three-leaf clovers.

A resolution rule is how the Datastore API determines what do with a field in the case of a conflict. Conflicting data can occur when two devices change the contents of a datastore simultaneously, or if data is modified when one or both of the devices are offline.

The Lucky Shamrock game saves two pieces of game data: your best time and the total number of games played.

Your best time is saved in a field called min_time and the resolution rule for the field is set to min. This means that in the case of a conflict we want to choose the minimum value (the fastest time).

table.setResolutionRule('min_time', 'min');

Your total games played is stored in a field called game_count and the resolution rule is sum. If there’s a conflict we want to save the total number of games played which involves adding up all games played across devices or instances of the game.

table.setResolutionRule('game_count', 'sum');

Lucky Shamrock is a basic example of using the Dropbox Datastore API to save a user's game state and shows how to use conflict resolution rules to determine the value of a fields in case of a conflict. If you want to see your data update as you play the game, you can watch the data update live in the datastore browser. The full code for Lucky Shamrock is available here.

// Copy link