view API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 453:dace3b7779ca

Re #379: Fix validation of requirements to check for unit * Restructure all validation messages to a common base system that will translate better * Update "No More Than" texts to differentiate two contexts (adding and validating)
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jan 2012 20:05:25 +0000
parents b671085871b7
children afc6410e4efc
line wrap: on
line source

// This file (UnitRequiresAtLeastNUnitsRequirement.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 System.Collections.Generic;
using IBBoard.WarFoundry.API.Objects;
using System.Text;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	/// <summary>
	/// A requirement where a WarFoundryObject requires at least N units of any of the specified unit types before any number of that object can be taken in an army.
	///
	/// The definition for how this requirement is built from a data file is defined in the <see cref="UnitRequiresAtLeastNUnitsRequirementFactory"/> class.
	/// </summary>
	public class RequiresAtLeastNUnitsRequirement : AbstractRequirement
	{
		public static readonly string REQUIREMENT_ID = "RequiresAtLeastNUnits";
		private List<UnitCountRequirementData> requiredTypes;

		public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes)
		{
			requiredTypes = new List<UnitCountRequirementData>();

			foreach (UnitType unitType in requiredUnitTypes)
			{
				AddUnitTypeRequirement(unitType);
			}
		}

		public override string RequirementID
		{
			get
			{
				return REQUIREMENT_ID;
			}
		}

		protected override bool TypeEquals(object obj)
		{
			RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj;
			if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		public override int GetHashCode()
		{
			int hash = 0;

			foreach (UnitCountRequirementData req in requiredTypes)
			{
				hash += req.UnitType.GetHashCode();
			}

			return hash;
		}

		/// <summary>
		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
		/// </summary>
		/// <returns>
		/// A <code>Validation</code> enum to show the result of the validation
		/// </returns>
		/// <param name='wfObject'>
		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
		/// </param>
		/// <param name='toArmy'>
		/// The army to add the object to.
		/// </param>
		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
		{
			return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
		}

		protected override bool IsApplicable(Army toArmy)
		{
			return false;
		}

		protected override bool IsApplicable(WarFoundryObject toObject)
		{
			bool isApplicable = false;
			UnitType unitType = GetUnitTypeFromObject(toObject);

			if (unitType != null)
			{
				isApplicable = IsApplicable(unitType);
			}

			return isApplicable;
		}

		protected UnitType GetUnitTypeFromObject(WarFoundryObject toObject)
		{
			UnitType unitType = null;

			if (toObject is UnitType)
			{
				unitType = (UnitType)toObject;
			}
			else if (toObject is Unit)
			{
				unitType = ((Unit)toObject).UnitType;
			}

			return unitType;
		}

		private bool IsApplicable(UnitType unitType)
		{
			bool isApplicable = false;
			foreach (UnitCountRequirementData requirement in requiredTypes)
			{
				if (requirement.UnitType.Equals(unitType))
				{
					isApplicable = true;
					break;
				}
			}
			return isApplicable;
		}

		private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy)
		{
			Validation isValid = Validation.Passed;
			
			foreach (UnitCountRequirementData requirement in requiredTypes)
			{
				if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
				{
					isValid = Validation.Failed;
					break;
				}
			}
			
			return isValid;
		}

		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
		{
			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
		}

		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
		{
			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
		}

		/// <summary>
		/// Adds a requirement for there to be at least minCount of a given UnitType
		/// </summary>
		/// <param name='unitType'>
		/// The unit type to require.
		/// </param>
		/// <param name='minCount'>
		/// The minimum number of that type that must exist.
		/// </param>
		public void AddUnitTypeRequirement(UnitType unitType, int minCount)
		{
			requiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
		}

		/// <summary>
		/// Adds a requirement for there to be one or more of a given UnitType
		/// </summary>
		/// <param name='unitType'>
		/// The unit type to require.
		/// </param>
		public void AddUnitTypeRequirement(UnitType unitType)
		{
			AddUnitTypeRequirement(unitType, 1);
		}

		/// <summary>
		/// Checks whether the supplied army is currently valid according to this requirement.
		/// </summary>
		/// <returns>
		/// A <code>Validation</code> enum to show the result of the validation
		/// </returns>
		/// <param name='toValidate'>
		/// The army to validate
		/// </param>
		public override Validation ValidatesArmy(Army toValidate)
		{
			Validation isValid = Validation.Passed;

			foreach (UnitCountRequirementData requirement in requiredTypes)
			{
				if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
				{
					isValid = Validation.Failed;
					break;
				}
			}

			return isValid;
		}

		protected override string GetFailedRequirementsString(Army army)
		{
			return String.Join("; ", GetFailedRequirements(army).ToArray());
		}

		private List<string> GetFailedRequirements(Army army)
		{
			List<string> failures = new List<string>();

			foreach (UnitCountRequirementData requirement in requiredTypes)
			{
				int unitCount = army.GetUnitTypeCount(requirement.UnitType);

				if (unitCount < requirement.Count)
				{
					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")");
				}
			}

			return failures;
		}

		protected override string GetFailedAddingRequirementsString(UnitType toAdd, Army toArmy)
		{
			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
		}

		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
		{
			List<string> failures = new List<string>();

			foreach (UnitCountRequirementData requirement in requiredTypes)
			{
				int unitCount = GetUnitTypeCount(army, requirement.UnitType, unitType);

				if (unitCount < requirement.Count)
				{
					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")");
				}
			}

			return failures;
		}
	}
}