changeset 360:777725613edb

Re #345: Add failure message to requirements * Add protected property to hold prefix for validation string, as that is all that changes
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 14:33:27 +0000
parents 2a9c046be55a
children 8781e53c59bb
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs
diffstat 2 files changed, 48 insertions(+), 44 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 14:23:51 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 14:33:27 2011 +0000
     1.3 @@ -17,6 +17,7 @@
     1.4  
     1.5  		public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes)
     1.6  		{
     1.7 +			FailureStringPrefix = "Army must contain: ";
     1.8  			requiredTypes = new List<UnitCountRequirementData>();
     1.9  
    1.10  			foreach (UnitType unitType in requiredUnitTypes)
    1.11 @@ -179,10 +180,12 @@
    1.12  			return isValid;
    1.13  		}
    1.14  
    1.15 +		protected string FailureStringPrefix { get; set; }
    1.16 +
    1.17  		protected override string GetValidationFailedMessage (Army army)
    1.18  		{
    1.19  			StringBuilder sb = new StringBuilder();
    1.20 -			sb.Append("Army must contain: ");
    1.21 +			sb.Append(FailureStringPrefix);
    1.22  			sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray()));
    1.23  			sb.Append(".");
    1.24  			return sb.ToString();
     2.1 --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sun May 01 14:23:51 2011 +0000
     2.2 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sun May 01 14:33:27 2011 +0000
     2.3 @@ -1,43 +1,44 @@
     2.4 -// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     2.5 -// 
     2.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.
     2.7 -using System;
     2.8 -using System.Collections.Generic;
     2.9 -using IBBoard.WarFoundry.API.Objects;
    2.10 -
    2.11 -namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.12 -{
    2.13 -	/// <summary>
    2.14 -	/// A requirement where a UnitType requires at least N units of one or more unit types before any number of that object can be taken in an army.
    2.15 -	/// </summary>
    2.16 -	public class UnitRequiresAtLeastNUnitsRequirement : RequiresAtLeastNUnitsRequirement
    2.17 -	{
    2.18 -		private UnitType requirementOnType;
    2.19 -
    2.20 -		public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base()
    2.21 -		{
    2.22 -			requirementOnType = requirementOn;
    2.23 -		}
    2.24 -
    2.25 -		protected override bool IsApplicable(WarFoundryObject toObjectAdded)
    2.26 -		{
    2.27 -			return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded);
    2.28 -		}
    2.29 -
    2.30 -		private bool IsRequirementOnType(WarFoundryObject toObjectAdded)
    2.31 -		{
    2.32 -			return requirementOnType.Equals(toObjectAdded) || (toObjectAdded is Unit && requirementOnType.Equals(((Unit)toObjectAdded).UnitType));
    2.33 -		}
    2.34 -
    2.35 -		protected override bool IsApplicable(Army toArmy)
    2.36 -		{
    2.37 -			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
    2.38 -		}
    2.39 -
    2.40 -		public override Validation ValidatesArmy(Army toArmy)
    2.41 -		{
    2.42 -			return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;
    2.43 -		}
    2.44 -	}
    2.45 -}
    2.46 -
    2.47 +// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
    2.48 +// 
    2.49 +// 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.50 +using System;
    2.51 +using System.Collections.Generic;
    2.52 +using IBBoard.WarFoundry.API.Objects;
    2.53 +
    2.54 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.55 +{
    2.56 +	/// <summary>
    2.57 +	/// A requirement where a UnitType requires at least N units of one or more unit types before any number of that object can be taken in an army.
    2.58 +	/// </summary>
    2.59 +	public class UnitRequiresAtLeastNUnitsRequirement : RequiresAtLeastNUnitsRequirement
    2.60 +	{
    2.61 +		private UnitType requirementOnType;
    2.62 +
    2.63 +		public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base()
    2.64 +		{
    2.65 +			requirementOnType = requirementOn;
    2.66 +			FailureStringPrefix = "Army contains " + requirementOn.Name + " and so must contain: ";
    2.67 +		}
    2.68 +
    2.69 +		protected override bool IsApplicable(WarFoundryObject toObjectAdded)
    2.70 +		{
    2.71 +			return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded);
    2.72 +		}
    2.73 +
    2.74 +		private bool IsRequirementOnType(WarFoundryObject toObjectAdded)
    2.75 +		{
    2.76 +			return requirementOnType.Equals(toObjectAdded) || (toObjectAdded is Unit && requirementOnType.Equals(((Unit)toObjectAdded).UnitType));
    2.77 +		}
    2.78 +
    2.79 +		protected override bool IsApplicable(Army toArmy)
    2.80 +		{
    2.81 +			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
    2.82 +		}
    2.83 +
    2.84 +		public override Validation ValidatesArmy(Army toArmy)
    2.85 +		{
    2.86 +			return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;
    2.87 +		}
    2.88 +	}
    2.89 +}
    2.90 +