ToXml Extension Method in .NET 3.5

by Cameron Albert 27. February 2008 22:07

I built the following extension method in .NET 35. to XML serialize objects; thought it might be useful for someone else. I use to serialize collections to pass XML data to my stored procedures that save player skills and items in my PBBG engine.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace WebTools
{
public static class XmlHelper
    {
public static string ToXml(this object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, obj);
return sw.ToString();
}
}
}
}

I wrote another extension method specific to retrieving the modified values of a collection. You have to control when the IsDirty flag is set yourself but my properties take care of that.

public interface IModifiable
    {
bool IsDirty { get; }
void MarkClean();
void MarkDirty();
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebTools
{
public static class ModifierHelpercs
    {
public static List<TSource> Modified<TSource>(this IEnumerable<TSource> source) where TSource : IModifiable
        {
return (from s in source where s.IsDirty select s).ToList();
}
}
}

Tags: , , , ,

ASP.NET Development | Game Development | General

Passing Lists to SQL Server 2005 using XML

by Cameron Albert 31. October 2007 14:44
Jon Galloway wrote up a nice article on passing lists in a stored procedure using XML and SQL 2005's ability to query XML. For anyone who ever wanted to pass an array to a stored procedure this a good read. :)

Tags: , , ,

ASP.NET Development | General

Engine Structure

by Cameron Albert 30. October 2007 00:05

I am structuring my PBBG game engine to be as flexible as possible in order to build various types of games. In order to do that I need to abstract out the components of the engine. Since persistent browser based games are, well, browser based, I decided to follow the normal n-tier model. I am creating a data tier, my actuall database, an application tier which is the engine and will handle client connections, authentication, commands and reading and writing to the database. On top of the application layer will reside the game layer which will be customizable libraries that will use and access the application layer. This follows along the MUD driver and MUD lib pattern where my engine will be the driver which will persist data and handle all communications and my MUD libs or games will be written in an OO fashion to take advantage of the game engine.

The engine is being written in C# and in such a way to take advantage of features of ASP.NET such as HttpModules and HttpHandlers. 

.NET PBBG Engine

by Cameron Albert 17. October 2007 23:48

In between updating Perenthia and adding new features I have been pulling parts of the code base into a more generic PBBG Engine I am writing in C#. I started working on it when I upgraded the Knights of the Realm game and used parts of it for Perenthia. I am hoping to put Knights of the Realm Beta 2 on the new engine once I get it finished.

I am building the engine as generic as I can but it will incorporate a base rules set and some basic concepts. The base objects will be Avatars, Places and Things. These objects will contain the properties required to function within the rules set and all objects will derive from a base GameObject class that will provide a properties collection for creating custom properties on derived game objects.

The game will be driven by commands sent from the client. Some objects will handle the commands in the engine framework while other commands will cause events to be raised that deriving implementations can handle and provide custom execution or additional execution of the commands.

The egnine will basically be a commands/rules processor that I will hopefully be able to build a variety of games on. I have plans and ideas for several types of games and do not want to continually build the same thing over and over, hence the PBBG engine. 

Silverlight

by Cameron Albert 17. August 2007 15:31
I am starting to look into Silverlight a little more now that I have the VS 2008 Beta 2 and the Expression Blend 2 Preview. I like that in Silverlight 1.1 you can code in C# rather than doing everything in XAML or JavaScript.

Tags: , ,

ASP.NET Development

GridView Grouping and Summaries

by Cameron Albert 18. July 2007 13:49
If you are looking to group your grid view results and provide summaries such as totals, etc. I found a great utility for grouping GridView results. It is a set of classes that provide a helper for your grids to allow grouping. The component is super easy to use just remember that if you don't use a data source control you have to handle the GridView Sorting event. You don't actually have to have any code in the event handler, just handle the event.

Tags: , , ,

ASP.NET Development

X, Y, Z Coordinate Struct for .NET PBBGs

by Cameron Albert 16. July 2007 15:18

I created a struct in C# for storing the X, Y and Z coordinates for characters, objects and rooms within my current PBBG project Perenthia. The struct, called Vector, is serializable and can be used as the key value in a sorted or generic dictionary. Here is the code for the class.

[Serializable]
    public struct Vector : IEquatable<Vector>
    {
        public static readonly Vector Empty = new Vector(0, 0, 0);

        public Vector(int x, int y, int z)
        {
            _x = x;
            _y = y;
            _z = z;
        }

        public void SetLocation(int x, int y)
        {
            this.SetLocation(x, y, this.Z);
        }

        public void SetLocation(int x, int y, int z)
        {
            _x = x;
            _y = y;
            _z = z;
        }

        public Vector Copy()
        {
            return new Vector(this.X, this.Y, this.Z);
        }

        public static Vector FromString(string value)
        {
            if (!String.IsNullOrEmpty(value))
            {
                string[] parts = value.Split(',');
                if (parts != null && parts.Length == 3)
                {
                    int x, y, z;
                    if (Int32.TryParse(parts[0], out x))
                    {
                        if (Int32.TryParse(parts[1], out y))
                        {
                            if (Int32.TryParse(parts[2], out z))
                            {
                                return new Vector(x, y, z);
                            }
                        }
                    }
                }
            }
            return Vector.Empty;
        }

        #region GetHashCode
        public override int GetHashCode()
        {
            // The Y value should always come first in any kind of sorting, comparison or hashing operations
            // followed by X and then Z because typical loops would start with the Y value.
            return (this.Y.GetHashCode() + this.X.GetHashCode() + this.Z.GetHashCode());
        }
        #endregion

        #region Equals
        public bool Equals(Vector obj)
        {
            // The Y value should always come first in any kind of sorting, comparison or hashing operations
            // followed by X and then Z because typical loops would start with the Y value.
            if (obj != null)
            {
                if (obj.Y == this.Y)
                {
                    if (obj.X == this.X)
                    {
                        return (obj.Z == this.Z);
                    }
                }
            }
            return false;
        }

        public override bool Equals(object obj)
        {
            if (obj is Vector)
            {
                return this.Equals((Vector)obj);
            }
            return base.Equals(obj);
        }
        #endregion

        #region ToString
        public override string ToString()
        {
            return String.Format("{0},{1},{2}", _x, _y, _z);
        }
        public string ToString(bool forDisplay)
        {
            if (forDisplay)
            {
                return String.Format("X = {0}, Y = {1}, Z = {2}", _x, _y, _z);
            }
            return this.ToString();
        }
        #endregion

        #region Operators
        public static Vector operator +(Vector v1, Vector v2)
        {
            return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
        }

        public static Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
        }

        public static bool operator ==(Vector v1, Vector v2)
        {
            return v1.Equals(v2);
        }

        public static bool operator !=(Vector v1, Vector v2)
        {
            return (!v1.Equals(v2));
        }

        public static bool operator >=(Vector v1, Vector v2)
        {
            // The Y value should always come first in any kind of sorting, comparison or hashing operations
            // followed by X and then Z because typical loops would start with the Y value.
            if (v1.Y >= v2.Y)
            {
                if (v1.X >= v2.X)
                {
                    return (v1.Z >= v2.Z);
                }
            }
            return false;
        }

        public static bool operator <=(Vector v1, Vector v2)
        {
            // The Y value should always come first in any kind of sorting, comparison or hashing operations
            // followed by X and then Z because typical loops would start with the Y value.
            if (v1.Y <= v2.Y)
            {
                if (v1.X <= v2.X)
                {
                    return (v1.Z <= v2.Z);
                }
            }
            return false;
        }
        #endregion

        #region Properties
        private int _x;

        public int X
        {
            get { return _x; }
            set { _x = value; }
        }

        private int _y;

        public int Y
        {
            get { return _y; }
            set { _y = value; }
        }

        private int _z;

        public int Z
        {
            get { return _z; }
            set { _z = value; }
        }
        #endregion

        #region IEquatable<Vector> Members

        bool IEquatable<Vector>.Equals(Vector other)
        {
            return this.Equals(other);
        }

        #endregion
    }

Tags: , , , ,

ASP.NET Development | Game Development | General

PBBG Game Engine

by Cameron Albert 3. July 2007 10:38

While building the persistent browser based game Perenthia I have been putting together a PBBG Engine that I am going to use to build additional games. Among those additional games will be Aelerion, which will be a space adventure game in which I plan to use Silverlight as the front end user interface.

The Knights of the Realm 2 uses a mixure of some of the code I wrote for the PBBG Engine and some of the code I migrated from the first version of the game. I also chose to try something new with Knights of the Realm in that I stored XML serialized objects in the database using my XQuery class. I decided to try this method to see what kind of impact it would have on game play performance and maintainability. Since Knights of the Realm is not a real time game and executes tournaments in the early hours of the morning I figured this would be a safer test.

Since writing Knights of the Realm 2 I decided not to use the XML serialiazation for my PBBG Engine because I want the engine to be able to support real time game play and serialization is just too slow. I did some perf testing with a database, xml serialization and flat text files and found that flat text files were the fastest when saving information, xml serlization was the slowest and database fell in the middle. When loading information back into objects the database was the fatest because I didn't have to do a lot of conversions from strings to integers like I did with the flat text files. Xml serialization came in last in loading as well and not just in miliseconds but was a good 3-4 seconds behind the database and flat files in both loading and saving. However, using an XmlReader and XmlWriter to read and write the Xml values into an Xml data type column in the database turned out to be faster than serialization but still slower than straight database access because of the need to convert Xml node value strings to integers.

My PBBG Engine will utilize a mixure of regular data type columns and xml columns in the database. The reasoning is that static things such as player name, race and gender will be static columns but the player table will also feature and xml data type column for storing custom properties. A base player object will contain a key/value collection where custom properties can be stored and that collection will use the XmlReader and XmlWriter to save the key/value pairs in the xml data type column.

I also plan on making use of the application cache in ASP.NET for some of the common objects such as rooms so that I am not making thousands of database calls everytime a player moves around in the world. Some of my initial tests have worked out quite well using a lightweight set of objects that represent rooms or tiles on a map. These objects contain just enough information to determine whether or not a player can move into the room and their coordinates in the overall map. 

The Future of Persistent Browser Based Games

by Cameron Albert 30. June 2007 16:59

I've been developing persistent browser based games using ASP.NET and AJAX for a little while now but an emerging technology from Microsoft called Silverlight that I believe will re-define how browser games are built for .NET developers. Silverlight is Microsoft's answer to Flash but with advantages for .NET developers in that I can program C# on the backend and have Silverlight on the front end.

My current PBBG Perenthia will be ASP.NET and AJAX but another game I have in the design phase called Aelerion will feature a Silverlight front end using (hopefully) the same game engine I wrote for Perenthia. 

Tags: , , , , , ,

General

Powered by BlogEngine.NET 1.5.0.7
Modified Theme by Mads Kristensen

About the Author

CameronAlbert.com I am Senior Software Development Consultant specializing in Silverlight, WPF and the Microsoft .NET Framework. 

My current project Perenthia is a Silverlight multi-player game based in a fantasy world that combines text adventure games with some moderate graphics

View Cameron Albert's profile on LinkedIn
See how we're connected

Follow cameronalbert on Twitter

 

Recommended Books

Silverlight 4 Business Application Development - Beginner's Guide:

http://www.packtpub.com/microsoft-silverlight-4-business-application-development-beginners-guide/book

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide