Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view API/Objects/Requirement/AbstractUnitRequirement.cs @ 457:8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
* Rename public accessor for unit types to ConstraintTypes so that "no more than" requirements don't work with "required types"
* Remove class level version of ConstraintTypes to fall back to abstract class version
* Make sure we cascade allowedObject up through RequiresNoMoreThanNOfUnitTypeRequirement
* Rebuild UnitRequiresNoMoreThanNOfUnitTypeRequirement "IsApplicable" so that we don't get applicable for unrelated types, even if the requirement is currently in a failure state
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 26 Feb 2012 15:14:01 +0000 |
parents | 52baffdd2ab9 |
children | 680db2462e34 |
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; using System.Collections.Generic; namespace IBBoard.WarFoundry.API.Objects.Requirement { public abstract class AbstractUnitRequirement<OBJECT_TYPE> : IRequirement where OBJECT_TYPE : IWarFoundryObject { private List<UnitCountRequirementData> requiredTypes; private OBJECT_TYPE allowedObject; public AbstractUnitRequirement(OBJECT_TYPE allowedObject, params UnitType[] requiredUnitTypes) { this.allowedObject = allowedObject; requiredTypes = new List<UnitCountRequirementData>(); foreach (UnitType unitType in requiredUnitTypes) { AddUnitTypeRequirement(unitType); } } public abstract string RequirementID { get; } protected List<UnitCountRequirementData> ConstraintTypes { get { return requiredTypes; } } public abstract void AddUnitTypeRequirement(UnitType unitType); 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(IWarFoundryObject toObjectAdded, Army toArmy) { return IsApplicable(toArmy) || IsApplicable(toObjectAdded); } protected virtual bool IsApplicable(Army toArmy) { return false; } protected virtual bool IsApplicable(IWarFoundryObject toObject) { return false; } 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(IWarFoundryObject 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(IWarFoundryObject toAdd, Army toArmy) { return String.Format(AllowsAddingFailedMessage, GetFailedAddingRequirementsString(toAdd, toArmy)); } protected abstract string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy); public abstract Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy); public abstract Validation ValidatesArmy(Army army); protected UnitType GetUnitTypeFromObject(IWarFoundryObject toObject) { UnitType unitType = null; if (toObject is UnitType) { unitType = (UnitType)toObject; } else if (toObject is Unit) { unitType = ((Unit)toObject).UnitType; } return unitType; } protected int GetUnitTypeCount(Army toArmy, UnitType unitType, IWarFoundryObject wfObject) { return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); } protected int GetCountFromObject(IWarFoundryObject wfObject, UnitType limitedType) { return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; } protected int GetObjectCount(Army toArmy, IWarFoundryObject wfObject) { return GetObjectCountFromArmy(toArmy) + GetObjectCountFromObject(wfObject); } protected abstract int GetObjectCountFromArmy(Army toArmy); protected virtual int GetObjectCountFromObject(IWarFoundryObject wfObject) { return allowedObject.Equals(wfObject) ? 1 : 0; } public OBJECT_TYPE AllowedObject { get { return allowedObject; } } } }