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 wrap: on
line diff
--- a/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 19:16:23 2011 +0000
+++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 19:17:40 2011 +0000
@@ -50,12 +50,12 @@
 		}
 
 
-		public string GetValidationMessage (Army army)
+		public string GetValidationMessage(Army army)
 		{
 			string message = "";
 
-			var obj = ValidatesArmy(army);
-			if (!Validates.AsOkay(obj))
+			Validation result = ValidatesArmy(army);
+			if (!Validates.AsOkay(result))
 			{
 				message = GetValidationFailedMessage(army);
 			}
@@ -63,11 +63,26 @@
 			return message;
 		}
 
-		protected abstract string GetValidationFailedMessage (Army army);
+		protected abstract string GetValidationFailedMessage(Army army);
+
+		public string GetAllowsAddingMessage(UnitType toAdd, Army toArmy)
+		{
+			string message = "";
+
+			Validation result = AllowsAdding(toAdd, toArmy);
+			if (!Validates.AsOkay(result))
+			{
+				message = GetAllowsAddingFailedMessage(toAdd, toArmy);
+			}
+
+			return message;
+		}
+
+		protected abstract string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy);
 
-		public abstract  Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
+		public abstract  Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
 
-		public abstract  Validation ValidatesArmy (Army army);
+		public abstract  Validation ValidatesArmy(Army army);
 	}
 }
 
--- a/API/Objects/Requirement/IRequirement.cs	Sun May 01 19:16:23 2011 +0000
+++ b/API/Objects/Requirement/IRequirement.cs	Sun May 01 19:17:40 2011 +0000
@@ -45,6 +45,20 @@
 		/// The army to validate.
 		/// </param>
 		string GetValidationMessage(Army army);
+
+		/// <summary>
+		/// Gets the validation message for adding a unit to the army
+		/// </summary>
+		/// <returns>
+		/// A validation message, if the type cannot be added, else an empty string.
+		/// </returns>
+		/// <param name='toAdd'>
+		/// The unit type to try to add
+		/// </param>
+		/// <param name='toArmy'>
+		/// The army the unit will be added to
+		/// </param>
+		string GetAllowsAddingMessage(UnitType toAdd, Army toArmy);
 	}
 }
 
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 19:16:23 2011 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 19:17:40 2011 +0000
@@ -182,7 +182,7 @@
 
 		protected string FailureStringPrefix { get; set; }
 
-		protected override string GetValidationFailedMessage (Army army)
+		protected override string GetValidationFailedMessage(Army army)
 		{
 			StringBuilder sb = new StringBuilder();
 			sb.Append(FailureStringPrefix);
@@ -207,6 +207,32 @@
 
 			return failures;
 		}
+
+		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		{
+			StringBuilder sb = new StringBuilder();
+			sb.Append(FailureStringPrefix);
+			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
+			sb.Append(".");
+			return sb.ToString();
+		}
+
+		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;
+		}
 	}
 }
 
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 19:16:23 2011 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 19:17:40 2011 +0000
@@ -147,6 +147,32 @@
 
 			return failures;
 		}
+
+		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		{
+			StringBuilder sb = new StringBuilder();
+			sb.Append(FailureStringPrefix);
+			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
+			sb.Append(".");
+			return sb.ToString();
+		}
+
+		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
+		{
+			List<string> failures = new List<string>();
+
+			foreach (UnitCountRequirementData requirement in limitedTypes)
+			{
+				int unitCount = GetUnitTypeCount(army, requirement.UnitType, unitType);
+
+				if (unitCount > requirement.Count)
+				{
+					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")");
+				}
+			}
+
+			return failures;
+		}
 	}
 }