# HG changeset patch # User IBBoard # Date 1322685843 0 # Node ID 410f3d85c9c5bc415e031ad9b6bb31f8978b8c75 # Parent 0922851f6125558595522cd3be4552942a36c713 Re #350: Add requirement to allow N of unit for specific other units * Add code to get failure message for "allowed to add" * Refactor out common for checking if numbers exceed limits diff -r 0922851f6125 -r 410f3d85c9c5 API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Tue Nov 29 20:55:21 2011 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Wed Nov 30 20:44:03 2011 +0000 @@ -3,6 +3,7 @@ // 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; +using System.Text; namespace IBBoard.WarFoundry.API.Objects.Requirement { @@ -58,7 +59,42 @@ protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy) { - return ""; + StringBuilder sb = new StringBuilder(); + sb.Append(FailureStringPrefix); + sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray())); + sb.Append("."); + return sb.ToString(); + } + + private List GetFailedAddingRequirements(UnitType unitType, Army toArmy) + { + List failures = new List(); + int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, unitType); + + foreach (UnitCountRequirementData limit in requiredTypes) + { + int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType); + + if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount)) + { + failures.Add(String.Format("{0} {1} for every {2} {3} (have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount)); + } + } + + return failures; + } + + private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount) + { + int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject); + return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount); + } + + private bool IsValidByRequirement (UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount) + { + double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0); + double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0); + return allowedTypeMultiplier <= limitedTypeMultiplier; } public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) @@ -71,11 +107,8 @@ foreach (UnitCountRequirementData limit in requiredTypes) { typeFound |= (addedUnitType == limit.UnitType); - int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject); - double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0); - double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0); - if (allowedTypeMultiplier > limitedTypeMultiplier) + if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount)) { canAdd = Validation.Failed; break;