diff API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs @ 442:5ac76de8ce62

Re #350: Add requirement to allow N of unit for specific other units * Add code for parsing min numbers as well as allowed numbers
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 20:10:13 +0000
parents d2331ee59d74
children 4fbb7f205f7e
line wrap: on
line diff
--- a/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs	Sat Dec 03 16:55:24 2011 +0000
+++ b/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs	Sat Dec 03 20:10:13 2011 +0000
@@ -45,16 +45,23 @@
 
 				if (requirementParts.Length == 2)
 				{
-					string amount = requirementParts[1];
+					string[] amounts = requirementParts[1].Split(';');
 
-					try
+					int minCount;
+					int allowedAmounts;
+
+					if (amounts.Length == 1)
 					{
-						req.AddUnitTypeRequirement(unitType, 1, Int32.Parse(amount));
+						minCount = 1;
+						allowedAmounts = ParseAllowedAmount(amounts[0], unitID);
 					}
-					catch (FormatException)
+					else
 					{
-						throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", amount, unitID));
+						minCount = ParseMinCount(amounts[0], unitID);
+						allowedAmounts = ParseAllowedAmount(amounts[1], unitID);
 					}
+
+					req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts);
 				}
 				else
 				{
@@ -62,6 +69,30 @@
 				}
 			}
 		}
+
+		private int ParseMinCount(string minCount, string unitID)
+		{
+			try
+			{
+				return Int32.Parse(minCount);
+			}
+			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));
+			}
+		}
+
+		private int ParseAllowedAmount(string allowedAmount, string unitID)
+		{
+			try
+			{
+				return Int32.Parse(allowedAmount);
+			}
+			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));
+			}
+		}
 	}
 }