changeset 47:85f2b9c3609c

Re #13 - Use XPath for file loading * Replace loading of race unit types, categories and equipment with XPath method * Add loading of abilities using XPath Also: * Add implementation for Ability * Add AddAbility method to Race * Alter some logging so that "for ID" is followed by ID not name
author IBBoard <dev@ibboard.co.uk>
date Thu, 26 Mar 2009 20:49:42 +0000
parents a5855fcd75ab
children b49372dd8afa
files api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/Ability.cs api/Objects/Race.cs
diffstat 3 files changed, 56 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Mon Mar 23 20:57:07 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Thu Mar 26 20:49:42 2009 +0000
@@ -203,8 +203,8 @@
 			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats");
 			LoadSystemStatsForSystem(system, elem);
 			system.StandardSystemStatsID = defaultStatsID;
-			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
-			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID);
+			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
+			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
 			system.SetAsFullyLoaded();
 		}
 		
@@ -244,40 +244,32 @@
 			race.SetAsLoading();
 			
 			XmlNode elem = GetExtraData(race);
-			XmlNode colNode = elem.FirstChild;
-						
-			foreach (XmlElement node in colNode.ChildNodes)
+			
+			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:units/race:unit"))
 			{
 				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
 				race.AddUnitType(type);
 			}
 			
-			colNode = colNode.NextSibling;
-			
-			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
+			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:categories/cat:cat"))
 			{
-				foreach (XmlElement node in colNode.ChildNodes)
-				{
-					race.AddCategory(CreateCategoryFromElement(node));
-				}
-				
-				colNode = colNode.NextSibling;
+				race.AddCategory(CreateCategoryFromElement(node));
 			}
 							
-			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
+			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:equipment/cat:equipmentItem"))
 			{
-				foreach (XmlElement node in colNode.ChildNodes)
-				{
-					EquipmentItem item = CreateEquipmentItemFromElement(node, race);
-					race.AddEquipmentItem(item);
-				}
+				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
+				race.AddEquipmentItem(item);
+			}
+							
+			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:abilities/cat:ability"))
+			{
+				Ability ability = CreateAbilityFromElement(node, race);
+				race.AddAbility(ability);
 			}
 			
-			Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
-			//TODO: Load abilities
-			race.SetAbilities(raceAbilities);
 			race.SetAsFullyLoaded();
-			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
+			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
 		}
 		
 		protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
@@ -287,7 +279,7 @@
 			
 			try
 			{
-				doc.Load(reader);			
+				doc.Load(reader);
 			}
 			//Don't catch XMLSchemaExceptions - let them get thrown out
 			finally
@@ -549,5 +541,15 @@
 			
 			return new EquipmentItem(id, name, cost, min, max, armourType, race);
 		}
+		
+		private Ability CreateAbilityFromElement(XmlElement elem, Race race)
+		{
+			string id = elem.GetAttribute("id");
+			string name = elem.GetAttribute("name");
+			Ability ability = new Ability(id, name);
+			XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager());
+			ability.Description = (node == null) ? "" : node.InnerText;
+			return ability;
+		}
 	}
 }
\ No newline at end of file
--- a/api/Objects/Ability.cs	Mon Mar 23 20:57:07 2009 +0000
+++ b/api/Objects/Ability.cs	Thu Mar 26 20:49:42 2009 +0000
@@ -6,10 +6,27 @@
 
 namespace IBBoard.WarFoundry.API.Objects
 {
+	/// <summary>
+	/// An Ability is a special rule that a UnitType has, made up of an ability name and a description.
+	/// </summary>
 	public class Ability : WarFoundryObject
 	{
-		public Ability()
+		private string description;
+		
+		public Ability(String id, String name) : base(id, name)
+		{
+		}
+		
+		public string Description
 		{
+			get { return description; }
+			set
+			{
+				if (value!=null)
+				{
+					description = value.Trim();
+				}
+			}
 		}
 	}
 }
--- a/api/Objects/Race.cs	Mon Mar 23 20:57:07 2009 +0000
+++ b/api/Objects/Race.cs	Thu Mar 26 20:49:42 2009 +0000
@@ -238,9 +238,19 @@
 			return items;
 		}
 		
+		public void AddAbility(Ability newAbility)
+		{
+			//TODO: Throw DuplicateItemException
+			abilities.Add(newAbility.ID, newAbility);
+		}
+
+		[Obsolete("Use AddAbility method instead")]
 		public void SetAbilities(Dictionary<string, Ability> newAbilities)
 		{
-			abilities = newAbilities;
+			foreach (Ability ability in newAbilities.Values)
+			{
+				AddAbility(ability);
+			}
 		}
 				
 		public Ability GetAbility(string id)