changeset 438:410f3d85c9c5

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
author IBBoard <dev@ibboard.co.uk>
date Wed, 30 Nov 2011 20:44:03 +0000
parents 0922851f6125
children 5252dfb9cdfb
files API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs
diffstat 1 files changed, 38 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Tue Nov 29 20:55:21 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Wed Nov 30 20:44:03 2011 +0000
     1.3 @@ -3,6 +3,7 @@
     1.4  // 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.
     1.5  using System;
     1.6  using System.Collections.Generic;
     1.7 +using System.Text;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.10  {
    1.11 @@ -58,7 +59,42 @@
    1.12  
    1.13  		protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
    1.14  		{
    1.15 -			return "";
    1.16 +			StringBuilder sb = new StringBuilder();
    1.17 +			sb.Append(FailureStringPrefix);
    1.18 +			sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
    1.19 +			sb.Append(".");
    1.20 +			return sb.ToString();
    1.21 +		}
    1.22 +
    1.23 +		private List<string> GetFailedAddingRequirements(UnitType unitType, Army toArmy)
    1.24 +		{
    1.25 +			List<string> failures = new List<string>();
    1.26 +			int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, unitType);
    1.27 +
    1.28 +			foreach (UnitCountRequirementData limit in requiredTypes)
    1.29 +			{
    1.30 +				int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType);
    1.31 +
    1.32 +				if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount))
    1.33 +				{
    1.34 +					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.35 +				}
    1.36 +			}
    1.37 +
    1.38 +			return failures;
    1.39 +		}
    1.40 +
    1.41 +		private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount)
    1.42 +		{
    1.43 +			int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
    1.44 +			return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount);
    1.45 +		}
    1.46 +
    1.47 +		private bool IsValidByRequirement (UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount)
    1.48 +		{
    1.49 +			double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
    1.50 +			double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
    1.51 +			return allowedTypeMultiplier <= limitedTypeMultiplier;
    1.52  		}
    1.53  
    1.54  		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    1.55 @@ -71,11 +107,8 @@
    1.56  			foreach (UnitCountRequirementData limit in requiredTypes)
    1.57  			{
    1.58  				typeFound |= (addedUnitType == limit.UnitType);
    1.59 -				int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
    1.60 -				double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
    1.61 -				double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
    1.62  
    1.63 -				if (allowedTypeMultiplier > limitedTypeMultiplier)
    1.64 +				if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount))
    1.65  				{
    1.66  					canAdd = Validation.Failed;
    1.67  					break;