Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view API/Objects/Requirement/AbstractRequirement.cs @ 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 | 8f5125740316 |
children |
line wrap: on
line source
// 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 abstract string RequirementID { get; } public override bool Equals(object obj) { if (obj == null) { return false; } else if (obj.GetType().Equals(this.GetType())) { return TypeEquals(obj); } else { return false; } } public override abstract int GetHashCode(); /// <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 = ""; Validation result = ValidatesArmy(army); if (!Validates.AsOkay(result)) { message = GetValidationFailedMessage(army); } return message; } 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) { string message = ""; Validation result = AllowsAdding(toAdd, toArmy); if (!Validates.AsOkay(result)) { message = GetAllowsAddingFailedMessage(toAdd, toArmy); } return message; } 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); public abstract Validation ValidatesArmy(Army army); } }