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 diff
     1.1 --- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Wed Nov 30 20:44:03 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Wed Nov 30 21:06:41 2011 +0000
     1.3 @@ -52,11 +52,6 @@
     1.4  
     1.5  		protected string FailureStringPrefix { get; set; }
     1.6  
     1.7 -		protected override string GetValidationFailedMessage(Army army)
     1.8 -		{
     1.9 -			return "";
    1.10 -		}
    1.11 -
    1.12  		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
    1.13  		{
    1.14  			StringBuilder sb = new StringBuilder();
    1.15 @@ -90,7 +85,7 @@
    1.16  			return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount);
    1.17  		}
    1.18  
    1.19 -		private bool IsValidByRequirement (UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount)
    1.20 +		private bool IsValidByRequirement(UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount)
    1.21  		{
    1.22  			double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
    1.23  			double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
    1.24 @@ -135,7 +130,55 @@
    1.25  
    1.26  		public override Validation ValidatesArmy(Army army)
    1.27  		{
    1.28 -			return Validation.NotApplicable;
    1.29 +			Validation canAdd = Validation.NotApplicable;
    1.30 +			int allowedTypeCount = army.GetUnitTypeCount(allowedType);
    1.31 +			bool typeFound = (allowedTypeCount > 0);
    1.32 +
    1.33 +			foreach (UnitCountRequirementData limit in requiredTypes)
    1.34 +			{
    1.35 +				int limitedTypeCount = army.GetUnitTypeCount(limit.UnitType);
    1.36 +				typeFound |= (limitedTypeCount > 0);
    1.37 +
    1.38 +				if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
    1.39 +				{
    1.40 +					canAdd = Validation.Failed;
    1.41 +					break;
    1.42 +				}
    1.43 +			}
    1.44 +
    1.45 +			if (typeFound && canAdd == Validation.NotApplicable)
    1.46 +			{
    1.47 +				canAdd = Validation.Passed;
    1.48 +			}
    1.49 +
    1.50 +			return canAdd;
    1.51 +		}
    1.52 +
    1.53 +		protected override string GetValidationFailedMessage(Army army)
    1.54 +		{
    1.55 +			StringBuilder sb = new StringBuilder();
    1.56 +			sb.Append(FailureStringPrefix);
    1.57 +			sb.Append(String.Join("; ", GetFailedValidationRequirements(army).ToArray()));
    1.58 +			sb.Append(".");
    1.59 +			return sb.ToString();
    1.60 +		}
    1.61 +
    1.62 +		private List<string> GetFailedValidationRequirements(Army army)
    1.63 +		{
    1.64 +			List<string> failures = new List<string>();
    1.65 +			int allowedTypeCount = army.GetUnitTypeCount(allowedType);
    1.66 +
    1.67 +			foreach (UnitCountRequirementData limit in requiredTypes)
    1.68 +			{
    1.69 +				int limitedTypeCount =  army.GetUnitTypeCount(limit.UnitType);
    1.70 +
    1.71 +				if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
    1.72 +				{
    1.73 +					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));
    1.74 +				}
    1.75 +			}
    1.76 +
    1.77 +			return failures;
    1.78  		}
    1.79  
    1.80  		public override string RequirementID