comparison 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
comparison
equal deleted inserted replaced
441:d2331ee59d74 442:5ac76de8ce62
43 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID)); 43 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
44 } 44 }
45 45
46 if (requirementParts.Length == 2) 46 if (requirementParts.Length == 2)
47 { 47 {
48 string amount = requirementParts[1]; 48 string[] amounts = requirementParts[1].Split(';');
49 49
50 try 50 int minCount;
51 int allowedAmounts;
52
53 if (amounts.Length == 1)
51 { 54 {
52 req.AddUnitTypeRequirement(unitType, 1, Int32.Parse(amount)); 55 minCount = 1;
56 allowedAmounts = ParseAllowedAmount(amounts[0], unitID);
53 } 57 }
54 catch (FormatException) 58 else
55 { 59 {
56 throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", amount, unitID)); 60 minCount = ParseMinCount(amounts[0], unitID);
61 allowedAmounts = ParseAllowedAmount(amounts[1], unitID);
57 } 62 }
63
64 req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts);
58 } 65 }
59 else 66 else
60 { 67 {
61 req.AddUnitTypeRequirement(unitType); 68 req.AddUnitTypeRequirement(unitType);
62 } 69 }
63 } 70 }
64 } 71 }
72
73 private int ParseMinCount(string minCount, string unitID)
74 {
75 try
76 {
77 return Int32.Parse(minCount);
78 }
79 catch (FormatException)
80 {
81 throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
82 }
83 }
84
85 private int ParseAllowedAmount(string allowedAmount, string unitID)
86 {
87 try
88 {
89 return Int32.Parse(allowedAmount);
90 }
91 catch (FormatException)
92 {
93 throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
94 }
95 }
65 } 96 }
66 } 97 }
67 98