Mercurial > repos > IBDev-IBBoard.WarFoundry.API
changeset 439:5252dfb9cdfb
Re #350: Add requirement to allow N of unit for specific other units
* Add code for validating army (based on code for validating adding)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 30 Nov 2011 21:06:41 +0000 |
parents | 410f3d85c9c5 |
children | baa34d91031f |
files | API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs |
diffstat | 1 files changed, 50 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Wed Nov 30 20:44:03 2011 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Wed Nov 30 21:06:41 2011 +0000 @@ -52,11 +52,6 @@ protected string FailureStringPrefix { get; set; } - protected override string GetValidationFailedMessage(Army army) - { - return ""; - } - protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy) { StringBuilder sb = new StringBuilder(); @@ -90,7 +85,7 @@ return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount); } - private bool IsValidByRequirement (UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount) + private bool IsValidByRequirement(UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount) { double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0); double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0); @@ -135,7 +130,55 @@ public override Validation ValidatesArmy(Army army) { - return Validation.NotApplicable; + Validation canAdd = Validation.NotApplicable; + int allowedTypeCount = army.GetUnitTypeCount(allowedType); + bool typeFound = (allowedTypeCount > 0); + + foreach (UnitCountRequirementData limit in requiredTypes) + { + int limitedTypeCount = army.GetUnitTypeCount(limit.UnitType); + typeFound |= (limitedTypeCount > 0); + + if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) + { + canAdd = Validation.Failed; + break; + } + } + + if (typeFound && canAdd == Validation.NotApplicable) + { + canAdd = Validation.Passed; + } + + return canAdd; + } + + protected override string GetValidationFailedMessage(Army army) + { + StringBuilder sb = new StringBuilder(); + sb.Append(FailureStringPrefix); + sb.Append(String.Join("; ", GetFailedValidationRequirements(army).ToArray())); + sb.Append("."); + return sb.ToString(); + } + + private List<string> GetFailedValidationRequirements(Army army) + { + List<string> failures = new List<string>(); + int allowedTypeCount = army.GetUnitTypeCount(allowedType); + + foreach (UnitCountRequirementData limit in requiredTypes) + { + int limitedTypeCount = army.GetUnitTypeCount(limit.UnitType); + + if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) + { + 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; } public override string RequirementID