Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view API/Objects/Requirement/AbstractUnitRequirement.cs @ 455:afc6410e4efc
Re #379: Fix validation of requirements to check for unit
* Move to "Unit" requirements, since we assume things depend on units
* Rename some classes to more meaningful names from unit-based change
* Rename "requires N for M" requirement as we can make it more flexible
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 22 Feb 2012 20:45:39 +0000 |
parents | |
children | 52baffdd2ab9 |
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> RequiredTypes { 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 true; } protected virtual bool IsApplicable(IWarFoundryObject 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(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; } } } }