comparison API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs @ 375:d9bf78a8f517

Re #351: Add extensible requirement handling method * Wrap exception from integer parsing
author IBBoard <dev@ibboard.co.uk>
date Sat, 02 Jul 2011 15:10:17 +0000
parents 13793f3a2a2e
children e50682387d63
comparison
equal deleted inserted replaced
374:13793f3a2a2e 375:d9bf78a8f517
25 private void AddRequirements(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data) 25 private void AddRequirements(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data)
26 { 26 {
27 foreach (string requirement in data.Split('|')) 27 foreach (string requirement in data.Split('|'))
28 { 28 {
29 string[] requirementParts = requirement.Split(':'); 29 string[] requirementParts = requirement.Split(':');
30 UnitType unitType = race.GetUnitType(requirementParts[0]); 30 string unitID = requirementParts[0];
31 UnitType unitType = race.GetUnitType(unitID);
31 32
32 if (unitType == null) 33 if (unitType == null)
33 { 34 {
34 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", requirementParts[0])); 35 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", unitID));
35 } 36 }
36 37
37 if (requirementParts.Length == 2) 38 if (requirementParts.Length == 2)
38 { 39 {
39 req.AddUnitTypeRequirement(unitType, Int32.Parse(requirementParts[1])); 40 string amount = requirementParts[1];
41
42 try
43 {
44 req.AddUnitTypeRequirement(unitType, Int32.Parse(amount));
45 }
46 catch (FormatException ex)
47 {
48 throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires at least N units' requirement", amount, unitID));
49 }
40 } 50 }
41 else 51 else
42 { 52 {
43 req.AddUnitTypeRequirement(unitType); 53 req.AddUnitTypeRequirement(unitType);
44 } 54 }