Azure, Cloud, Development, MVVM, Open Source, Silverlight, USCloud, Windows, Windows 8, WP7, WP8

Scaling Collaborative Applications

One of our local MVPs, Michael Perry, has written a great article about scaling collaborative Windows 8 applications with Correspondence and Windows Azure.

Azure is a great way to quickly deploy and scale your web sites. Correspondence is a rich framework for bringing collaborative experiences to multiple platforms. When these two come together, amazing things can happen.

http://qedcode.com/content/scaling-collaborative-applications-azure-and-correspondence

For those that don’t know, Correspondence is Michael’s innovative collaboration framework for occasionally connected smart clients and web applications. Correspondence is hosted on CodePlex at http://correspondence.codeplex.com/ and samples can be found on Michaels blog at http://qedcode.com.

Development, Featured, Product Announcements, Windows, Windows 8

Windows 8 Store is Open for Business

Starting immediately, Windows Store app submission is open to all registered Windows Store developers including individuals and companies. Join the excitement and become part of the vibrant community of app and game developers for Windows 8.

I am starting tonight to migrate a few of the Windows Phone apps I’ve already built, as well as a few ideas I have for some new content, like the Mastermind Game I’ve been playing around with.  My teammate Jared Bienz has done a lot of work around migrating from Windows Phone to Windows 8 – his blog posts on the subject would be valuable to all Windows 8 developers who have spent any time with Windows Phone.

So – create your Store developer account today and start submitint your apps and games at https://appdev.microsoft.com/StorePortals

Registration is quick and easy, so go do it!

Azure, Cloud, Dallas, Development, Events, Featured, Open Source, Students, Texas, Training, user group, Windows 8, WP7

Upcoming User Group Meetings

There are a couple of great-sounding User Group meetings coming up this week that I thought you all might be interested in:

Windows Phone App Developer Group – DFW Dallas TexasSeptember AT&T Mobile App Hackathon Education (Dallas)Friday, 5:00 PM Sep 14
Dallas PHPAn Illustrated Git PrimerTuesday, 7:00 PM Sep 11
DFW C++ User Group, meeting at NCR’s officeLeak Free C++Tuesday, 6:00 PM Sep 11

If you a new to Git, I highly recommend attending the PHP User Group’s presentation. The speaker and I have had numerous conversations about Git, and he does a really good job of explaining the basics. I was going to go and try to ask some of my newer questions about different workflow strategies, branching and such, but a prior family obligation is going to keep me from attending any groups on Tuesday night.

If you’re a Windows Phone or Windows 8 developer, I’ll also recommend the AT&T Mobile Hackathon. I’ve participated in these before and they are very well run by the gang over at AT&T and I know you’ll have an amazing time participating. The prizes are usually pretty good too, so bring your great idea and be ready to crank out some code!

These are just a couple that popped into my Inbox this morning, but there are plenty of other amazing activities to be found around TX, OK, AR and LA – just check http://southcentralcommunity.com and http://communitymegaphone.com for more details.

Cloud, Development, Featured, MVVM, Windows, Windows 8

Windows 8 Games and Roaming Data

I was recently asked to write an article for our upcoming 30-to-Launch series about using RoaminData in a Windows 8 game. Here’s that article and a link to the code.

Every game is unique, and carries with it a unique set of game data to save and load at various points of the games lifecycle. In the past, most gamers – especially casual gamers – had only one device on which their favorite game was installed. Data storage for those games was simpler for developers to manage in that the game data could be saved locally in a variety of formats, and loaded when the game started or at the user’s request, and was all self-contained within the game itself. Today, with people owning multiple devices both at work and at home, game experiences are more challenging to manage as users want to be able to share game data between different computers without having to store that data on some external device like a thumb drive or portable hard drive. Although there are a variety of custom code solutions that could be developed to support the sharing of game data between computers, WinRT provides a set of classes that facilitates this type of data sharing automagically – it’s called the Windows 8 Roaming Data Store. In this article, we’ll look at a simple game implemented for WinRT that uses the Roaming Data Store that enables a player to stop gameplay on one machine and resume it on a second.

Enter the Game

For this article, we will be using a very basic WinRT game based on the popular game Bulls and Cows, often referred to as Mastermind, to illustrate how game data can be saved and shared using the different features of the Roaming Application Data Store in WinRT.

Figure 1 – Sample Mastermind Game

Gameplay is very straightforward – the computer draws a random set of 4 colored boxes from a set of 6 different color choices to form a “code”. The player, often referred to as the code breaker”, provides a series of “guesses” to the solution by selecting a combination of 4 colors via the large buttons on the bottom that the game engine will test against the solution. The player will receive one red indicator for each color the player selects that represents the proper color at the proper position in the solution, and one white indicator for each color the player selects that is part of the solution but NOT in the proper location. In the sample above, the player was able to solve the game in 7 tries using deductive reasoning based on the red and white indicators for each of their guesses. For our game, we want to create an environment where the user can save off their current game state, and restore it on a different computer at some time in the future. To understand how to achieve this, you first have to understand how the Roaming Application Data Store works on WinRT.

Roaming Application Data Store on WinRT

The Roaming Application Data Store is part of the larger WinRT Application Data Store that provides data synchronization between computers sharing a common application. The data is synchronized through the use of a shared cloud storage area attached to a user’s Microsoft account.

Figure 2 – Synchronizing Data via the Roaming Data settings

The Application Data Store is comprised of three separate data storage areas:

  • Local Settings – data that is stored and scoped locally to this single instance of an installed application
  • Roaming Settings – data that is shared between devices on which the user has installed a particular application
  • Temporary Settings – transient data created by the application, stored and scoped locally to a single machine, and could be deleted by the OS at any time

Each of these three data stores can be further broken down into App Files and App Settings.

App Settings

App Settings are stored in the registry, exposed to the developer as a set of simple Key/Value pairs into which any valid WinRT data type can be stored (if binary types are needed, the developer must use App Files instead of App Settings). App Settings support the concept of Containers, into which sets of related App Settings can be organized and stored together. A default container is used when none is explicitly specified, or developers can create their own containers up to 32 levels deep to better organize their application settings. Composite Settings can also be used to support atomic updates of related settings to ensure data consistency during concurrent access and roaming.

App Files

The Files interface gives the developer a way to store serialized objects, data files, or other complex objects using the familiar File patterns. Under the covers, App Files are stored on the file system in a hidden folder on the user’s computer, located beneath the application’s installation directory. Files can be listed, created, deleted and updated using the classes in the System.IO namespace.

Both App Settings and App Files are automatically synchronized between machines on which the application is installed. By simply writing to the App Settings or App Files within the Roaming Data store, data synchronization is automatic.

Using the Roaming Application Data Store in the game

NOTE: You can download the code sample that goes with this post from CodePlex at the following URL:

http://win8roamingstorage.codeplex.com/

The first step to incorporating roaming data was to get the game up and running with all the basic features and decide on what bits of state I needed to save. I wanted to make sure that I had something simple to serialize and deserialize from storage, and I also wanted to experiment with both App Settings and App Files to see which ones worked better for different situations.

Saving Game State

Since my game logic was stored in a separate set of classes from the application UI, and I had used the MVVM pattern for managing application state for data binding, I had everything I needed in one place – the ViewModel. I was tempted to just save off the entire MainViewModel itself – all in one chunk – but decided instead to save off pieces of it to save space (see Observations: Quota below). My MainViewModel does a lot more than just hold state, so I selected just the bits I needed to persist into roaming storage. So, after every move that was submitted by the player, I called out to the following method:

private async void SaveGameState()
{
	StorageHelper.SetGameInProgress();
	await StorageHelper.SaveObjectToRoamingFolder(STR_Gamejson, _game);
	await StorageHelper.SaveObjectToRoamingFolder(STR_Movesjson, Moves);
	StorageHelper.PutObjectToSetting<string>("MoveSlotOne", MoveSlotOne);
	StorageHelper.PutObjectToSetting<string>("MoveSlotTwo", MoveSlotTwo);
	StorageHelper.PutObjectToSetting<string>("MoveSlotThree", MoveSlotThree);
	StorageHelper.PutObjectToSetting<string>("MoveSlotFour", MoveSlotFour);
}

This method uses a helper class I created to save App Files and App Settings into the roaming data store.

First, we tackle saving a file to the Roaming Folder. I’ve got two examples in my source code for this – first is the Game object that tracks the current state of the game including the current solution, history of moves, number of moves remaining, etc. The second is the PlayerMoveViewModel objects stored in the Moves collection. These are wrappers around the GameMove objects that contain UI-friendly representations of the Move data as well as the corresponding MoveResults (i.e. # of white and red indicators). The method used here is SaveObjectToRoamingFolder on my StorageHelper class:

public async static void SaveObjectToRoamingFolder(string filename, object o)
{
	var appData = ApplicationData.Current;
	string jsonData = await JsonConvert.SerializeObjectAsync(o);
	StorageFile sampleFile = await appData.RoamingFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	await FileIO.WriteTextAsync(sampleFile, jsonData);
}

I’m using Json.NET for my JSON serialization needs as I’ve used it before, it’s fast, and easy to install via NuGet

. By using JSON, I minimize the size of each stored file to approx. 1KB for both the Moves collection and the Game object itself.

Next we tackle the four buttons the player was using to enter their guesses. I still feel that storing these settings are not specifically required from a usability perspective, but I thought it would add a nice touch to the game to have the board look exactly as it had before the player left it on their previous machine. To store these four settings, I decided to use the App Settings data store to see how that would work out. I extended my helper class with the PutObjectToSetting<T>() method to make this easy:

public static void PutObjectToSetting<T>(string key, T value)
{
	var appData = ApplicationData.Current;
	appData.RoamingSettings.Values[key] = value;
}

Short and sweet, this method simply attaches to the current RoamingSettings object and inserts a value according to a specific key if none exists, or updates the value of that key if already present. I’ve recently learned that people are calling this an “upsert”, although I’m not sure how I feel about that term 🙂

Loading Game State

Saving the game state was really easy – after every move, the game would save off the current state of the board and the 4 input buttons to the Roaming Store. Retrieving the data was a bit more challenging as I had identified 3 use cases for loading data:

  • Restarting the game on the local machine
  • Restarting the game on the secondary machine
  • Updating the currently running game on the local machine with data from the secondary machine.

The first two use cases are the primary use cases I identified for this game. I wanted to make sure that I could effectually stop the game on the local machine and restore it back to where I came from on the remote machine. I also wanted to consider the situation where I might pause a game on one machine and pick it up on another, only to come back to the first machine later. This was a bit more work, but ultimately boiled down to some additional code at launch time, and an event to register for while the app was running.

MVVM is a great pattern for separating UI logic from Application logic, but sometimes – as in the case of initially launching the application, there can be some overlap. My sample application uses the MvvmLight Preview library from Laurent Bugnion at GalaSoft (also available via a convenient NuGet package) to make this easier to manage. When my application launches, I use the MvvmLight Messenger object to send a notification to the UI that the user needs to be asked if they want to load the existing game, or start a new one when saved game state is found. I didn’t want to do this in the UI, which would have simplified the code, as I felt it broke the MVVM pattern and I wanted to keep to the pattern as much as possible.

So, in the constructor for the MainViewModel object, I register a listener for the GameBoardReadyMessage to check for existing game state and alert the UI if necessary. Otherwise, we trigger the beginning of a new game and let the user start entering data.

Messenger.Default.Register<GameBoardReadyMessage>(this, (message) =>
{
	if (StorageHelper.GameInProgress)
	{
		var msg = new AskForGameRestoreMessage();
		Messenger.Default.Send<AskForGameRestoreMessage>(msg);
	}
	else
	{
		StartNewGame();
	}
});

When the Main Page launches, it send a GameBoardReadyMessage when it’s ready to be loaded, and registers to listen for AskForGameRestoreMessage messages to arrive from the MainViewModel. When AskForGameRestoreMessage is triggered, the code-behind in the MainPage will prompt the user to confirm that they want to load the existing game (LoadSavedGameMessage) or start a new one (StartNewGameMessage). Depending on the answer, additional messages are fired to alert the ViewModel of the user’s selection. The path we want to follow here is the LoadSavedGameMessage path in the MainViewModel, as implemented in the LoadSavedGame method:

private async void LoadSavedGame(ApplicationData appData)
{
	try
	{
		CurrentGame = await StorageHelper.GetObjectFromRoamingFolder<Game>(appData, STR_Gamejson);
		CurrentGame.OnFailure += _game_OnFailure;
		CurrentGame.OnVictory += _game_OnVictory;
		Moves.Clear();
		var moves = await StorageHelper.GetObjectFromRoamingFolder<ObservableCollection<PlayerMoveViewModel>&t;(appData, STR_Movesjson);

		foreach (var move in moves)
		{
			Moves.Add(move);
		}

		MoveSlotOne = StorageHelper.GetObjectFromSetting<string>(appData, "MoveSlotOne");
		MoveSlotTwo = StorageHelper.GetObjectFromSetting<string>(appData, "MoveSlotTwo");
		MoveSlotThree = StorageHelper.GetObjectFromSetting<string>(appData, "MoveSlotThree");
		MoveSlotFour = StorageHelper.GetObjectFromSetting<string>(appData, "MoveSlotFour");
	}
	catch (Exception ex)
	{
		// show error message
		ClearSavedGameState();
		Messenger.Default.Send<ErrorLoadingGameMessage>(new ErrorLoadingGameMessage(ex));
		// start new game
		StartNewGame();
	}
}

In this code sample, we’re attempting to pull out all of the saved game state from both App Files and App Settings and reconstitute the game in progress. If anything goes wrong, we let the user know via an alert and just start a new game. The methods I added to the StorageHelper class do the reverse of the work we did to save the object to storage, and instead pull the data back out:

public async static Task<T> GetObjectFromRoamingFolder<T>(ApplicationData appData, string filename)
{
	StorageFile sampleFile = await appData.RoamingFolder.GetFileAsync(filename);
	string jsonData = await FileIO.ReadTextAsync(sampleFile);
	var o = await JsonConvert.DeserializeObjectAsync<T>(jsonData);
	return o;
}

GetObjectFromRoamingFolder<T> takes the data from the Roaming Store and uses Json.NET to turn that data back into the object of our choice (note that this method has to be marked as async because the underlying access to the RoamingFolder requires await).

public static T GetObjectFromSetting<T>(ApplicationData appData, string setting)
{
	var raw = appData.RoamingSettings.Values[setting];
	T obj = (T)raw;
	return obj;
}

Lastly, the GetObjectFromSetting<T>() method pulls back a value from the RoamingSettings collection and casts it into the proper type before returning.

Other things to consider

There are a few specifics related to the Roaming Data store that need to be considered before using its capabilities in your application. I didn’t account for all of them in my sample, but I wanted to list the more important ones here for you to consider when implementing Roaming Data in your game:

Quotas

Unlike data stored in the Local Data area, Roaming Data is governed by a storage quota

, as defined by the RoamingStorageQuota property of the ApplicationData object. This quota provides a maximum size for data that can be synchronized in the cloud via the Roaming Data store. As of this writing, the current RoamingStoreQuota value is 100KB. If this limit is exceeded, the application will continue to run but the data will NOT be synchronized to the cloud for access by other installations. For game data, this is an important consideration as you design your game. You’ll need to optimize the amount of data that is stored, and the serialization formats used, to ensure that you stay within this limit or risk shutting down the automatic synchronization.

Versioning

Games, just like traditional applications, evolve over time to take on new features, fix bugs, and improve gameplay. You should consider a versioning strategy for your Roaming App data just like you’d consider a versioning strategy for the game itself. The ApplicationData class provides a Version property as well as a SetVersionAsync() method to allow you to set a version on your data, and check in when loading the application data. If the version number of the stored data is less than the version your application is expecting, the app can either reject the saved game state in favor of a new game, or you can provide an update strategy to convert the old saved game data to the new format. Current guidance is to use sequential and incrementing version numbers, starting at 1, for your Application and ApplicationData versioning needs.

DataChanged event

Depending on how your game is intended to be used, there might be a situation where there is more than one concurrently running instance of your game. In this situation, the data synchronized via the cloud could be different or even older, than the data you’re working with locally. This could create a data collision of sorts, where the application will need to make a decision on how to proceed. To aid you in dealing with these collisions, the Roaming Data store provides a simple event to which you can connect an event handler to deal with this situation – the DataChanged event:

public event TypedEventHandler<ApplicationData, Object> DataChanged

By subscribing to this event, your application will be notified when the game state on another computer has changed, and you can respond accordingly. In my sample application, I respond to this event by calling into LoadSavedGame, passing in the provided ApplicationData object to refresh the game state and the UI.

Please note: The DataChanged event is NOT intended to be used to facilitate player moves in a turn based game. Not only is the game data synchronized for a single Microsoft account, the rate at which the updates occurs is not guaranteed to be timely enough for acceptable game play. There are other techniques, such as Sockets, that provide a much better alternative to creating networked turn-based games and applications.

Summary

This was a really fun project for me to do. I had never written a game at all before, in any shape or form, so I was concerned about that aspect of it as much as the strategies I’d use for loading and storing game state. I’ve posted all my code on CodePlex at the following URL for you to download and inspect at your leisure:

http://win8roamingstorage.codeplex.com/

Please keep in mind that this is not a “complete” game, in that it is not something I’d consider ready for the Windows Store (at least not yet) but it will be fun to continue working on it to get there.

More detail about the Roaming Application Settings, and the ApplicationData object can be found all over the web. Here are a list of additional articles, references and code samples to help you get started.

Development, Open Source, Windows, Windows 8, WP7, WP8

Starter Kit News

Couple of quick announcements about the Starter Kits I’ve been working on over the past year:

First, I just made an update tonight to the Windows Phone Starter Kit for Schools (a.k.a. the Windows Phone Starter Kits for Schools, Organizations, Teams, Clubs, and Just About Everything Else). Since Google recently decided to discontinue their Weather API, I needed to find another source. I was using Bing originally, but the Bing API requires developers to pay a subscription fee to access the API after the first 5,000 transactions. Instead, today I switched over to a free Weather API from http://www.worldweatheronline.com. World Weather Online allows for developers to get a free account providing current conditions as well as (up to) a 5-day forecast.  For this starter kit, that would fit perfectly! I made the updates tonight in both the GitHub repository as well as the MSDN Code Gallery so you can get it from either place.

Second, I’ve been getting a LOT of questions about Starter Kits for Windows 8. Many people have asked if there are any plans to create Win8 versions of all the existing Starter Kits. So far, there has not been any plans, but I think I might be persuaded to start working on some, if I could find some interested community members to work with me.  The primary ones to move over would be the Starter Kit for Schools and the Starter Kit for RSS, as those are the 2 most popular Starter Kits of the bunch. There are quite a few bits that would need to change considering the new UX with Windows 8 Store Apps, but I think it’s perfectly manageable in a relatively short period of time. After all – these are starter kits/sample code – not completed applications. There is every expectation that people will need to make additional customizations as well as UI updates before submitting the applications for publication. If you are interested in helping out with this, please let me know and we can get to work straight away! There might even be a prize in it for you for getting the code completed and published 🙂

Azure, Cloud, Development, Windows 8

New Azure Stuff – WAMS and WATK

Hey – I’ve been travelling and presenting this week, so the Azure team decided it would be a good time to make a bunch of udpates!

Here’s a quick overview of what’s new, and where you can find more information:

Windows Azure Mobile Services

Windows Azure Mobile Services is a fantastic new feature added to the Windows Azure pantheon of services to provide an easy way to integrate a Windows Azure backend to new or existing Windows 8 clients.  Using the WAMS SDK, a developer can easily create a Windows 8 application that incorporates a Windows Azure SQL Database backend, WPNS and integrated Authentication & Authorization in a matter of a few minutes. The WAMS SDK comes with an upgrade to the Windows Azure Portal allowing the developer to configure the WAMS application’s Azure resources including SQL Database, Push and Authn/AuthZ API keys. Really simple to use and abstracts away all the complexities of connecting to a SQL Database instance from a Win8 app.  Definitely check this out!

  • Log into your Windows Azure account at https://account.windowsazure.com
  • Click on the “Preview Features” tab on the secondary navigation, just below the main nagvigation
  • Scroll down until you see the Mobile Services section and click “Sign Up”.

Activation doesn’t usually take too long, so you should be up and running in no time!  Once you’re activated, step through the WAMS tutorial on the Windows Azure website to get a full view of everything WAMS provides. This is going to be a big help to you Windows 8 developers out there, so get cracking!

Windows Azure Training Kit

A new version of the Windows Azure Training Kit dropped on Wednesday of last week.  The new kit is represented as the August 2012 Refresh, and contains some important updates including

  • Added the Windows Azure Device + Services 1-day event agenda
  • Added the Windows Azure ITProCamp 1-day event agenda
  • Added 2 presentations designed for Windows Azure Mobile Services
  • Added 15 Demos with Demo Scripts for content delivery

You can download the installer, or visit their project on GitHub to get just the pieces you want.

 

BootCamp, Dallas, Development, Events, Texas, Training, Windows, Windows 8

Windows 8 DevCamps

Get the lowdown on Windows 8 and start coding your dream application. Join us for these free learning sessions and see firsthand how Windows 8 is the largest developer opportunity – ever.

  • Day One is a top-to-bottom Windows 8 Release Preview tour, followed by an InstallFest to prepare your system for hands-on development.
  • Day Two is the all-out Hackathon. Code to your heart’s content with Windows 8 experts available to help and answer your questions. You could even win cool prizes for your app.

Attend just one day or join us for both sessions. It’s your choice, so register today!

Events are taking place in over 25 cities nationwide, including:

Location

 DevCamp

 Hackathon

Manhattan, NY

7/14

7/15

St Louis, MO

7/16

7/17

Brooklyn, NY

7/19

7/20

Nashville, TN

7/19

7/20

Rochester, NY

7/27

7/28

Mountain View, CA

7/27

7/28

Atlanta, GA

8/3

8/4

Ft. Lauderdale, FL

8/3

8/4

Redmond, WA

8/3

8/4

Dallas, TX

8/7

8/8

Chevy Chase, MD

8/10

8/11

Denver, CO

8/10

8/11

Irvine, CA

8/17

8/18

Boston, MA

8/17

8/18

Raleigh, NC

8/17

8/18

Reston, VA

8/17

8/18

Orlando, FL

8/17

8/18

Minneapolis, MN

8/23

8/24

Houston, TX

8/24

8/25

San Francisco, CA

8/24

8/25

Downers Grove, IL

8/28

8/29

Phoenix, AZ

9/7

9/8

Los Angeles, CA

9/14

9/15

Malvern, PA

9/14

9/15

 

Sign up now before all the seats are taken!

Dallas, Development, Events, Texas, Training, Windows 8

Windows 8 Hackathon

Have you started building your Windows 8 app? Does this describe you?

  • Installed the Windows 8 Consumer Preview or Windows 8 Release preview?
  • Installed Visual Studio 11 Express for Windows 8?
  • Started writing your Windows 8 Metro application?

If so, we are running this Windows 8 app building event on June 26th at the Microsoft Office in Irving, with the goal being to help you make progress on, refine or even finish your Windows 8 Metro application. This is not a training event, but rather an event focused on helping you make progress on your Windows 8 Metro application (please see the prerequisites below).

So join us for this free, full-day event designed to help you with your Windows 8 coding and application building. We can help with your apps integration with the Windows shell, including notifications, live tiles, deep links, and contracts with other apps. Best of all you can build once and support all Windows customers, no matter what type of PC they have – from tablets to laptops to convertibles to desktops.

Prerequisites:

These hackathons are a really fun way to get “down and dirty” with the technology and experience development along side others in the same room. It is also a great chance to have your application reviewed by someone who understands the Windows Metro environment and can help make sure that your app has the best possible chance to get published. I had my own app reviewed yesterday and I can personally testify that this kind of review really helped me maximize my time so that my submissions have a much higher chance to get accepted on the first try.

So – are you ready to take on Windows 8?  If so – sign up today!