changeset 433:93c373701016

Re #356: Add factory for UnitRequiresNoMoreThanNOfUnitTypeRequirement * Fill in body of methods, based on existing factory * Rename factory to match requirement
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Nov 2011 20:55:02 +0000
parents f64a52e2f8b1
children c32bb2a53b87
files API/Factories/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory.cs API/Factories/Requirement/UnitRequiresNoMoreThanNUnitsRequirementFactory.cs IBBoard.WarFoundry.API.csproj
diffstat 3 files changed, 68 insertions(+), 30 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory.cs	Sun Nov 13 20:55:02 2011 +0000
     1.3 @@ -0,0 +1,67 @@
     1.4 +// This file (UnitRequiresNoMoreThanNUnitsRequirementFactory.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 +using IBBoard.WarFoundry.API.Objects;
     1.9 +using IBBoard.WarFoundry.API.Objects.Requirement;
    1.10 +
    1.11 +namespace IBBoard.WarFoundry.API.Factories.Requirement
    1.12 +{
    1.13 +	public class UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory : IRequirementFactory
    1.14 +	{
    1.15 +		public UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory()
    1.16 +		{
    1.17 +			//Do nothing special
    1.18 +		}
    1.19 +
    1.20 +		public string AppliesToID
    1.21 +		{
    1.22 +			get
    1.23 +			{
    1.24 +				return UnitRequiresNoMoreThanNOfUnitTypeRequirement.REQUIREMENT_ID;
    1.25 +			}
    1.26 +		}
    1.27 +
    1.28 +		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    1.29 +		{
    1.30 +			UnitRequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(type);
    1.31 +			Race race = type.Race;
    1.32 +			AddRequirements(req, race, data, raceFactory);
    1.33 +			return req;
    1.34 +		}
    1.35 +
    1.36 +		private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresNoMoreThanNOfUnitTypeRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    1.37 +		{
    1.38 +			foreach (string requirement in data.Split('|'))
    1.39 +			{
    1.40 +				string[] requirementParts = requirement.Split(':');
    1.41 +				string unitID = requirementParts[0];
    1.42 +				UnitType unitType = raceFactory.GetUnitType(unitID, race);
    1.43 +
    1.44 +				if (unitType == null)
    1.45 +				{
    1.46 +					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires no more than N of unit type' requirement", unitID));
    1.47 +				}
    1.48 +
    1.49 +				if (requirementParts.Length == 2)
    1.50 +				{
    1.51 +					string amount = requirementParts[1];
    1.52 +
    1.53 +					try
    1.54 +					{
    1.55 +						req.AddUnitTypeRequirement(unitType, Int32.Parse(amount));
    1.56 +					}
    1.57 +					catch (FormatException)
    1.58 +					{
    1.59 +						throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires no more than N of unit type' requirement", amount, unitID));
    1.60 +					}
    1.61 +				}
    1.62 +				else
    1.63 +				{
    1.64 +					req.AddUnitTypeRequirement(unitType);
    1.65 +				}
    1.66 +			}
    1.67 +		}
    1.68 +	}
    1.69 +}
    1.70 +
     2.1 --- a/API/Factories/Requirement/UnitRequiresNoMoreThanNUnitsRequirementFactory.cs	Sun Nov 13 20:46:46 2011 +0000
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,29 +0,0 @@
     2.4 -// This file (UnitRequiresNoMoreThanNUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     2.5 -// 
     2.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.
     2.7 -using System;
     2.8 -using IBBoard.WarFoundry.API.Objects;
     2.9 -using IBBoard.WarFoundry.API.Objects.Requirement;
    2.10 -
    2.11 -namespace IBBoard.WarFoundry.API.Factories.Requirement
    2.12 -{
    2.13 -	public class UnitRequiresNoMoreThanNUnitsRequirementFactory : IRequirementFactory
    2.14 -	{
    2.15 -		public UnitRequiresNoMoreThanNUnitsRequirementFactory()
    2.16 -		{
    2.17 -			//Do nothing special
    2.18 -		}
    2.19 -
    2.20 -		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType unitType, string typeID, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    2.21 -		{
    2.22 -			throw new NotImplementedException();
    2.23 -		}
    2.24 -
    2.25 -		public string AppliesToID {
    2.26 -			get {
    2.27 -				throw new NotImplementedException ();
    2.28 -			}
    2.29 -		}
    2.30 -	}
    2.31 -}
    2.32 -
     3.1 --- a/IBBoard.WarFoundry.API.csproj	Sun Nov 13 20:46:46 2011 +0000
     3.2 +++ b/IBBoard.WarFoundry.API.csproj	Sun Nov 13 20:55:02 2011 +0000
     3.3 @@ -183,7 +183,7 @@
     3.4      <Compile Include="API\Factories\IRaceFactory.cs" />
     3.5      <Compile Include="API\Factories\Requirement\IRequirementFactory.cs" />
     3.6      <Compile Include="API\Commands\EditArmyCommand.cs" />
     3.7 -    <Compile Include="API\Factories\Requirement\UnitRequiresNoMoreThanNUnitsRequirementFactory.cs" />
     3.8 +    <Compile Include="API\Factories\Requirement\UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory.cs" />
     3.9    </ItemGroup>
    3.10    <ItemGroup>
    3.11      <Reference Include="System.Xml" />