Over a million developers have joined DZone.

For Gamers: Creating a Simple Leaderboard with Azure Storage

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

If you are engaged in mobile application development you’ve most likely spent quite a bit of time thinking about services.  How can I provide interesting data to users of my app?  How can I save and share data between users?  Traditional solutions involve finding a hosting provider, standing up a host and writing services.  With cloud computing the barrier to entry is lower, however in many cases you still need to code the services layer.  But in some unique cases, it may be possible to solve your service problem without writing a single line of server side code.  In this example I’ll be using Windows Azure Storage and the Windows Azure toolkit for WP7 to create a leader board (a way to store and retrieve the highest scores).  I’ll be focusing on the basics, and allow you to add additional columns as needed.  For our leaderboard we’ll need two basic operations:

  1. Ability to save a score to the server
  2. Ability to request the top 10, 20, … scores

You may be surprised to find out that this can be accomplished via windows azure storage.  The example WP7 project code may be downloaded here.


You’ll need the following tools to run the project:

  1. Windows Phone development kit (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27570)
  2. Windows Azure Toolkit for WP7
  3. A Windows Azure Account with one storage service.  You can sign up for a free 90 day trial here http://www.microsoft.com/windowsazure/free-trial/

Step 1: define a domain class for the data

Our domain model will store the score and the alias of the gamer.  The domain model will also need to provide a partition key and row key as required by Azure Tables.  We’ll be taking advantage of the fact that partition keys are kept in indexed order from least to greatest.

public class Score : TableServiceEntity
private long p;
public Score() {}
public Score(long p)
this.p = p;
Value = p;
PartitionKey = (long.MaxValue - Value).ToString("d19");
public string Alias { get; set; }
public long Value { get; set; }
public override string ToString()
return Value + " (" + Alias + ")";

The partition key and row key are inherited from the TableServiceEntity class (part of the toolkit).  Did you notice how the partition key is derived?  We are taking the score and subtracting it from the maximum long value.  This ensures that the highest scores will have the smallest partition key.  This way our azure storage account acts as a priority queue.  When we request the scores, they’ll always return the highest scores, sorted in highest to lowest order.

Step 2: create a simple UX to proof the concept

Well need a button to submit random scores and provision the table with data.  We’ll also need a button to request the top 10 scores.  Finally we’ll need a list box to display the leaderboard.  Here’s the completed UX.  See the project code for details.


Step 3: code operations to persist and query scores

Lets start with the simpler query case.  Pulling data from Azure tables is very simple, and this is particularly the case when we’re using the WP7 toolkit as it comes with a handy C# client api. (Azure tables speak simple OData/Atom/Rest so you could role your own client).  In the following code you’ll need to replace “yourservice” with your azure storage account name.  Similarly you’ll need to supply your own account credentials.  We’ll discuss security and securing your account credentials in a later post.  Much of the example is building a URI…this is standard REST stuff.  The base URI will be your account, taken from the context object, plus the table name of “scores”, and finaly we indicate the number of items to return (10 in this case).  So in 14 lines of code we have a top scores service.

TableServiceContext ctx = new TableServiceContext("https://yourservice.table.core.windows.net", creds);
CloudTableClient c = new CloudTableClient(ctx);
table = new DataServiceCollection(ctx);
table.LoadCompleted += this.OnTableLoaded;
var tableUri = new Uri(

Now that we’ve solved retrieving the scores, we need to look at saving the scores. This example merely generates a random score between 0-1000. It also generates a GUID for the row key, to ensure that ties (scores of the same value) won’t conflict, that is, we cannot simply use the score as the primary key. Similarly, the alias are chosen at random. Scores make an ideal partition key as they’ll be distributed fairly well. As above, you’ll need to change “yourservice” to your own storage service name.

TableServiceContext ctx = new TableServiceContext("https://yourservice.table.core.windows.net/", creds);
string[] aliases = {"tacowan", "dizzer", "thedude", "puzzlemaster", "wp7owner"};
int rnd = random.Next(1000);
Score score = new Score(rnd);
score.RowKey = Guid.NewGuid().ToString();
score.Alias = aliases[rnd % 5];
ctx.AddObject("scores", score);
ctx.BeginSaveChanges(asyncResult =>
{try { ctx.EndSaveChanges(asyncResult);}

 I recommend downloading my example code, provide your own account key and name where necessary and run the example. Store a score or two then retrieve the results. You’ll see the scores readjust as new leaders are added. Hopefully you’ll think of even better ways to use Azure Storage. The value in this approach is that we are not utilizing and compute power or incuring any additional costs to run a server. Plus it’s simple, fast, and can service nearly unlimited demand as your application becomes more popular. You’ll never have to manage a server, or add new hosts, because this is in cloud storage.


Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}