changeset 453:dace3b7779ca

Re #379: Fix validation of requirements to check for unit * Restructure all validation messages to a common base system that will translate better * Update "No More Than" texts to differentiate two contexts (adding and validating)
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jan 2012 20:05:25 +0000
parents b671085871b7
children def5d333c5e8
files API/Objects/Requirement/AbstractRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 5 files changed, 53 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/AbstractRequirement.cs	Sun Jan 29 19:39:25 2012 +0000
+++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun Jan 29 20:05:25 2012 +0000
@@ -3,14 +3,13 @@
 // 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;
 
-
 namespace IBBoard.WarFoundry.API.Objects.Requirement
 {
 	public abstract class AbstractRequirement : IRequirement
 	{
 		public abstract string RequirementID { get; }
 
-		public override bool Equals (object obj)
+		public override bool Equals(object obj)
 		{
 			if (obj == null)
 			{
@@ -54,7 +53,6 @@
 			return true;
 		}
 
-
 		public string GetValidationMessage(Army army)
 		{
 			string message = "";
@@ -68,7 +66,14 @@
 			return message;
 		}
 
-		protected abstract string GetValidationFailedMessage(Army army);
+		protected virtual string ValidationFailedMessage { get { return "Army must contain: {0}."; } }
+
+		private string GetValidationFailedMessage(Army army)
+		{
+			return String.Format(ValidationFailedMessage, GetFailedRequirementsString(army));
+		}
+
+		protected abstract string GetFailedRequirementsString(Army army);
 
 		public string GetAllowsAddingMessage(UnitType toAdd, Army toArmy)
 		{
@@ -83,7 +88,14 @@
 			return message;
 		}
 
-		protected abstract string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy);
+		protected virtual string AllowsAddingFailedMessage { get { return ValidationFailedMessage; } }
+
+		protected string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		{
+			return String.Format(AllowsAddingFailedMessage, GetFailedAddingRequirementsString(toAdd, toArmy));
+		}
+
+		protected abstract string GetFailedAddingRequirementsString(UnitType toAdd, Army toArmy);
 
 		public abstract  Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
 
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun Jan 29 19:39:25 2012 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun Jan 29 20:05:25 2012 +0000
@@ -202,11 +202,9 @@
 			return isValid;
 		}
 
-		protected virtual string ValidationFailedMessage { get { return "Army must contain: {0}."; } }
-
-		protected override string GetValidationFailedMessage(Army army)
+		protected override string GetFailedRequirementsString(Army army)
 		{
-			return String.Format(ValidationFailedMessage, String.Join("; ", GetFailedRequirements(army).ToArray()));
+			return String.Join("; ", GetFailedRequirements(army).ToArray());
 		}
 
 		private List<string> GetFailedRequirements(Army army)
@@ -226,11 +224,9 @@
 			return failures;
 		}
 
-		protected virtual string AllowsAddingFailedMessage { get { return ValidationFailedMessage; } }
-
-		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		protected override string GetFailedAddingRequirementsString(UnitType toAdd, Army toArmy)
 		{
-			return String.Format(AllowsAddingFailedMessage, String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
+			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
 		}
 
 		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
--- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Sun Jan 29 19:39:25 2012 +0000
+++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Sun Jan 29 20:05:25 2012 +0000
@@ -16,7 +16,6 @@
 		public RequiresNUnitsForMUnitsRequirement(UnitType allowedType, params UnitType[] requiredUnitTypes)
 		{
 			this.allowedType = allowedType;
-			FailureStringPrefix = "Army must contain: ";
 			requiredTypes = new List<UnitCountRequirementData>();
 
 			foreach (UnitType unitType in requiredUnitTypes)
@@ -50,15 +49,9 @@
 			}
 		}
 
-		protected string FailureStringPrefix { get; set; }
-
-		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		protected override string GetFailedAddingRequirementsString(UnitType toAdd, Army toArmy)
 		{
-			StringBuilder sb = new StringBuilder();
-			sb.Append(FailureStringPrefix);
-			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
-			sb.Append(".");
-			return sb.ToString();
+			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
 		}
 
 		private List<string> GetFailedAddingRequirements(UnitType unitType, Army toArmy)
@@ -180,13 +173,9 @@
 			return canAdd;
 		}
 
-		protected override string GetValidationFailedMessage(Army army)
+		protected override string GetFailedRequirementsString(Army army)
 		{
-			StringBuilder sb = new StringBuilder();
-			sb.Append(FailureStringPrefix);
-			sb.Append(String.Join("; ", GetFailedValidationRequirements(army).ToArray()));
-			sb.Append(".");
-			return sb.ToString();
+			return String.Join("; ", GetFailedValidationRequirements(army).ToArray());
 		}
 
 		private List<string> GetFailedValidationRequirements(Army army)
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Jan 29 19:39:25 2012 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Jan 29 20:05:25 2012 +0000
@@ -18,7 +18,6 @@
 
 		public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes)
 		{
-			FailureStringPrefix = "Army cannot contain more than: ";
 			limitedTypes = new List<UnitCountRequirementData>();
 
 			foreach (UnitType unitType in limitedUnitTypes)
@@ -141,15 +140,17 @@
 			return hash;
 		}
 
-		protected string FailureStringPrefix { get; set; }
-
-		protected override string GetValidationFailedMessage (Army army)
+		protected override string ValidationFailedMessage
 		{
-			StringBuilder sb = new StringBuilder();
-			sb.Append(FailureStringPrefix);
-			sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray()));
-			sb.Append(".");
-			return sb.ToString();
+			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)
@@ -169,13 +170,9 @@
 			return failures;
 		}
 
-		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
+		protected override string GetFailedAddingRequirementsString(UnitType toAdd, Army toArmy)
 		{
-			StringBuilder sb = new StringBuilder();
-			sb.Append(FailureStringPrefix);
-			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
-			sb.Append(".");
-			return sb.ToString();
+			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
 		}
 
 		private List<string> GetFailedAddingRequirements(UnitType unitType, Army army)
--- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Jan 29 19:39:25 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Jan 29 20:05:25 2012 +0000
@@ -17,7 +17,6 @@
 		public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base()
 		{
 			requirementOnType = requirementOn;
-			FailureStringPrefix = "Army contains " + requirementOn.Name + " and so cannot contain more than: ";
 		}
 
 		/// <summary>
@@ -37,23 +36,36 @@
 			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
 		}
 
-
 		protected override bool IsApplicable(WarFoundryObject toObject, Army toArmy)
 		{
 			return IsApplicable(toArmy) || IsApplicable(toObject);
 		}
 
-
 		protected override bool IsApplicable(Army toArmy)
 		{
 			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
 		}
 
-
 		protected override bool IsApplicable(WarFoundryObject toObject)
 		{
 			return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
 		}
+
+		protected override string ValidationFailedMessage
+		{
+			get
+			{
+				return "Army contains " + requirementOnType.Name + " and so cannot contain more than: {0}.";
+			}
+		}
+
+		protected override string AllowsAddingFailedMessage
+		{
+			get
+			{
+				return "Army would contain " + requirementOnType.Name + " and so cannot contain more than: {0}.";
+			}
+		}
 	}
 }