# HG changeset patch # User IBBoard # Date 1304258180 0 # Node ID dbe7ccb1e5578fb7dafacfc3731d14d76eef8d5c # Parent 50d0d3b39a0b0067afe895baf612d096fb18736e Re #345: Add failure message to requirements * Add abstract requirement class to handle some of the message commonality diff -r 50d0d3b39a0b -r dbe7ccb1e557 API/Objects/Requirement/AbstractRequirement.cs --- /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; + } + } + + /// + /// Type-specific equality checking - must be implemented by each class + /// + /// + /// true if this object is equal to obj, else false + /// + /// + /// The object to compare to + /// + 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); + } +} + diff -r 50d0d3b39a0b -r dbe7ccb1e557 API/Objects/Requirement/IRequirement.cs --- 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 -{ - /// - /// Base interface for a Requirement that constrains the units/equipment that can be taken in an army +{ + /// + /// Base interface for a Requirement that constrains the units/equipment that can be taken in an army /// public interface IRequirement - { - /// - /// Checks whether the supplied WarFoundryObject can be added to the supplied army. - /// - /// - /// A Validation enum to show the result of the validation - /// - /// - /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement - /// - /// - /// The army to add the object to. - /// - Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy); - - /// - /// Checks whether the supplied army is currently valid according to this requirement. - /// - /// - /// A Validation enum to show the result of the validation - /// - /// - /// The army to validate - /// + { + /// + /// Checks whether the supplied WarFoundryObject can be added to the supplied army. + /// + /// + /// A Validation enum to show the result of the validation + /// + /// + /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement + /// + /// + /// The army to add the object to. + /// + Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy); + + /// + /// Checks whether the supplied army is currently valid according to this requirement. + /// + /// + /// A Validation enum to show the result of the validation + /// + /// + /// The army to validate + /// Validation ValidatesArmy(Army army); + + /// + /// Gets the validation message from validating the army + /// + /// + /// A validation message, if the validation fails, else an empty string. + /// + /// + /// The army to validate. + /// + string GetValidationMessage(Army army); } } diff -r 50d0d3b39a0b -r dbe7ccb1e557 API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- 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 @@ /// /// 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. /// - public class RequiresAtLeastNUnitsRequirement : IRequirement + public class RequiresAtLeastNUnitsRequirement : AbstractRequirement { private List 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 @@ /// /// The army to add the object to. /// - 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 @@ /// /// The army to validate /// - 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; + } } } diff -r 50d0d3b39a0b -r dbe7ccb1e557 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- 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 @@ /// /// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army. /// - public class RequiresNoMoreThanNOfUnitTypeRequirement : IRequirement + public class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractRequirement { private List limitedTypes; @@ -36,7 +36,7 @@ /// /// The army to add the object to. /// - public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) { Validation canAdd = Validation.Passed; @@ -96,7 +96,7 @@ /// /// The army to validate /// - 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; + } } } diff -r 50d0d3b39a0b -r dbe7ccb1e557 IBBoard.WarFoundry.API.csproj --- 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 @@ +