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 diff
     1.1 --- a/api/Factories/Xml/WarFoundryXmlSaver.cs	Fri Aug 21 19:51:24 2009 +0000
     1.2 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs	Fri Aug 21 20:12:27 2009 +0000
     1.3 @@ -43,6 +43,18 @@
     1.4  
     1.5  		private string CreateXmlString(WarFoundryObject toSave)
     1.6  		{
     1.7 +			string xmlString = "";
     1.8 +
     1.9 +			if (toSave is Army)
    1.10 +			{
    1.11 +				xmlString = CreateArmyXmlString((Army)toSave);
    1.12 +			}
    1.13 +
    1.14 +			return xmlString;
    1.15 +		}
    1.16 +
    1.17 +		private string CreateArmyXmlString(Army toSave)
    1.18 +		{
    1.19  			/*
    1.20  <army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
    1.21  	<units>
    1.22 @@ -65,18 +77,6 @@
    1.23  	</units>
    1.24  </army>
    1.25  */
    1.26 -			string xmlString = "";
    1.27 -
    1.28 -			if (toSave is Army)
    1.29 -			{
    1.30 -				xmlString = CreateArmyXmlString((Army)toSave);
    1.31 -			}
    1.32 -
    1.33 -			return xmlString;
    1.34 -		}
    1.35 -
    1.36 -		private string CreateArmyXmlString(Army toSave)
    1.37 -		{
    1.38  			XmlDocument doc = new XmlDocument();
    1.39  			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    1.40  			doc.AppendChild(declaration);
    1.41 @@ -96,21 +96,62 @@
    1.42  			
    1.43  			foreach (Unit unit in toSave.GetUnits())
    1.44  			{
    1.45 -				XmlElement unitElem = doc.CreateElement("unit");
    1.46 -				unitElem.SetAttribute("id", unit.ID);
    1.47 -				unitElem.SetAttribute("unitName", unit.Name);
    1.48 -				unitElem.SetAttribute("unitType", unit.UnitType.ID);
    1.49 -				unitElem.SetAttribute("size", unit.Size.ToString());
    1.50 +				units.AppendChild(CreateUnitElement(unit, doc));
    1.51  				
    1.52 -				if (!unit.Race.Equals(toSave.Race))
    1.53 -				{
    1.54 -					unitElem.SetAttribute("race", unit.Race.ID);
    1.55 -				}
    1.56 -				
    1.57 -				units.AppendChild(unitElem);
    1.58  			}
    1.59  			
    1.60  			return doc.OuterXml;
    1.61  		}
    1.62 +
    1.63 +		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
    1.64 +		{
    1.65 +			XmlElement unitElem = doc.CreateElement("unit");
    1.66 +			unitElem.SetAttribute("id", unit.ID);
    1.67 +			unitElem.SetAttribute("unitName", unit.Name);
    1.68 +			unitElem.SetAttribute("unitType", unit.UnitType.ID);
    1.69 +			unitElem.SetAttribute("size", unit.Size.ToString());
    1.70 +			
    1.71 +			if (!unit.Race.Equals(unit.Army.Race))
    1.72 +			{
    1.73 +				unitElem.SetAttribute("race", unit.Race.ID);
    1.74 +			}
    1.75 +
    1.76 +			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
    1.77 +
    1.78 +			if (equipmentElem != null)
    1.79 +			{
    1.80 +				unitElem.AppendChild(equipmentElem);
    1.81 +			}
    1.82 +			
    1.83 +			return unitElem;
    1.84 +		}
    1.85 +
    1.86 +		private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
    1.87 +		{
    1.88 +			UnitEquipmentItem[] equipItems = unit.GetEquipment();
    1.89 +			int equipItemCount = equipItems.Length;
    1.90 +			XmlElement equipmentElem = null;
    1.91 +
    1.92 +			if (equipItemCount > 0)
    1.93 +			{
    1.94 +				equipmentElem = doc.CreateElement("equipment");
    1.95 +				
    1.96 +				for (int i = 0; i < equipItemCount; i++)
    1.97 +				{
    1.98 +					equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
    1.99 +				}
   1.100 +			}
   1.101 +
   1.102 +			return equipmentElem;
   1.103 +		}
   1.104 +
   1.105 +		private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
   1.106 +		{
   1.107 +			XmlElement equipmentItemElem = doc.CreateElement("equipItem");
   1.108 +			equipmentItemElem.SetAttribute("id", item.EquipmentItemID);
   1.109 +			equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
   1.110 +			equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
   1.111 +			return equipmentItemElem;
   1.112 +		}
   1.113  	}
   1.114  }
     2.1 --- a/dtds/army.xsd	Fri Aug 21 19:51:24 2009 +0000
     2.2 +++ b/dtds/army.xsd	Fri Aug 21 20:12:27 2009 +0000
     2.3 @@ -16,6 +16,7 @@
     2.4  													<xs:complexType>
     2.5  														<xs:attribute name="id" type="xs:string" /> <!-- ID reference to either a custom equipment item or a Race equipment item -->
     2.6  														<xs:attribute name="amount" type="core:nonNegativeDouble" use="required"/><!-- Double used to allow for percentages to be stored -->
     2.7 +														<xs:attribute name="amountType" type="equipmentAmountType" default="ratio"/>
     2.8  														<xs:attribute name="isCustomEquipment" type="xs:boolean" default="false"/>
     2.9  													</xs:complexType>
    2.10  												</xs:element>
    2.11 @@ -70,4 +71,10 @@
    2.12  		<xs:attribute name="maxPoints" type="xs:positiveInteger" use="required"/>
    2.13  	</xs:complexType>
    2.14  </xs:element>
    2.15 +<xs:simpleType name="equipmentAmountType">
    2.16 +	<xs:restriction base="xs:string">
    2.17 +		<xs:enumeration value="ratio"/>
    2.18 +		<xs:enumeration value="fixed"/>
    2.19 +	</xs:restriction>
    2.20 +</xs:simpleType>
    2.21  </xs:schema>
    2.22 \ No newline at end of file