changeset 441:d2331ee59d74

Re #350: Add requirement to allow N of unit for specific other units * Add factory for requirement * Add extra method that assumes "allow N unit for every 1 unit" * Update/fix documentation
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 16:55:24 +0000
parents baa34d91031f
children 5ac76de8ce62
files API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs IBBoard.WarFoundry.API.csproj
diffstat 3 files changed, 86 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs	Sat Dec 03 16:55:24 2011 +0000
     1.3 @@ -0,0 +1,67 @@
     1.4 +// This file (UnitRequiresNUnitsForMUnitsRequirementFactory.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 UnitRequiresNUnitsForMUnitsRequirementFactory : IRequirementFactory
    1.14 +	{
    1.15 +		public UnitRequiresNUnitsForMUnitsRequirementFactory()
    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 RequiresNUnitsForMUnitsRequirement.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 +			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(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>(RequiresNUnitsForMUnitsRequirement 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 N units for M units' 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, 1, 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 N units for M units' 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/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Sat Dec 03 16:14:26 2011 +0000
     2.2 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Sat Dec 03 16:55:24 2011 +0000
     2.3 @@ -198,12 +198,29 @@
     2.4  		/// <param name='minCount'>
     2.5  		/// The minimum number of that type that must exist.
     2.6  		/// </param>
     2.7 +		/// <param name='allowedCount'>
     2.8 +		/// The number of units allowed for every minCount units of the supplied unit type.
     2.9 +		/// </param>
    2.10  		public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount)
    2.11  		{
    2.12  			requiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
    2.13  		}
    2.14  
    2.15  		/// <summary>
    2.16 +		/// Adds a requirement for there to be at least one of a given UnitType, allowing allowedCount of this UnitType
    2.17 +		/// </summary>
    2.18 +		/// <param name='unitType'>
    2.19 +		/// The unit type to require.
    2.20 +		/// </param>
    2.21 +		/// <param name='allowedCount'>
    2.22 +		/// The number of units allowed for each unit of the supplied unit type.
    2.23 +		/// </param>
    2.24 +		public void AddUnitTypeRequirement(UnitType unitType, int allowedCount)
    2.25 +		{
    2.26 +			AddUnitTypeRequirement(unitType, 1, allowedCount);
    2.27 +		}
    2.28 +
    2.29 +		/// <summary>
    2.30  		/// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType
    2.31  		/// </summary>
    2.32  		/// <param name='unitType'>
    2.33 @@ -211,7 +228,7 @@
    2.34  		/// </param>
    2.35  		public void AddUnitTypeRequirement(UnitType unitType)
    2.36  		{
    2.37 -			AddUnitTypeRequirement(unitType, 1, 1);
    2.38 +			AddUnitTypeRequirement(unitType, 1);
    2.39  		}
    2.40  	}
    2.41  }
     3.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Dec 03 16:14:26 2011 +0000
     3.2 +++ b/IBBoard.WarFoundry.API.csproj	Sat Dec 03 16:55:24 2011 +0000
     3.3 @@ -192,6 +192,7 @@
     3.4        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     3.5      </None>
     3.6      <Compile Include="API\Objects\Requirement\RequiresNUnitsForMUnitsRequirement.cs" />
     3.7 +    <Compile Include="API\Factories\Requirement\UnitRequiresNUnitsForMUnitsRequirementFactory.cs" />
     3.8    </ItemGroup>
     3.9    <ItemGroup>
    3.10      <Reference Include="System.Xml" />