view API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs @ 461:f0594621e4a0

Re #379: Fix validation of requirements to check for unit * Move "Is applicable" checks up to abstract "no more than N of unit type" class
author IBBoard <dev@ibboard.co.uk>
date Mon, 05 Mar 2012 19:52:54 +0000
parents 680db2462e34
children 159dc9be36c2
line wrap: on
line source

// This file (RequiresNoMoreThanNOfUnitTypeRequirement.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 System.Collections.Generic;
using System.Text;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	/// <summary>
	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
	/// </summary>
	public abstract class RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
	{
		public static readonly string REQUIREMENT_ID = "RequiresNoMoreThanNUnits";

		public RequiresNoMoreThanNOfUnitTypeRequirement(OBJECT_TYPE allowedObject, params UnitType[] limitedUnitTypes) : base(allowedObject, limitedUnitTypes)
		{
			//Do nothing special
		}

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

		/// <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(IWarFoundryObject wfObject, Army toArmy)
		{
			return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
		}

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

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

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

			return isApplicable;
		}

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

		private Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy)
		{
			Validation canAdd = Validation.Passed;
			
			foreach (UnitCountRequirementData limit in ConstraintTypes)
			{
				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
				{
					canAdd = Validation.Failed;
					break;
				}
			}
			
			return canAdd;
		}

		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 no more than maxCount of a given UnitType
		/// </summary>
		/// <param name='unitType'>
		/// The unit type to limit.
		/// </param>
		/// <param name='minCount'>
		/// The maximum number of that type that must exist.
		/// </param>
		public void AddUnitTypeRequirement(UnitType unitType, int maxCount)
		{
			ConstraintTypes.Add(new UnitCountRequirementData(unitType, maxCount));
		}

		/// <summary>
		/// Adds a requirement for there to be none of a given UnitType
		/// </summary>
		/// <param name='unitType'>
		/// The unit type to limit.
		/// </param>
		public override void AddUnitTypeRequirement(UnitType unitType)
		{
			AddUnitTypeRequirement(unitType, 0);
		}

		/// <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 army)
		{
			Validation canAdd = Validation.NotApplicable;
			int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject);

			if (allowedTypeCount > 0)
			{
				canAdd = Validation.Passed;

				foreach (UnitCountRequirementData limit in ConstraintTypes)
				{
					if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
					{
						canAdd = Validation.Failed;
						break;
					}
				}
			}
			else
			{
				canAdd = Validation.NotApplicable;
			}
			
			return canAdd;
		}

		protected override bool TypeEquals(object obj)
		{
			RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> other = (RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE>)obj;
			return Collections.Collections.AreEqual(ConstraintTypes, other.ConstraintTypes);
		}

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

			foreach (UnitCountRequirementData limit in ConstraintTypes)
			{
				hash += limit.UnitType.GetHashCode();
			}

			return hash;
		}

		protected override string ValidationFailedMessage
		{
			get
			{
				return "Army cannot contain more than: {0}.";
			}
		}

		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 ConstraintTypes)
			{
				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(IWarFoundryObject toAdd, Army toArmy)
		{
			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
		}

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

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

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

			return failures;
		}

		protected override int GetObjectCountFromArmy(Army toArmy)
		{
			return 0;
		}
	}
}