changeset 362:c19e61262400

Re #345: Add failure message to requirements * Add implementation that lists only failed parts for "no more than" requirements
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 15:39:05 +0000
parents 8781e53c59bb
children 506af4cbe3f5
files API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 2 files changed, 85 insertions(+), 65 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 14:45:56 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 15:39:05 2011 +0000
     1.3 @@ -4,6 +4,7 @@
     1.4  using System;
     1.5  using IBBoard.WarFoundry.API.Objects;
     1.6  using System.Collections.Generic;
     1.7 +using System.Text;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.10  {
    1.11 @@ -16,6 +17,7 @@
    1.12  
    1.13  		public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes)
    1.14  		{
    1.15 +			FailureStringPrefix = "Army cannot contain more than: ";
    1.16  			limitedTypes = new List<UnitCountRequirementData>();
    1.17  
    1.18  			foreach (UnitType unitType in limitedUnitTypes)
    1.19 @@ -116,17 +118,34 @@
    1.20  		{
    1.21  			RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
    1.22  			return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
    1.23 -		}
    1.24 -
    1.25 -		public override int GetHashCode()
    1.26 -		{
    1.27 -			return base.GetHashCode ();
    1.28  		}
    1.29  
    1.30 +		protected string FailureStringPrefix { get; set; }
    1.31 +
    1.32  		protected override string GetValidationFailedMessage (Army army)
    1.33  		{
    1.34 -			string message = "";
    1.35 -			return message;
    1.36 +			StringBuilder sb = new StringBuilder();
    1.37 +			sb.Append(FailureStringPrefix);
    1.38 +			sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray()));
    1.39 +			sb.Append(".");
    1.40 +			return sb.ToString();
    1.41 +		}
    1.42 +
    1.43 +		private List<string> GetFailedRequirements(Army army)
    1.44 +		{
    1.45 +			List<string> failures = new List<string>();
    1.46 +
    1.47 +			foreach (UnitCountRequirementData requirement in limitedTypes)
    1.48 +			{
    1.49 +				int unitCount = army.GetUnitTypeCount(requirement.UnitType);
    1.50 +
    1.51 +				if (unitCount > requirement.Count)
    1.52 +				{
    1.53 +					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")");
    1.54 +				}
    1.55 +			}
    1.56 +
    1.57 +			return failures;
    1.58  		}
    1.59  	}
    1.60  }
     2.1 --- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 14:45:56 2011 +0000
     2.2 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 15:39:05 2011 +0000
     2.3 @@ -1,58 +1,59 @@
     2.4 -// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     2.5 -// 
     2.6 -// 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.
     2.7 -using System;
     2.8 -using System.Collections.Generic;
     2.9 -using IBBoard.WarFoundry.API.Objects;
    2.10 -
    2.11 -namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.12 -{
    2.13 -	/// <summary>
    2.14 -	/// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army.
    2.15 -	/// </summary>
    2.16 -	public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement
    2.17 -	{
    2.18 -		private UnitType requirementOnType;
    2.19 -
    2.20 -		public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base()
    2.21 -		{
    2.22 -			requirementOnType = requirementOn;
    2.23 -		}
    2.24 -
    2.25 -		/// <summary>
    2.26 -		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    2.27 -		/// </summary>
    2.28 -		/// <returns>
    2.29 -		/// A <code>Validation</code> enum to show the result of the validation
    2.30 -		/// </returns>
    2.31 -		/// <param name='wfObject'>
    2.32 -		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    2.33 -		/// </param>
    2.34 -		/// <param name='toArmy'>
    2.35 -		/// The army to add the object to.
    2.36 -		/// </param>
    2.37 -		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    2.38 -		{
    2.39 -			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
    2.40 -		}
    2.41 -
    2.42 -
    2.43 -		private bool IsApplicable(WarFoundryObject toObject, Army toArmy)
    2.44 -		{
    2.45 -			return IsApplicable(toArmy) || IsApplicable(toObject);
    2.46 -		}
    2.47 -
    2.48 -
    2.49 -		private bool IsApplicable(Army toArmy)
    2.50 -		{
    2.51 -			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
    2.52 -		}
    2.53 -
    2.54 -
    2.55 -		private bool IsApplicable(WarFoundryObject toObject)
    2.56 -		{
    2.57 -			return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
    2.58 -		}
    2.59 -	}
    2.60 -}
    2.61 -
    2.62 +// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
    2.63 +// 
    2.64 +// 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.
    2.65 +using System;
    2.66 +using System.Collections.Generic;
    2.67 +using IBBoard.WarFoundry.API.Objects;
    2.68 +
    2.69 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.70 +{
    2.71 +	/// <summary>
    2.72 +	/// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army.
    2.73 +	/// </summary>
    2.74 +	public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement
    2.75 +	{
    2.76 +		private UnitType requirementOnType;
    2.77 +
    2.78 +		public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base()
    2.79 +		{
    2.80 +			requirementOnType = requirementOn;
    2.81 +			FailureStringPrefix = "Army contains " + requirementOn.Name + " and so cannot contain more than: ";
    2.82 +		}
    2.83 +
    2.84 +		/// <summary>
    2.85 +		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    2.86 +		/// </summary>
    2.87 +		/// <returns>
    2.88 +		/// A <code>Validation</code> enum to show the result of the validation
    2.89 +		/// </returns>
    2.90 +		/// <param name='wfObject'>
    2.91 +		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    2.92 +		/// </param>
    2.93 +		/// <param name='toArmy'>
    2.94 +		/// The army to add the object to.
    2.95 +		/// </param>
    2.96 +		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    2.97 +		{
    2.98 +			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
    2.99 +		}
   2.100 +
   2.101 +
   2.102 +		private bool IsApplicable(WarFoundryObject toObject, Army toArmy)
   2.103 +		{
   2.104 +			return IsApplicable(toArmy) || IsApplicable(toObject);
   2.105 +		}
   2.106 +
   2.107 +
   2.108 +		private bool IsApplicable(Army toArmy)
   2.109 +		{
   2.110 +			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
   2.111 +		}
   2.112 +
   2.113 +
   2.114 +		private bool IsApplicable(WarFoundryObject toObject)
   2.115 +		{
   2.116 +			return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
   2.117 +		}
   2.118 +	}
   2.119 +}
   2.120 +