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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 13:56:20 2011 +0000
     1.3 @@ -0,0 +1,73 @@
     1.4 +// This file (AbstractRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     1.5 +// 
     1.6 +// 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.
     1.7 +using System;
     1.8 +
     1.9 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.10 +{
    1.11 +	public abstract class AbstractRequirement : IRequirement
    1.12 +	{
    1.13 +		public override bool Equals (object obj)
    1.14 +		{
    1.15 +			if (obj == null)
    1.16 +			{
    1.17 +				return false;
    1.18 +			}
    1.19 +			else if (obj.GetType().Equals(this.GetType()))
    1.20 +			{
    1.21 +				return TypeEquals(obj);
    1.22 +			}
    1.23 +			else
    1.24 +			{
    1.25 +				return false;
    1.26 +			}
    1.27 +		}
    1.28 +
    1.29 +		/// <summary>
    1.30 +		/// Type-specific equality checking - must be implemented by each class
    1.31 +		/// </summary>
    1.32 +		/// <returns>
    1.33 +		/// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code>
    1.34 +		/// </returns>
    1.35 +		/// <param name='obj'>
    1.36 +		/// The object to compare to
    1.37 +		/// </param>
    1.38 +		protected abstract bool TypeEquals(object obj);
    1.39 +
    1.40 +		protected virtual bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
    1.41 +		{
    1.42 +			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
    1.43 +		}
    1.44 +
    1.45 +		protected virtual bool IsApplicable(Army toArmy)
    1.46 +		{
    1.47 +			return true;
    1.48 +		}
    1.49 +
    1.50 +		protected virtual bool IsApplicable(WarFoundryObject toObject)
    1.51 +		{
    1.52 +			return true;
    1.53 +		}
    1.54 +
    1.55 +
    1.56 +		public string GetValidationMessage (Army army)
    1.57 +		{
    1.58 +			string message = "";
    1.59 +
    1.60 +			var obj = ValidatesArmy(army);
    1.61 +			if (!Validates.AsOkay(obj))
    1.62 +			{
    1.63 +				message = GetValidationFailedMessage(army);
    1.64 +			}
    1.65 +
    1.66 +			return message;
    1.67 +		}
    1.68 +
    1.69 +		protected abstract string GetValidationFailedMessage (Army army);
    1.70 +
    1.71 +		public abstract  Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
    1.72 +
    1.73 +		public abstract  Validation ValidatesArmy (Army army);
    1.74 +	}
    1.75 +}
    1.76 +
     2.1 --- a/API/Objects/Requirement/IRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
     2.2 +++ b/API/Objects/Requirement/IRequirement.cs	Sun May 01 13:56:20 2011 +0000
     2.3 @@ -1,39 +1,50 @@
     2.4 -// This file (Requirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     2.5 +// This file (Requirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     2.6  // 
     2.7  // 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.
     2.8  using System;
     2.9  
    2.10  namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.11 -{
    2.12 -	/// <summary>
    2.13 -	/// Base interface for a Requirement that constrains the units/equipment that can be taken in an army
    2.14 +{
    2.15 +	/// <summary>
    2.16 +	/// Base interface for a Requirement that constrains the units/equipment that can be taken in an army
    2.17  	/// </summary>
    2.18  	public interface IRequirement
    2.19 -	{
    2.20 -		/// <summary>
    2.21 -		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    2.22 -		/// </summary>
    2.23 -		/// <returns>
    2.24 -		/// A <code>Validation</code> enum to show the result of the validation
    2.25 -		/// </returns>
    2.26 -		/// <param name='wfObject'>
    2.27 -		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    2.28 -		/// </param>
    2.29 -		/// <param name='toArmy'>
    2.30 -		/// The army to add the object to.
    2.31 -		/// </param>
    2.32 -		Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
    2.33 -
    2.34 -		/// <summary>
    2.35 -		/// Checks whether the supplied army is currently valid according to this requirement.
    2.36 -		/// </summary>
    2.37 -		/// <returns>
    2.38 -		/// A <code>Validation</code> enum to show the result of the validation
    2.39 -		/// </returns>
    2.40 -		/// <param name='toValidate'>
    2.41 -		/// The army to validate
    2.42 -		/// </param>
    2.43 +	{
    2.44 +		/// <summary>
    2.45 +		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    2.46 +		/// </summary>
    2.47 +		/// <returns>
    2.48 +		/// A <code>Validation</code> enum to show the result of the validation
    2.49 +		/// </returns>
    2.50 +		/// <param name='wfObject'>
    2.51 +		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    2.52 +		/// </param>
    2.53 +		/// <param name='toArmy'>
    2.54 +		/// The army to add the object to.
    2.55 +		/// </param>
    2.56 +		Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy);
    2.57 +
    2.58 +		/// <summary>
    2.59 +		/// Checks whether the supplied army is currently valid according to this requirement.
    2.60 +		/// </summary>
    2.61 +		/// <returns>
    2.62 +		/// A <code>Validation</code> enum to show the result of the validation
    2.63 +		/// </returns>
    2.64 +		/// <param name='toValidate'>
    2.65 +		/// The army to validate
    2.66 +		/// </param>
    2.67  		Validation ValidatesArmy(Army army);
    2.68 +
    2.69 +		/// <summary>
    2.70 +		/// Gets the validation message from validating the army
    2.71 +		/// </summary>
    2.72 +		/// <returns>
    2.73 +		/// A validation message, if the validation fails, else an empty string.
    2.74 +		/// </returns>
    2.75 +		/// <param name='army'>
    2.76 +		/// The army to validate.
    2.77 +		/// </param>
    2.78 +		string GetValidationMessage(Army army);
    2.79  	}
    2.80  }
    2.81  
     3.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
     3.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 13:56:20 2011 +0000
     3.3 @@ -10,7 +10,7 @@
     3.4  	/// <summary>
     3.5  	/// 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.
     3.6  	/// </summary>
     3.7 -	public class RequiresAtLeastNUnitsRequirement : IRequirement
     3.8 +	public class RequiresAtLeastNUnitsRequirement : AbstractRequirement
     3.9  	{
    3.10  		private List<UnitCountRequirementData> requiredTypes;
    3.11  
    3.12 @@ -24,27 +24,16 @@
    3.13  			}
    3.14  		}
    3.15  
    3.16 -		public override bool Equals (object obj)
    3.17 +		protected override bool TypeEquals (object obj)
    3.18  		{
    3.19 -			if (obj == null)
    3.20 +			RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj;
    3.21 +			if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
    3.22  			{
    3.23  				return false;
    3.24  			}
    3.25 -			else if (obj.GetType().Equals(this.GetType()))
    3.26 -			{
    3.27 -				RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj;
    3.28 -				if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
    3.29 -				{
    3.30 -					return false;
    3.31 -				}
    3.32 -				else
    3.33 -				{
    3.34 -					return true;
    3.35 -				}
    3.36 -			}
    3.37  			else
    3.38  			{
    3.39 -				return false;
    3.40 +				return true;
    3.41  			}
    3.42  		}
    3.43  
    3.44 @@ -60,22 +49,17 @@
    3.45  		/// <param name='toArmy'>
    3.46  		/// The army to add the object to.
    3.47  		/// </param>
    3.48 -		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    3.49 +		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    3.50  		{
    3.51  			return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
    3.52  		}
    3.53  
    3.54 -		private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
    3.55 -		{
    3.56 -			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
    3.57 -		}
    3.58 -
    3.59 -		protected virtual bool IsApplicable(Army toArmy)
    3.60 +		protected override bool IsApplicable(Army toArmy)
    3.61  		{
    3.62  			return false;
    3.63  		}
    3.64  
    3.65 -		protected virtual bool IsApplicable(WarFoundryObject toObject)
    3.66 +		protected override bool IsApplicable(WarFoundryObject toObject)
    3.67  		{
    3.68  			bool isApplicable = false;
    3.69  			UnitType unitType = GetUnitTypeFromObject(toObject);
    3.70 @@ -178,7 +162,7 @@
    3.71  		/// <param name='toValidate'>
    3.72  		/// The army to validate
    3.73  		/// </param>
    3.74 -		public virtual Validation ValidatesArmy(Army toValidate)
    3.75 +		public override Validation ValidatesArmy(Army toValidate)
    3.76  		{
    3.77  			Validation isValid = Validation.Passed;
    3.78  
    3.79 @@ -193,6 +177,12 @@
    3.80  
    3.81  			return isValid;
    3.82  		}
    3.83 +
    3.84 +		protected override string GetValidationFailedMessage (Army army)
    3.85 +		{
    3.86 +			string message = "";
    3.87 +			return message;
    3.88 +		}
    3.89  	}
    3.90  }
    3.91  
     4.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Tue Apr 26 19:19:08 2011 +0000
     4.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun May 01 13:56:20 2011 +0000
     4.3 @@ -10,7 +10,7 @@
     4.4  	/// <summary>
     4.5  	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
     4.6  	/// </summary>
     4.7 -	public class RequiresNoMoreThanNOfUnitTypeRequirement : IRequirement
     4.8 +	public class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractRequirement
     4.9  	{
    4.10  		private List<UnitCountRequirementData> limitedTypes;
    4.11  
    4.12 @@ -36,7 +36,7 @@
    4.13  		/// <param name='toArmy'>
    4.14  		/// The army to add the object to.
    4.15  		/// </param>
    4.16 -		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    4.17 +		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    4.18  		{
    4.19  			Validation canAdd = Validation.Passed;
    4.20  			
    4.21 @@ -96,7 +96,7 @@
    4.22  		/// <param name='toValidate'>
    4.23  		/// The army to validate
    4.24  		/// </param>
    4.25 -		public Validation ValidatesArmy(Army army)
    4.26 +		public override Validation ValidatesArmy(Army army)
    4.27  		{
    4.28  			Validation canAdd = Validation.Passed;
    4.29  			
    4.30 @@ -112,28 +112,22 @@
    4.31  			return canAdd;
    4.32  		}
    4.33  
    4.34 -		public override bool Equals(object obj)
    4.35 +		protected override bool TypeEquals(object obj)
    4.36  		{
    4.37 -			if (obj == null)
    4.38 -			{
    4.39 -				return false;
    4.40 -			}
    4.41 -			else if (!(obj is RequiresNoMoreThanNOfUnitTypeRequirement))
    4.42 -			{
    4.43 -				return false;
    4.44 -			}
    4.45 -			else
    4.46 -			{
    4.47 -				RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
    4.48 -
    4.49 -				return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
    4.50 -			}
    4.51 +			RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
    4.52 +			return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
    4.53  		}
    4.54  
    4.55  		public override int GetHashCode()
    4.56  		{
    4.57  			return base.GetHashCode ();
    4.58  		}
    4.59 +
    4.60 +		protected override string GetValidationFailedMessage (Army army)
    4.61 +		{
    4.62 +			string message = "";
    4.63 +			return message;
    4.64 +		}
    4.65  	}
    4.66  }
    4.67  
     5.1 --- a/IBBoard.WarFoundry.API.csproj	Tue Apr 26 19:19:08 2011 +0000
     5.2 +++ b/IBBoard.WarFoundry.API.csproj	Sun May 01 13:56:20 2011 +0000
     5.3 @@ -193,6 +193,7 @@
     5.4      <Compile Include="API\Objects\Requirement\UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs" />
     5.5      <Compile Include="API\Objects\Requirement\IRequirement.cs" />
     5.6      <Compile Include="API\Objects\Requirement\RequirementHandler.cs" />
     5.7 +    <Compile Include="API\Objects\Requirement\AbstractRequirement.cs" />
     5.8    </ItemGroup>
     5.9    <ItemGroup>
    5.10      <Reference Include="System.Xml" />