changeset 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
files api/Factories/Xml/WarFoundryXmlSaver.cs dtds/army.xsd
diffstat 2 files changed, 71 insertions(+), 23 deletions(-) [+]
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;
+		}
 	}
 }
--- a/dtds/army.xsd	Fri Aug 21 19:51:24 2009 +0000
+++ b/dtds/army.xsd	Fri Aug 21 20:12:27 2009 +0000
@@ -16,6 +16,7 @@
 													<xs:complexType>
 														<xs:attribute name="id" type="xs:string" /> <!-- ID reference to either a custom equipment item or a Race equipment item -->
 														<xs:attribute name="amount" type="core:nonNegativeDouble" use="required"/><!-- Double used to allow for percentages to be stored -->
+														<xs:attribute name="amountType" type="equipmentAmountType" default="ratio"/>
 														<xs:attribute name="isCustomEquipment" type="xs:boolean" default="false"/>
 													</xs:complexType>
 												</xs:element>
@@ -70,4 +71,10 @@
 		<xs:attribute name="maxPoints" type="xs:positiveInteger" use="required"/>
 	</xs:complexType>
 </xs:element>
+<xs:simpleType name="equipmentAmountType">
+	<xs:restriction base="xs:string">
+		<xs:enumeration value="ratio"/>
+		<xs:enumeration value="fixed"/>
+	</xs:restriction>
+</xs:simpleType>
 </xs:schema>
\ No newline at end of file