changeset 490:e35fc85eda2b

Re #410: Create "N units per M models in parent unit" requirement * Add factory to create requirements * Add methods to allow things other than 1:1 values for N and M
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jul 2012 21:02:59 +0100
parents aac77204ae0e
children a41c2bc1683a
files API/Factories/Requirement/UnitRequiresNParentModelsForMUnitsRequirementFactory.cs API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirement.cs API/WarFoundryHacks.cs IBBoard.WarFoundry.API.csproj
diffstat 4 files changed, 112 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Factories/Requirement/UnitRequiresNParentModelsForMUnitsRequirementFactory.cs	Sun Jul 29 21:02:59 2012 +0100
@@ -0,0 +1,97 @@
+// This file (UnitRequiresNParentModelsForMUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 IBBoard
+//
+// 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.
+using System;
+using IBBoard.WarFoundry.API.Objects.Requirement;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API.Factories.Requirement
+{
+	public class UnitRequiresNParentModelsForMUnitsRequirementFactory : IRequirementFactory
+	{
+		public UnitRequiresNParentModelsForMUnitsRequirementFactory()
+		{
+		}
+
+		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(IBBoard.WarFoundry.API.Objects.UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
+		{
+			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(type);
+			Race race = type.Race;
+			AddRequirements(req, race, data, raceFactory);
+			return req;
+		}
+		
+		private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresNParentModelsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
+		{
+			foreach (string requirement in data.Split('|'))
+			{
+				string[] requirementParts = requirement.Split(':');
+				string unitID = requirementParts[0];
+				UnitType unitType = GetUnitTypeFromID(unitID, race, raceFactory);
+				
+				if (requirementParts.Length == 2)
+				{
+					string[] amounts = requirementParts[1].Split(';');
+					
+					if (amounts.Length == 1)
+					{
+						req.AddUnitTypeRequirement(unitType, ParseAllowedAmount(amounts[0], unitID));
+					}
+					else
+					{
+						req.AddUnitTypeRequirement(unitType, ParseAllowedAmount(amounts[0], unitID), ParseModelCount(amounts[1], unitID));
+					}
+				}
+				else
+				{
+					req.AddUnitTypeRequirement(unitType);
+				}
+			}
+		}
+
+		private UnitType GetUnitTypeFromID<SOURCE_FILE_TYPE, ENTRY_TYPE>(string unitID, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
+		{
+			UnitType unitType = raceFactory.GetUnitType(unitID, race);
+			
+			if (unitType == null)
+			{
+				throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N models in parent unit for M units' requirement", unitID));
+			}
+
+			return unitType;
+		}
+		
+		private int ParseModelCount(string minCount, string unitID)
+		{
+			try
+			{
+				return Int32.Parse(minCount);
+			}
+			catch (FormatException)
+			{
+				throw new InvalidRequirementException(String.Format("Invalid model amount '{0}' for unit types '{1}' for 'Requires N models in parent unit for M units' requirement", minCount, unitID));
+			}
+		}
+		
+		private int ParseAllowedAmount(string allowedAmount, string unitID)
+		{
+			try
+			{
+				return Int32.Parse(allowedAmount);
+			}
+			catch (FormatException)
+			{
+				throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N models in parent unit for M units' requirement", allowedAmount, unitID));
+			}
+		}
+
+		public string AppliesToID
+		{
+			get
+			{
+				return UnitRequiresNParentModelsForMUnitsRequirement.REQUIREMENT_ID;
+			}
+		}
+	}
+}
+
--- a/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirement.cs	Sun Jul 29 15:31:09 2012 +0100
+++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirement.cs	Sun Jul 29 21:02:59 2012 +0100
@@ -10,7 +10,7 @@
 {
 	public class UnitRequiresNParentModelsForMUnitsRequirement : AbstractUnitRequirement<UnitType>
 	{
-		public static readonly string N_PER_M_IN_PARENT_REQUIREMENT_ID = "RequiresNoMoreThanNUnitsPerMModelsInParent";
+		public static readonly string REQUIREMENT_ID = "RequiresNoMoreThanNUnitsPerMModelsInParent";
 
 		public UnitRequiresNParentModelsForMUnitsRequirement(UnitType allowedObject, params UnitType[] limitedUnitTypes) : base(allowedObject, limitedUnitTypes)
 		{
@@ -21,7 +21,7 @@
 		{
 			get
 			{
-				return N_PER_M_IN_PARENT_REQUIREMENT_ID;
+				return REQUIREMENT_ID;
 			}
 		}
 
@@ -83,7 +83,17 @@
 
 		public override void AddUnitTypeRequirement(UnitType unitType)
 		{
-			this.ConstraintTypes.Add(new UnitCountRequirementData(unitType, 1, 1));
+			AddUnitTypeRequirement(unitType, 1);
+		}
+
+		public void AddUnitTypeRequirement(UnitType unitType, int allowedCount)
+		{
+			AddUnitTypeRequirement(unitType, allowedCount, 1);
+		}
+
+		public void AddUnitTypeRequirement(UnitType unitType, int allowedCount, int reqsCount)
+		{
+			ConstraintTypes.Add(new UnitCountRequirementData(unitType, reqsCount, allowedCount));
 		}
 
 		protected override string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy, AddingContext context)
--- a/API/WarFoundryHacks.cs	Sun Jul 29 15:31:09 2012 +0100
+++ b/API/WarFoundryHacks.cs	Sun Jul 29 21:02:59 2012 +0100
@@ -32,6 +32,7 @@
 			WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresAtLeastNUnitsRequirementFactory());
 			WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresNoMoreThanNOfUnitTypeRequirementFactory());
 			WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresNUnitsForMUnitsRequirementFactory());
+			WarFoundryLoader.RegisterRequirementFactory(new UnitRequiresNParentModelsForMUnitsRequirementFactory());
 		}
 	}
 }
--- a/IBBoard.WarFoundry.API.csproj	Sun Jul 29 15:31:09 2012 +0100
+++ b/IBBoard.WarFoundry.API.csproj	Sun Jul 29 21:02:59 2012 +0100
@@ -164,6 +164,7 @@
     <Compile Include="API\Loading\LoadableObjectSourceResourceSet.cs" />
     <Compile Include="API\Objects\Requirement\Context\NullAddingContext.cs" />
     <Compile Include="API\Objects\Requirement\UnitRequiresNParentModelsForMUnitsRequirement.cs" />
+    <Compile Include="API\Factories\Requirement\UnitRequiresNParentModelsForMUnitsRequirementFactory.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System.Xml" />