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 wrap: on
line diff
--- 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<string> GetFailedAddingRequirements(UnitType unitType, Army toArmy)
+		{
+			List<string> failures = new List<string>();
+			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;