changeset 374:13793f3a2a2e

Re #351: Add extensible requirement handling method * Add initial conditional to fetching of factory * Exception if content isn't correct
author IBBoard <dev@ibboard.co.uk>
date Sat, 02 Jul 2011 14:59:45 +0000
parents e10630f39ec2
children d9bf78a8f517
files API/Factories/Requirement/InvalidRequirementException.cs API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs API/Factories/Xml/WarFoundryXmlRaceFactory.cs IBBoard.WarFoundry.API.csproj
diffstat 4 files changed, 35 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Requirement/InvalidRequirementException.cs	Sat Jul 02 14:59:45 2011 +0000
     1.3 @@ -0,0 +1,15 @@
     1.4 +// This file (InvalidRequirementException.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     1.5 +// 
     1.6 +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
     1.7 +using System;
     1.8 +
     1.9 +namespace IBBoard.WarFoundry.API.Factories.Requirement
    1.10 +{
    1.11 +	public class InvalidRequirementException : Exception
    1.12 +	{
    1.13 +		public InvalidRequirementException(string message) : base(message)
    1.14 +		{
    1.15 +		}
    1.16 +	}
    1.17 +}
    1.18 +
     2.1 --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Tue Jun 14 14:27:14 2011 +0000
     2.2 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 02 14:59:45 2011 +0000
     2.3 @@ -27,13 +27,20 @@
     2.4  			foreach (string requirement in data.Split('|'))
     2.5  			{
     2.6  				string[] requirementParts = requirement.Split(':');
     2.7 +				UnitType unitType = race.GetUnitType(requirementParts[0]);
     2.8 +
     2.9 +				if (unitType == null)
    2.10 +				{
    2.11 +					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", requirementParts[0]));
    2.12 +				}
    2.13 +
    2.14  				if (requirementParts.Length == 2)
    2.15  				{
    2.16 -					req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0]), Int32.Parse(requirementParts[1]));
    2.17 +					req.AddUnitTypeRequirement(unitType, Int32.Parse(requirementParts[1]));
    2.18  				}
    2.19  				else
    2.20  				{
    2.21 -					req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0]));
    2.22 +					req.AddUnitTypeRequirement(unitType);
    2.23  				}
    2.24  			}
    2.25  		}
     3.1 --- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Tue Jun 14 14:27:14 2011 +0000
     3.2 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Jul 02 14:59:45 2011 +0000
     3.3 @@ -388,9 +388,17 @@
     3.4  			}
     3.5  		}
     3.6  
     3.7 -		UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name)
     3.8 +		private UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name)
     3.9  		{
    3.10 -			return new UnitRequiresAtLeastNUnitsRequirementFactory();
    3.11 +		
    3.12 +			UnitRequiresAtLeastNUnitsRequirementFactory factory = null;
    3.13 +			
    3.14 +			if ("RequiresAtLeastNUnits".Equals(name))
    3.15 +			{
    3.16 +				factory = new UnitRequiresAtLeastNUnitsRequirementFactory();
    3.17 +			}
    3.18 +
    3.19 +			return factory;
    3.20  		}
    3.21  		
    3.22  		private void LoadExtraDataForUnitType(XmlElement elem, UnitType type)
     4.1 --- a/IBBoard.WarFoundry.API.csproj	Tue Jun 14 14:27:14 2011 +0000
     4.2 +++ b/IBBoard.WarFoundry.API.csproj	Sat Jul 02 14:59:45 2011 +0000
     4.3 @@ -180,6 +180,7 @@
     4.4      <Compile Include="API\Objects\Requirement\RequirementHandler.cs" />
     4.5      <Compile Include="API\Objects\Requirement\AbstractRequirement.cs" />
     4.6      <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactory.cs" />
     4.7 +    <Compile Include="API\Factories\Requirement\InvalidRequirementException.cs" />
     4.8    </ItemGroup>
     4.9    <ItemGroup>
    4.10      <Reference Include="System.Xml" />