diff API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 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 777725613edb
children 71fceea2725b
line wrap: on
line diff
--- 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;
+		}
 	}
 }