# HG changeset patch # User IBBoard # Date 1337802987 -3600 # Node ID f48c49b537385c4877f21e1ac1ebaa072302df64 # Parent e4e2a85604b6f7a50f151ae8924fc051d395e0e0 Re #410: "N units per M models in parent" requirement * Add a "context" object that will hold the parent unit (or possibly other stuff) * Update all method signatures and calls diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/AbstractUnitRequirement.cs --- a/API/Objects/Requirement/AbstractUnitRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -73,17 +73,17 @@ return hash; } - protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy) + protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy, AddingContext context) { - return IsApplicable(toArmy) || IsApplicable(toObjectAdded); + return IsApplicable(toArmy, context) || IsApplicable(toObjectAdded, context); } - protected virtual bool IsApplicable(Army toArmy) + protected virtual bool IsApplicable(Army toArmy, AddingContext context) { return false; } - protected virtual bool IsApplicable(IWarFoundryObject toObject) + protected virtual bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { return false; } @@ -132,7 +132,42 @@ protected abstract string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy); - public abstract Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy); + /// + /// 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. + /// + public Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) + { + return AllowsAdding(wfObject, toArmy, null); + } + + /// + /// 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. + /// + /// The context for the action (may be null) + public Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy, AddingContext context) + { + return IsApplicable(wfObject, toArmy, context) ? CheckAllowsAdding(wfObject, toArmy, context) : Validation.NotApplicable; + } + + protected abstract Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy, AddingContext context); public abstract Validation ValidatesArmy(Army army); diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/AddingContext.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Objects/Requirement/AddingContext.cs Wed May 23 20:56:27 2012 +0100 @@ -0,0 +1,17 @@ +// 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 +{ + /// + /// Marker interface (for now) of useful context information for the AllowsAdding method call. + /// + public interface AddingContext + { + + } +} + diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -20,7 +20,7 @@ return toArmy.Race == AllowedObject ? 1 : 0; } - protected override bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy, AddingContext context) { bool isApplicable = false; SimpleSet checkedTypes = new SimpleSet(); diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirement.cs --- a/API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -20,7 +20,7 @@ return toArmy.Race == AllowedObject ? 1 : 0; } - protected override bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy, AddingContext context) { bool isApplicable = false; SimpleSet checkedTypes = new SimpleSet(); diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -30,29 +30,12 @@ } } - /// - /// 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. - /// - public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) - { - return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; - } - - protected override bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy, AddingContext context) { return false; } - protected override bool IsApplicable(IWarFoundryObject toObject) + protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { bool isApplicable = false; UnitType unitType = GetUnitTypeFromObject(toObject); @@ -79,7 +62,7 @@ return isApplicable; } - private Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy) + protected override Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy, AddingContext context) { Validation isValid = Validation.Passed; diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -64,7 +64,7 @@ return normalisedLimited >= 1 && normalisedAllowed <= normalisedLimited; } - public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) + protected override Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy, AddingContext context) { Validation canAdd = Validation.NotApplicable; UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType; diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -28,29 +28,12 @@ } } - /// - /// 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. - /// - public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) - { - return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; - } - - protected override bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy, AddingContext context) { return false; } - protected override bool IsApplicable(IWarFoundryObject toObject) + protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { bool isApplicable = false; UnitType unitType = GetUnitTypeFromObject(toObject); @@ -77,7 +60,7 @@ return isApplicable; } - private Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy) + protected override Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy, AddingContext context) { Validation canAdd = Validation.Passed; diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -16,17 +16,12 @@ { } - public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) + protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy, AddingContext context) { - return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + return IsApplicable(toObject, context) || IsApplicableForRequiredType(toObject, toArmy); } - protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) - { - return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); - } - - protected override bool IsApplicable(IWarFoundryObject toObject) + protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); } diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs --- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -20,29 +20,12 @@ //Do nothing } - /// - /// 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. - /// - public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) + protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy, AddingContext context) { - return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + return IsApplicable(toObject, context) || IsApplicableForRequiredType(toObject, toArmy); } - protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) - { - return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); - } - - protected override bool IsApplicable(IWarFoundryObject toObject) + protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); } diff -r e4e2a85604b6 -r f48c49b53738 API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Wed May 23 20:37:23 2012 +0100 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Wed May 23 20:56:27 2012 +0100 @@ -17,12 +17,12 @@ //Do nothing special } - protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) + protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy, AddingContext context) { - return base.IsApplicable(toObject, toArmy) || IsApplicableForRequiredType(toObject, toArmy); + return base.IsApplicable(toObject, toArmy, context) || IsApplicableForRequiredType(toObject, toArmy); } - protected override bool IsApplicable(IWarFoundryObject toObject) + protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) { return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); } diff -r e4e2a85604b6 -r f48c49b53738 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Wed May 23 20:37:23 2012 +0100 +++ b/IBBoard.WarFoundry.API.csproj Wed May 23 20:56:27 2012 +0100 @@ -196,6 +196,7 @@ +