# HG changeset patch # User IBBoard # Date 1322943013 0 # Node ID 5ac76de8ce628c8152c6df678b4c9996f30b6a48 # Parent d2331ee59d74b66d965c1cc776fb8533c7c5e8f6 Re #350: Add requirement to allow N of unit for specific other units * Add code for parsing min numbers as well as allowed numbers diff -r d2331ee59d74 -r 5ac76de8ce62 API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs --- 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)); + } + } } }