# HG changeset patch # User IBBoard # Date 1330977174 0 # Node ID f0594621e4a0c7b450eca443e78950b03882a60e # Parent 025319b6fa7a0abce65dc1e1d4377f75635b3ae4 Re #379: Fix validation of requirements to check for unit * Move "Is applicable" checks up to abstract "no more than N of unit type" class diff -r 025319b6fa7a -r f0594621e4a0 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Mar 04 21:13:52 2012 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Mon Mar 05 19:52:54 2012 +0000 @@ -42,6 +42,43 @@ /// public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) { + return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + protected override bool IsApplicable(Army toArmy) + { + return false; + } + + protected override bool IsApplicable(IWarFoundryObject toObject) + { + bool isApplicable = false; + UnitType unitType = GetUnitTypeFromObject(toObject); + + if (unitType != null) + { + isApplicable = IsApplicable(unitType); + } + + return isApplicable; + } + + private bool IsApplicable(UnitType unitType) + { + bool isApplicable = false; + foreach (UnitCountRequirementData requirement in ConstraintTypes) + { + if (requirement.UnitType.Equals(unitType)) + { + isApplicable = true; + break; + } + } + return isApplicable; + } + + private Validation CheckAllowsAdding(IWarFoundryObject wfObject, Army toArmy) + { Validation canAdd = Validation.Passed; foreach (UnitCountRequirementData limit in ConstraintTypes) diff -r 025319b6fa7a -r f0594621e4a0 API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Mar 04 21:13:52 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Mon Mar 05 19:52:54 2012 +0000 @@ -17,26 +17,9 @@ //Do nothing special } - /// - /// 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) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; - } - protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) { - return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); + return base.IsApplicable(toObject, toArmy) || IsApplicableForRequiredType(toObject, toArmy); } protected override bool IsApplicable(IWarFoundryObject toObject)