view API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.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 3882b533d99d
children 7b9ff7b1df24
line wrap: on
line source

// This file (UnitRequiresAtLeastNUnitsRequirementFactory.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;

namespace IBBoard.WarFoundry.API.Factories.Requirement
{
	/// <summary>
	/// Factory for creating instances of <see cref="UnitRequiresAtLeastNUnitsRequirement" />. Data must be in the format:
	///
	/// <code>unitID[:count][|unitID[:count][|...]]</code>
	///
	/// e.g.:
	///
	/// <code>Swordsmen:2|Bowmen</code>
	///
	/// would generate a requirement to allow any number of the unit type after 1 unit with ID Bowmen or 2 units with ID Swordsmen were added.
	/// </summary>
	/// <exception cref='InvalidRequirementException'>
	/// Is thrown when the invalid requirement exception.
	/// </exception>
	public class UnitRequiresAtLeastNUnitsRequirementFactory : IRequirementFactory
	{
		public UnitRequiresAtLeastNUnitsRequirementFactory()
		{
			//Do nothing special
		}

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

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

		private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresAtLeastNUnitsRequirement 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 = raceFactory.GetUnitType(unitID, race);

				if (unitType == null)
				{
					throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", unitID));
				}

				if (requirementParts.Length == 2)
				{
					string amount = requirementParts[1];

					try
					{
						req.AddUnitTypeRequirement(unitType, Int32.Parse(amount));
					}
					catch (FormatException)
					{
						throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires at least N units' requirement", amount, unitID));
					}
				}
				else
				{
					req.AddUnitTypeRequirement(unitType);
				}
			}
		}
	}
}