view API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs @ 448:dbd779cdc0f9

Re #350: Add requirement to allow N of unit for specific other units * Fix limit checking when "limited count" is not 1 (e.g. "need 2 for every 3")
author IBBoard <dev@ibboard.co.uk>
date Fri, 23 Dec 2011 15:36:06 +0000
parents 4fbb7f205f7e
children afc6410e4efc
line wrap: on
line source

// This file (UnitRequiresNUnitsForMUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 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;
using IBBoard.WarFoundry.API.Objects.Requirement;
using System.Collections.Generic;

namespace IBBoard.WarFoundry.API.Factories.Requirement
{
	public class UnitRequiresNUnitsForMUnitsRequirementFactory : IRequirementFactory
	{
		public UnitRequiresNUnitsForMUnitsRequirementFactory()
		{
			//Do nothing special
		}

		public string AppliesToID
		{
			get
			{
				return RequiresNUnitsForMUnitsRequirement.REQUIREMENT_ID;
			}
		}

		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
		{
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(type);
			Race race = type.Race;
			AddRequirements(req, race, data, raceFactory);
			return req;
		}

		private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(RequiresNUnitsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
		{
			foreach (string requirement in data.Split('|'))
			{
				string[] requirementParts = requirement.Split(':');
				string unitIDs = requirementParts[0];
				UnitType[] unitTypes = GetUnitTypes(unitIDs, race, raceFactory);

				if (requirementParts.Length == 2)
				{
					string[] amounts = requirementParts[1].Split(';');

					int minCount;
					int allowedAmounts;

					if (amounts.Length == 1)
					{
						minCount = 1;
						allowedAmounts = ParseAllowedAmount(amounts[0], unitIDs);
					}
					else
					{
						minCount = ParseMinCount(amounts[0], unitIDs);
						allowedAmounts = ParseAllowedAmount(amounts[1], unitIDs);
					}

					req.AddUnitTypeRequirement(minCount, allowedAmounts, unitTypes);
				}
				else
				{
					req.AddUnitTypeRequirement(unitTypes);
				}
			}
		}

		private UnitType[] GetUnitTypes<SOURCE_FILE_TYPE, ENTRY_TYPE>(string data, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
		{
			List<UnitType> unitTypes = new List<UnitType>();

			foreach (string unitID in data.Split(';'))
			{
				UnitType unitType = raceFactory.GetUnitType(unitID, race);
	
				if (unitType == null)
				{
					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
				}

				unitTypes.Add(unitType);
			}

			return unitTypes.ToArray();
		}

		private int ParseMinCount(string minCount, string unitID)
		{
			try
			{
				return Int32.Parse(minCount);
			}
			catch (FormatException)
			{
				throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit types '{1}' for 'Requires N units 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 units for M units' requirement", allowedAmount, unitID));
			}
		}
	}
}