changeset 176:22429737cd77

Re #198: Add slots with counts to units * Migrate to using new Limit objects from ibboard:ticket:24 * Parse new objects * Move more data type definitions in to Core schema for re-use * Make UnitType just return limit objects
author IBBoard <dev@ibboard.co.uk>
date Thu, 22 Oct 2009 19:51:42 +0000
parents 996816199b72
children 948e90426a8d
files api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/UnitType.cs dtds/race.xsd dtds/warfoundry-core.xsd
diffstat 4 files changed, 98 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Wed Oct 21 19:01:13 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Thu Oct 22 19:51:42 2009 +0000
@@ -8,6 +8,7 @@
 using System.Xml;
 using IBBoard.Xml;
 using IBBoard.IO;
+using IBBoard.Limits;
 using IBBoard.CustomMath;
 using ICSharpCode.SharpZipLib.Zip;
 using IBBoard.WarFoundry.API.Objects;
@@ -156,13 +157,52 @@
 
 		private void LoadEquipmentSlotsForUnitType(XmlElement elem, UnitType type)
 		{
-			foreach (XmlElement equip in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:equipmentSlots/race:equipmentSlot"))
+			foreach (XmlElement equipSlot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:equipmentSlots/race:equipmentSlot"))
 			{
-				string slotName = equip.GetAttribute("name");
-				int slotLimit = XmlTools.GetIntValueFromAttribute(equip, "limit");
-				type.AddEquipmentSlot(slotName, slotLimit);
+				LoadEquipmentSlotForUnitType (type, equipSlot);
+			}
+		}
+
+		private static void LoadEquipmentSlotForUnitType (UnitType type, XmlElement equipSlot)
+		{
+			string slotName = equipSlot.GetAttribute ("name");
+			XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]");
+			AbstractLimit limit = GetLimitFromElement(limitElem);
+			
+			if (limit!=null)
+			{
+				type.AddEquipmentSlot (slotName, limit);
 			}
 		}
+		
+		private static AbstractLimit GetLimitFromElement(XmlElement limitElem)
+		{
+			AbstractLimit limit = null;
+			
+			switch(limitElem.Name)
+			{
+				case "percentageLimit":
+					double limitPercent = XmlTools.GetDoubleValueFromAttribute (limitElem, "limit");
+					bool roundUp = limitElem.GetAttribute("round").Equals("up");
+					limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp);
+					break;
+				case "sizeConstrainedLimit":
+					limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
+					break;
+				case "absoluteLimit":
+					limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
+					break;
+				case "unlimitedLimit":
+					limit = new UnlimitedLimit();
+					break;
+				default:
+					//TODO: Warn of missing handler for when we've extended the limit list
+					break;
+			}
+			
+			return limit;
+		}
+
 
 		private void LoadEquipmentForUnitType(XmlElement elem, UnitType type)
 		{
--- a/api/Objects/UnitType.cs	Wed Oct 21 19:01:13 2009 +0000
+++ b/api/Objects/UnitType.cs	Thu Oct 22 19:51:42 2009 +0000
@@ -5,6 +5,7 @@
 using System;
 using System.Collections.Generic;
 using System.Xml;
+using IBBoard.Limits;
 using IBBoard.Logging;
 using IBBoard.WarFoundry.API.Requirements;
 
@@ -31,7 +32,7 @@
 		private String notes = "";
 		private List<UnitType> containedTypes = new List<UnitType>();
 		private Dictionary<string, string> extraData = new Dictionary<string, string>();
-		private Dictionary<string, int> slotLimits = new Dictionary<string, int>();
+		private Dictionary<string, AbstractLimit> slotLimits = new Dictionary<string, AbstractLimit>();
 
 
 		public UnitType(string id, string typeName, Race parentRace) : base(id, typeName)
@@ -450,7 +451,7 @@
 			}
 		}
 
-		public void AddEquipmentSlot(string slotName, int slotLimit)
+		public void AddEquipmentSlot(string slotName, AbstractLimit slotLimit)
 		{
 			slotLimits.Add(slotName, slotLimit);
 		}
@@ -461,18 +462,23 @@
 		}
 
 		/// <summary>
-		/// Gets the maximum number of items allowed in a single slot
+		/// Gets the maximum limit on the number of items allowed in a single slot
 		/// </summary>
-		/// <param name="slotName">The name of the equipment slot to get the limit for the named slot</param>
-		/// <returns>The maximum number of items allowed in a slot, or <code>-1</code> if the slot is the default one or has not been specified</returns>
-		public int GetEquipmentSlotLimit(string slotName)
+		/// <param name="slotName">The name of the equipment slot to get the limit for</param>
+		/// <returns>The limit of the number of items allowed in a slot, or an infinite limit if the slot is the default one or has not been specified</returns>
+		public AbstractLimit GetEquipmentSlotLimit(string slotName)
 		{
-			int slotLimit = -1;
+			AbstractLimit slotLimit = null;
 
 			if (HasEquipmentSlot(slotName))
 			{
 				slotLimit = DictionaryUtils.GetValue(slotLimits, slotName);
 			}
+			
+			if (slotLimit == null)
+			{
+				slotLimit = new UnlimitedLimit();
+			}
 
 			return slotLimit;
 		}
--- a/dtds/race.xsd	Wed Oct 21 19:01:13 2009 +0000
+++ b/dtds/race.xsd	Thu Oct 22 19:51:42 2009 +0000
@@ -33,14 +33,11 @@
 											<xs:sequence>
 												<xs:element name="equipmentSlot" maxOccurs="unbounded">
 													<xs:complexType>
-														<xs:simpleContent>
-															<xs:extension base="xs:string">
-																<xs:attribute name="name" type="xs:string" use="required"/>
-																<xs:attribute name="limit" type="xs:integer" default="0"/>
-																<xs:attribute name="isPercentage" type="xs:boolean" default="false"/>
-																<xs:anyAttribute/>
-															</xs:extension>
-														</xs:simpleContent>
+														<xs:all>
+															<xs:element name="maxLimit" minOccurs="0" type="core:limit" />															
+														</xs:all>
+														<xs:attribute name="name" type="xs:string" use="required"/>
+														<xs:anyAttribute/>
 													</xs:complexType>
 												</xs:element>
 											</xs:sequence>
@@ -61,7 +58,7 @@
 														<xs:attribute name="maxNum" type="core:nonNegativeOrInfiniteInteger" default="-1"/>
 														<xs:attribute name="minPercentage" type="core:percentage" default="100"/>
 														<xs:attribute name="maxPercentage" type="core:percentage" default="100"/>
-														<xs:attribute name="roundDirection" type="updowntype" default="up"/>
+														<xs:attribute name="roundDirection" type="core:updowntype" default="up"/>
 														<xs:attribute name="costMultiplier" type="core:nonNegativeDouble" default="1"/>
 														<xs:attribute name="costRounding" type="costroundingtype" default="UpToHalf"/>
 														<xs:anyAttribute/>
@@ -199,12 +196,6 @@
 		<xs:anyAttribute/>
 	</xs:complexType>
 </xs:element>
-<xs:simpleType name="updowntype">
-	<xs:restriction base="xs:string">
-		<xs:enumeration value="up"/>
-		<xs:enumeration value="down"/>
-	</xs:restriction>
-</xs:simpleType>
 <xs:simpleType name="costroundingtype">
 	<xs:restriction base="xs:string">
 		<xs:enumeration value="Up"/>
--- a/dtds/warfoundry-core.xsd	Wed Oct 21 19:01:13 2009 +0000
+++ b/dtds/warfoundry-core.xsd	Thu Oct 22 19:51:42 2009 +0000
@@ -7,7 +7,35 @@
 			<xs:any minOccurs="0" maxOccurs="unbounded" />
 		</xs:sequence>
 	</xs:complexType>
-	</xs:element>
+</xs:element>
+<xs:complexType name="limit">
+	<xs:choice>
+		<xs:element name="percentageLimit">
+			<xs:complexType>
+				<xs:attribute name="limit" type="percentage" use="required"/>
+				<xs:attribute name="round" type="updowntype" default="up" />
+				<xs:anyAttribute/>
+			</xs:complexType>
+		</xs:element>
+		<xs:element name="sizeConstrainedLimit">
+			<xs:complexType>
+				<xs:attribute name="limit" type="xs:positiveInteger" use="required"/>
+				<xs:anyAttribute/>
+			</xs:complexType>
+		</xs:element>
+		<xs:element name="absoluteLimit">
+			<xs:complexType>
+				<xs:attribute name="limit" type="xs:positiveInteger" use="required"/>
+				<xs:anyAttribute/>
+			</xs:complexType>
+		</xs:element>
+		<xs:element name="unlimitedLimit">
+			<xs:complexType>
+				<xs:anyAttribute/>
+			</xs:complexType>
+		</xs:element>
+	</xs:choice>
+</xs:complexType>
 <xs:simpleType name="positiveOrInfiniteInteger">
 	<xs:union memberTypes="xs:positiveInteger infinity"/>
 </xs:simpleType>
@@ -40,4 +68,10 @@
 <xs:simpleType name="nonNegativeOrInfiniteIntegerOrPercentage">
 	<xs:union memberTypes="xs:nonNegativeInteger infinity percentage"/>
 </xs:simpleType>
+<xs:simpleType name="updowntype">
+	<xs:restriction base="xs:string">
+		<xs:enumeration value="up"/>
+		<xs:enumeration value="down"/>
+	</xs:restriction>
+</xs:simpleType>
 </xs:schema>
\ No newline at end of file