Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Game Attack Combos : WPF Hybrid Smart Client for Combo Calculations

, 23 May 2009
A WPF hybrid smart client for calculating attack combos in the Prince of Persia game.
Prize winner in Competition "Smart Client Article Contest"
GameAttackCombos_Src.zip
ComboServices
App_Data
ComboPackagesService.svc
GameAttackCombos.snk
Properties
GameAttackCombos.mdf
GameAttackCombos_log.LDF
Packages
PrinceOfPersia2008.gcp
PrinceOfPersia2010.gcp
PackageComboFiles
Properties
ServiceData
GameAttackCombosModel.edmx
Properties
Assets
Button Icons.design
Game Attack Combos Icon.design
Game Attack Combos Icon.ico
Prince of Persia 2008
Prince of Persia 2010
PrinceOfPersia2010Background.png
PrinceOfPersia2010Icon.png
PrinceOfPersia2008Background.png
PrinceOfPersia2008Icon.png
ComboClient
Game Attack Combos Icon.ico
Properties
Resources
Service References
Supporting
Themes
ComboServices
ComboPackagesService.disco
ComboPackagesService.wsdl
ComboPackagesService1.wsdl
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
app.manifest
ComboLogic
Properties
GameAttackCombos_Standalone.zip
Packages
PrinceOfPersia2008.gcp
PrinceOfPersia2010.gcp
Resources
using System;
using System.IO;
using System.Linq;
using System.ServiceModel.Activation;
using System.Web.Hosting;
using GG.GameAttackCombos.Data;
using GG.GameAttackCombos.Logic;

namespace GG.GameAttackCombos.Services {

	/// <summary>
	/// The service for requesting game attack combo packages for download.
	/// </summary>
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class ComboPackagesService : IComboPackagesService {

		/// <summary>
		/// Requests a download of a combo package for the specified game guide code, if there is a
		/// new version available.
		/// </summary>
		/// <param name="gameCode">A unique code to identify a known published game disc.</param>
		/// <param name="clientPackageVersion">
		/// The version the client has for the requested combo package. If a new
		/// version of the combo package exists on the server, it is returned; otherwise,
		/// nothing is returned to the client (i.e. the client's file is up-to-date).
		/// </param>
		/// <returns>A byte array containing the requested combo package.</returns>
		public byte[] DownloadComboPackage(string gameCode, string clientPackageVersion) {
			// Prepare a binary data array.
			byte[] FileData = null;

			// Parse the client version.
			Version ClientVersion = null;
			if (!string.IsNullOrEmpty(clientPackageVersion) && ComboPackage.ValidatePackageVersion(clientPackageVersion)) {
				ClientVersion = new Version(clientPackageVersion);
			} else {
				ClientVersion = new Version(0, 0);
			}

			// Get the file name for the specified code.
			string PackageFileName = null;
			if (!string.IsNullOrEmpty(gameCode)) {
				PackageFileName = GetComboPackageFileNameByGameCode(gameCode);
			}
			if (!string.IsNullOrEmpty(PackageFileName)) {
				// Build the full path to the package file.
				string ComboPackagesPath = HostingEnvironment.MapPath(Settings.ComboPackagesVirtualPath);
				PackageFileName = Path.Combine(ComboPackagesPath, PackageFileName);

				// Read the entire file into the array.
				using (FileStream PackageFile = File.Open(PackageFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
					// Open the package to get its version.
					Version CurrentVersion = null;
					using (ComboPackage Package = new ComboPackage(PackageFile)) {
						CurrentVersion = new Version(Package.Version);
					}

					// Check the version of the combo package file against the current one specified.
					if (CurrentVersion > ClientVersion) {
						// Copy the combo package file to the data array.
						FileData = StreamHelper.CopyStreamToArray(PackageFile);
					}
				}
			}

			// Return the file data.
			return FileData;
		}

		/// <summary>
		/// Gets the combo package file name for a matching specified game code.
		/// </summary>
		/// <param name="gameCode">A game code to lookup.</param>
		/// <returns></returns>
		private string GetComboPackageFileNameByGameCode(string gameCode) {
			string ComboPackageFileName = null;

			// Lookup the game code.
			using (GameAttackCombosEntities DataContext = new GameAttackCombosEntities()) {
				// Query for the corresponding game for a game disc with a matching code.
				var Query = from d in DataContext.GameDisc
							where d.GameDiscCode == gameCode
							select d.Game;

				// There should be only one result.
				Game Game = Query.FirstOrDefault();
				if (Game != null) {
					ComboPackageFileName = Game.ComboPackageFileName;
				}
			}

			return ComboPackageFileName;
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matt Sollars
Web Developer
United States United States
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.
 
The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 23 May 2009
Article Copyright 2009 by Matt Sollars
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid