changeset 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
files API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 02 14:59:45 2011 +0000
     1.2 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 02 15:10:17 2011 +0000
     1.3 @@ -27,16 +27,26 @@
     1.4  			foreach (string requirement in data.Split('|'))
     1.5  			{
     1.6  				string[] requirementParts = requirement.Split(':');
     1.7 -				UnitType unitType = race.GetUnitType(requirementParts[0]);
     1.8 +				string unitID = requirementParts[0];
     1.9 +				UnitType unitType = race.GetUnitType(unitID);
    1.10  
    1.11  				if (unitType == null)
    1.12  				{
    1.13 -					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", requirementParts[0]));
    1.14 +					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", unitID));
    1.15  				}
    1.16  
    1.17  				if (requirementParts.Length == 2)
    1.18  				{
    1.19 -					req.AddUnitTypeRequirement(unitType, Int32.Parse(requirementParts[1]));
    1.20 +					string amount = requirementParts[1];
    1.21 +
    1.22 +					try
    1.23 +					{
    1.24 +						req.AddUnitTypeRequirement(unitType, Int32.Parse(amount));
    1.25 +					}
    1.26 +					catch (FormatException ex)
    1.27 +					{
    1.28 +						throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires at least N units' requirement", amount, unitID));
    1.29 +					}
    1.30  				}
    1.31  				else
    1.32  				{