diff api/Factories/Xml/WarFoundryXmlSaver.cs @ 108:2060f23abee9

Re #53: Create XML saver * Add saving of unit equipment * Add extra property to DTD to indicate whether equipment amount is fixed or a ratio
author IBBoard <dev@ibboard.co.uk>
date Fri, 21 Aug 2009 20:12:27 +0000
parents c4ee96a91018
children 45f1db6356e9
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlSaver.cs	Fri Aug 21 19:51:24 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlSaver.cs	Fri Aug 21 20:12:27 2009 +0000
@@ -43,6 +43,18 @@
 
 		private string CreateXmlString(WarFoundryObject toSave)
 		{
+			string xmlString = "";
+
+			if (toSave is Army)
+			{
+				xmlString = CreateArmyXmlString((Army)toSave);
+			}
+
+			return xmlString;
+		}
+
+		private string CreateArmyXmlString(Army toSave)
+		{
 			/*
 <army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
 	<units>
@@ -65,18 +77,6 @@
 	</units>
 </army>
 */
-			string xmlString = "";
-
-			if (toSave is Army)
-			{
-				xmlString = CreateArmyXmlString((Army)toSave);
-			}
-
-			return xmlString;
-		}
-
-		private string CreateArmyXmlString(Army toSave)
-		{
 			XmlDocument doc = new XmlDocument();
 			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
 			doc.AppendChild(declaration);
@@ -96,21 +96,62 @@
 			
 			foreach (Unit unit in toSave.GetUnits())
 			{
-				XmlElement unitElem = doc.CreateElement("unit");
-				unitElem.SetAttribute("id", unit.ID);
-				unitElem.SetAttribute("unitName", unit.Name);
-				unitElem.SetAttribute("unitType", unit.UnitType.ID);
-				unitElem.SetAttribute("size", unit.Size.ToString());
+				units.AppendChild(CreateUnitElement(unit, doc));
 				
-				if (!unit.Race.Equals(toSave.Race))
-				{
-					unitElem.SetAttribute("race", unit.Race.ID);
-				}
-				
-				units.AppendChild(unitElem);
 			}
 			
 			return doc.OuterXml;
 		}
+
+		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
+		{
+			XmlElement unitElem = doc.CreateElement("unit");
+			unitElem.SetAttribute("id", unit.ID);
+			unitElem.SetAttribute("unitName", unit.Name);
+			unitElem.SetAttribute("unitType", unit.UnitType.ID);
+			unitElem.SetAttribute("size", unit.Size.ToString());
+			
+			if (!unit.Race.Equals(unit.Army.Race))
+			{
+				unitElem.SetAttribute("race", unit.Race.ID);
+			}
+
+			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
+
+			if (equipmentElem != null)
+			{
+				unitElem.AppendChild(equipmentElem);
+			}
+			
+			return unitElem;
+		}
+
+		private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
+		{
+			UnitEquipmentItem[] equipItems = unit.GetEquipment();
+			int equipItemCount = equipItems.Length;
+			XmlElement equipmentElem = null;
+
+			if (equipItemCount > 0)
+			{
+				equipmentElem = doc.CreateElement("equipment");
+				
+				for (int i = 0; i < equipItemCount; i++)
+				{
+					equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
+				}
+			}
+
+			return equipmentElem;
+		}
+
+		private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
+		{
+			XmlElement equipmentItemElem = doc.CreateElement("equipItem");
+			equipmentItemElem.SetAttribute("id", item.EquipmentItemID);
+			equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
+			equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
+			return equipmentItemElem;
+		}
 	}
 }