changeset 359:2a9c046be55a

Re #345: Add failure message to requirements * Implement message creation for "at least" requirement
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 14:23:51 +0000
parents dbe7ccb1e557
children 777725613edb
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs
diffstat 1 files changed, 23 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 13:56:20 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun May 01 14:23:51 2011 +0000
     1.3 @@ -4,6 +4,7 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using IBBoard.WarFoundry.API.Objects;
     1.7 +using System.Text;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.10  {
    1.11 @@ -180,8 +181,28 @@
    1.12  
    1.13  		protected override string GetValidationFailedMessage (Army army)
    1.14  		{
    1.15 -			string message = "";
    1.16 -			return message;
    1.17 +			StringBuilder sb = new StringBuilder();
    1.18 +			sb.Append("Army must contain: ");
    1.19 +			sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray()));
    1.20 +			sb.Append(".");
    1.21 +			return sb.ToString();
    1.22 +		}
    1.23 +
    1.24 +		private List<string> GetFailedRequirements(Army army)
    1.25 +		{
    1.26 +			List<string> failures = new List<string>();
    1.27 +
    1.28 +			foreach (UnitCountRequirementData requirement in requiredTypes)
    1.29 +			{
    1.30 +				int unitCount = army.GetUnitTypeCount(requirement.UnitType);
    1.31 +
    1.32 +				if (unitCount < requirement.Count)
    1.33 +				{
    1.34 +					failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")");
    1.35 +				}
    1.36 +			}
    1.37 +
    1.38 +			return failures;
    1.39  		}
    1.40  	}
    1.41  }