changeset 364:0dd8dbe8afe9

Fixes #345: Add failure message to requirements * Add implementation of failure messages for "allows adding"
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 19:17:40 +0000
parents 506af4cbe3f5
children 2fed577a1ea7
files API/Objects/Requirement/AbstractRequirement.cs API/Objects/Requirement/IRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 4 files changed, 88 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 19:16:23 2011 +0000
     1.2 +++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 19:17:40 2011 +0000
     1.3 @@ -50,12 +50,12 @@
     1.4  		}
     1.5  
     1.6  
     1.7 -		public string GetValidationMessage (Army army)
     1.8 +		public string GetValidationMessage(Army army)
     1.9  		{
    1.10  			string message = "";
    1.11  
    1.12 -			var obj = ValidatesArmy(army);
    1.13 -			if (!Validates.AsOkay(obj))
    1.14 +			Validation result = ValidatesArmy(army);
    1.15 +			if (!Validates.AsOkay(result))
    1.16  			{
    1.17  				message = GetValidationFailedMessage(army);
    1.18  			}
    1.19 @@ -63,11 +63,26 @@
    1.20  			return message;
    1.21  		}
    1.22  
    1.23 -		protected abstract string GetValidationFailedMessage (Army army);
    1.24 +		protected abstract string GetValidationFailedMessage(Army army);
    1.25 +
    1.26 +		public string GetAllowsAddingMessage(UnitType toAdd, Army toArmy)
    1.27 +		{
    1.28 +			string message = "";
    1.29 +
    1.30 +			Validation result = AllowsAdding(toAdd, toArmy);
    1.31 +			if (!Validates.AsOkay(result))
    1.32 +			{
    1.33 +				message = GetAllowsAddingFailedMessage(toAdd, toArmy);
    1.34 +			}
    1.35 +
    1.36 +			return message;
    1.37 +		}
    1.38 +
    1.39 +		protected abstract string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy);
    1.40  
    1.41 -		public abstract  Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
    1.42 +		public abstract  Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
    1.43  
    1.44 -		public abstract  Validation ValidatesArmy (Army army);
    1.45 +		public abstract  Validation ValidatesArmy(Army army);
    1.46  	}
    1.47  }
    1.48  
     2.1 --- a/API/Objects/Requirement/IRequirement.cs	Sun May 01 19:16:23 2011 +0000
     2.2 +++ b/API/Objects/Requirement/IRequirement.cs	Sun May 01 19:17:40 2011 +0000
     2.3 @@ -45,6 +45,20 @@
     2.4  		/// The army to validate.
     2.5  		/// </param>
     2.6  		string GetValidationMessage(Army army);
     2.7 +
     2.8 +		/// <summary>
     2.9 +		/// Gets the validation message for adding a unit to the army
    2.10 +		/// </summary>
    2.11 +		/// <returns>
    2.12 +		/// A validation message, if the type cannot be added, else an empty string.
    2.13 +		/// </returns>
    2.14 +		/// <param name='toAdd'>
    2.15 +		/// The unit type to try to add
    2.16 +		/// </param>
    2.17 +		/// <param name='toArmy'>
    2.18 +		/// The army the unit will be added to
    2.19 +		/// </param>
    2.20 +		string GetAllowsAddingMessage(UnitType toAdd, Army toArmy);
    2.21  	}
    2.22  }
    2.23  
     3.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 19:16:23 2011 +0000
     3.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 19:17:40 2011 +0000
     3.3 @@ -182,7 +182,7 @@
     3.4  
     3.5  		protected string FailureStringPrefix { get; set; }
     3.6  
     3.7 -		protected override string GetValidationFailedMessage (Army army)
     3.8 +		protected override string GetValidationFailedMessage(Army army)
     3.9  		{
    3.10  			StringBuilder sb = new StringBuilder();
    3.11  			sb.Append(FailureStringPrefix);
    3.12 @@ -207,6 +207,32 @@
    3.13  
    3.14  			return failures;
    3.15  		}
    3.16 +
    3.17 +		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
    3.18 +		{
    3.19 +			StringBuilder sb = new StringBuilder();
    3.20 +			sb.Append(FailureStringPrefix);
    3.21 +			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
    3.22 +			sb.Append(".");
    3.23 +			return sb.ToString();
    3.24 +		}
    3.25 +
    3.26 +		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
    3.27 +		{
    3.28 +			List<string> failures = new List<string>();
    3.29 +
    3.30 +			foreach (UnitCountRequirementData requirement in requiredTypes)
    3.31 +			{
    3.32 +				int unitCount = GetUnitTypeCount(army, requirement.UnitType, unitType);
    3.33 +
    3.34 +				if (unitCount < requirement.Count)
    3.35 +				{
    3.36 +					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")");
    3.37 +				}
    3.38 +			}
    3.39 +
    3.40 +			return failures;
    3.41 +		}
    3.42  	}
    3.43  }
    3.44  
     4.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 19:16:23 2011 +0000
     4.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 19:17:40 2011 +0000
     4.3 @@ -147,6 +147,32 @@
     4.4  
     4.5  			return failures;
     4.6  		}
     4.7 +
     4.8 +		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
     4.9 +		{
    4.10 +			StringBuilder sb = new StringBuilder();
    4.11 +			sb.Append(FailureStringPrefix);
    4.12 +			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
    4.13 +			sb.Append(".");
    4.14 +			return sb.ToString();
    4.15 +		}
    4.16 +
    4.17 +		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
    4.18 +		{
    4.19 +			List<string> failures = new List<string>();
    4.20 +
    4.21 +			foreach (UnitCountRequirementData requirement in limitedTypes)
    4.22 +			{
    4.23 +				int unitCount = GetUnitTypeCount(army, requirement.UnitType, unitType);
    4.24 +
    4.25 +				if (unitCount > requirement.Count)
    4.26 +				{
    4.27 +					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")");
    4.28 +				}
    4.29 +			}
    4.30 +
    4.31 +			return failures;
    4.32 +		}
    4.33  	}
    4.34  }
    4.35