changeset 358:dbe7ccb1e557

Re #345: Add failure message to requirements * Add abstract requirement class to handle some of the message commonality
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 13:56:20 +0000
parents 50d0d3b39a0b
children 2a9c046be55a
files API/Objects/Requirement/AbstractRequirement.cs API/Objects/Requirement/IRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs IBBoard.WarFoundry.API.csproj
diffstat 5 files changed, 140 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 13:56:20 2011 +0000
@@ -0,0 +1,73 @@
+// This file (AbstractRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
+// 
+// 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 override bool Equals (object obj)
+		{
+			if (obj == null)
+			{
+				return false;
+			}
+			else if (obj.GetType().Equals(this.GetType()))
+			{
+				return TypeEquals(obj);
+			}
+			else
+			{
+				return false;
+			}
+		}
+
+		/// <summary>
+		/// Type-specific equality checking - must be implemented by each class
+		/// </summary>
+		/// <returns>
+		/// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code>
+		/// </returns>
+		/// <param name='obj'>
+		/// The object to compare to
+		/// </param>
+		protected abstract bool TypeEquals(object obj);
+
+		protected virtual bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
+		{
+			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
+		}
+
+		protected virtual bool IsApplicable(Army toArmy)
+		{
+			return true;
+		}
+
+		protected virtual bool IsApplicable(WarFoundryObject toObject)
+		{
+			return true;
+		}
+
+
+		public string GetValidationMessage (Army army)
+		{
+			string message = "";
+
+			var obj = ValidatesArmy(army);
+			if (!Validates.AsOkay(obj))
+			{
+				message = GetValidationFailedMessage(army);
+			}
+
+			return message;
+		}
+
+		protected abstract string GetValidationFailedMessage (Army army);
+
+		public abstract  Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
+
+		public abstract  Validation ValidatesArmy (Army army);
+	}
+}
+
--- a/API/Objects/Requirement/IRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
+++ b/API/Objects/Requirement/IRequirement.cs	Sun May 01 13:56:20 2011 +0000
@@ -1,39 +1,50 @@
-// This file (Requirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
+// This file (Requirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
 // 
 // 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
-{
-	/// <summary>
-	/// Base interface for a Requirement that constrains the units/equipment that can be taken in an army
+{
+	/// <summary>
+	/// Base interface for a Requirement that constrains the units/equipment that can be taken in an army
 	/// </summary>
 	public interface IRequirement
-	{
-		/// <summary>
-		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
-		/// </summary>
-		/// <returns>
-		/// A <code>Validation</code> enum to show the result of the validation
-		/// </returns>
-		/// <param name='wfObject'>
-		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
-		/// </param>
-		/// <param name='toArmy'>
-		/// The army to add the object to.
-		/// </param>
-		Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
-
-		/// <summary>
-		/// Checks whether the supplied army is currently valid according to this requirement.
-		/// </summary>
-		/// <returns>
-		/// A <code>Validation</code> enum to show the result of the validation
-		/// </returns>
-		/// <param name='toValidate'>
-		/// The army to validate
-		/// </param>
+	{
+		/// <summary>
+		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
+		/// </summary>
+		/// <returns>
+		/// A <code>Validation</code> enum to show the result of the validation
+		/// </returns>
+		/// <param name='wfObject'>
+		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
+		/// </param>
+		/// <param name='toArmy'>
+		/// The army to add the object to.
+		/// </param>
+		Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
+
+		/// <summary>
+		/// Checks whether the supplied army is currently valid according to this requirement.
+		/// </summary>
+		/// <returns>
+		/// A <code>Validation</code> enum to show the result of the validation
+		/// </returns>
+		/// <param name='toValidate'>
+		/// The army to validate
+		/// </param>
 		Validation ValidatesArmy(Army army);
+
+		/// <summary>
+		/// Gets the validation message from validating the army
+		/// </summary>
+		/// <returns>
+		/// A validation message, if the validation fails, else an empty string.
+		/// </returns>
+		/// <param name='army'>
+		/// The army to validate.
+		/// </param>
+		string GetValidationMessage(Army army);
 	}
 }
 
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 13:56:20 2011 +0000
@@ -10,7 +10,7 @@
 	/// <summary>
 	/// A requirement where a WarFoundryObject requires at least N units of one or more unit types before any number of that object can be taken in an army.
 	/// </summary>
-	public class RequiresAtLeastNUnitsRequirement : IRequirement
+	public class RequiresAtLeastNUnitsRequirement : AbstractRequirement
 	{
 		private List<UnitCountRequirementData> requiredTypes;
 
@@ -24,27 +24,16 @@
 			}
 		}
 
-		public override bool Equals (object obj)
+		protected override bool TypeEquals (object obj)
 		{
-			if (obj == null)
+			RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj;
+			if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
 			{
 				return false;
 			}
-			else if (obj.GetType().Equals(this.GetType()))
-			{
-				RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj;
-				if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
-				{
-					return false;
-				}
-				else
-				{
-					return true;
-				}
-			}
 			else
 			{
-				return false;
+				return true;
 			}
 		}
 
@@ -60,22 +49,17 @@
 		/// <param name='toArmy'>
 		/// The army to add the object to.
 		/// </param>
-		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
+		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
 		{
 			return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
 		}
 
-		private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
-		{
-			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
-		}
-
-		protected virtual bool IsApplicable(Army toArmy)
+		protected override bool IsApplicable(Army toArmy)
 		{
 			return false;
 		}
 
-		protected virtual bool IsApplicable(WarFoundryObject toObject)
+		protected override bool IsApplicable(WarFoundryObject toObject)
 		{
 			bool isApplicable = false;
 			UnitType unitType = GetUnitTypeFromObject(toObject);
@@ -178,7 +162,7 @@
 		/// <param name='toValidate'>
 		/// The army to validate
 		/// </param>
-		public virtual Validation ValidatesArmy(Army toValidate)
+		public override Validation ValidatesArmy(Army toValidate)
 		{
 			Validation isValid = Validation.Passed;
 
@@ -193,6 +177,12 @@
 
 			return isValid;
 		}
+
+		protected override string GetValidationFailedMessage (Army army)
+		{
+			string message = "";
+			return message;
+		}
 	}
 }
 
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 13:56:20 2011 +0000
@@ -10,7 +10,7 @@
 	/// <summary>
 	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
 	/// </summary>
-	public class RequiresNoMoreThanNOfUnitTypeRequirement : IRequirement
+	public class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractRequirement
 	{
 		private List<UnitCountRequirementData> limitedTypes;
 
@@ -36,7 +36,7 @@
 		/// <param name='toArmy'>
 		/// The army to add the object to.
 		/// </param>
-		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
+		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
 		{
 			Validation canAdd = Validation.Passed;
 			
@@ -96,7 +96,7 @@
 		/// <param name='toValidate'>
 		/// The army to validate
 		/// </param>
-		public Validation ValidatesArmy(Army army)
+		public override Validation ValidatesArmy(Army army)
 		{
 			Validation canAdd = Validation.Passed;
 			
@@ -112,28 +112,22 @@
 			return canAdd;
 		}
 
-		public override bool Equals(object obj)
+		protected override bool TypeEquals(object obj)
 		{
-			if (obj == null)
-			{
-				return false;
-			}
-			else if (!(obj is RequiresNoMoreThanNOfUnitTypeRequirement))
-			{
-				return false;
-			}
-			else
-			{
-				RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
-
-				return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
-			}
+			RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
+			return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
 		}
 
 		public override int GetHashCode()
 		{
 			return base.GetHashCode ();
 		}
+
+		protected override string GetValidationFailedMessage (Army army)
+		{
+			string message = "";
+			return message;
+		}
 	}
 }
 
--- a/IBBoard.WarFoundry.API.csproj	Tue Apr 26 19:19:08 2011 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Sun May 01 13:56:20 2011 +0000
@@ -193,6 +193,7 @@
     <Compile Include="API\Objects\Requirement\UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs" />
     <Compile Include="API\Objects\Requirement\IRequirement.cs" />
     <Compile Include="API\Objects\Requirement\RequirementHandler.cs" />
+    <Compile Include="API\Objects\Requirement\AbstractRequirement.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System.Xml" />