changeset 447:4fbb7f205f7e

Re #350: Add requirement to allow N of unit for specific other units * Extend factory to support additive unit types * Add three parameter method to requirement to set min, allowed and additive unit types Note: Naughty code (untested) added to requirement to make factory compile! Needs testing.
author IBBoard <dev@ibboard.co.uk>
date Tue, 20 Dec 2011 21:03:33 +0000
parents ca1e8f7c8b73
children dbd779cdc0f9
files API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs
diffstat 2 files changed, 47 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs	Tue Dec 20 20:26:54 2011 +0000
+++ b/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs	Tue Dec 20 21:03:33 2011 +0000
@@ -4,6 +4,7 @@
 using System;
 using IBBoard.WarFoundry.API.Objects;
 using IBBoard.WarFoundry.API.Objects.Requirement;
+using System.Collections.Generic;
 
 namespace IBBoard.WarFoundry.API.Factories.Requirement
 {
@@ -35,13 +36,8 @@
 			foreach (string requirement in data.Split('|'))
 			{
 				string[] requirementParts = requirement.Split(':');
-				string unitID = requirementParts[0];
-				UnitType unitType = raceFactory.GetUnitType(unitID, race);
-
-				if (unitType == null)
-				{
-					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
-				}
+				string unitIDs = requirementParts[0];
+				UnitType[] unitTypes = GetUnitTypes(unitIDs, race, raceFactory);
 
 				if (requirementParts.Length == 2)
 				{
@@ -53,23 +49,42 @@
 					if (amounts.Length == 1)
 					{
 						minCount = 1;
-						allowedAmounts = ParseAllowedAmount(amounts[0], unitID);
+						allowedAmounts = ParseAllowedAmount(amounts[0], unitIDs);
 					}
 					else
 					{
-						minCount = ParseMinCount(amounts[0], unitID);
-						allowedAmounts = ParseAllowedAmount(amounts[1], unitID);
+						minCount = ParseMinCount(amounts[0], unitIDs);
+						allowedAmounts = ParseAllowedAmount(amounts[1], unitIDs);
 					}
 
-					req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts);
+					req.AddUnitTypeRequirement(minCount, allowedAmounts, unitTypes);
 				}
 				else
 				{
-					req.AddUnitTypeRequirement(unitType);
+					req.AddUnitTypeRequirement(unitTypes);
 				}
 			}
 		}
 
+		private UnitType[] GetUnitTypes<SOURCE_FILE_TYPE, ENTRY_TYPE>(string data, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
+		{
+			List<UnitType> unitTypes = new List<UnitType>();
+
+			foreach (string unitID in data.Split(';'))
+			{
+				UnitType unitType = raceFactory.GetUnitType(unitID, race);
+	
+				if (unitType == null)
+				{
+					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
+				}
+
+				unitTypes.Add(unitType);
+			}
+
+			return unitTypes.ToArray();
+		}
+
 		private int ParseMinCount(string minCount, string unitID)
 		{
 			try
@@ -78,7 +93,7 @@
 			}
 			catch (FormatException)
 			{
-				throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
+				throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
 			}
 		}
 
@@ -90,7 +105,7 @@
 			}
 			catch (FormatException)
 			{
-				throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
+				throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
 			}
 		}
 	}
--- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Tue Dec 20 20:26:54 2011 +0000
+++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Tue Dec 20 21:03:33 2011 +0000
@@ -278,6 +278,24 @@
 		{
 			requiredTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount));
 		}
+
+		/// <summary>
+		/// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing allowsAmount of this UnitType. If multiple unit types
+		/// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType).
+		/// </summary>
+		/// <param name='minCount'>
+		/// The minimum number of that type that must exist.
+		/// </param>
+		/// <param name="allowsAmount">
+		/// The number of units to be allowed for each 1 of unitType
+		/// </param>
+		/// <param name='unitType'>
+		/// The unit type or types to require.
+		/// </param>
+		public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes)
+		{
+			requiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount));
+		}
 	}
 }