comparison API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 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
comparison
equal deleted inserted replaced
358:dbe7ccb1e557 359:2a9c046be55a
2 // 2 //
3 // 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. 3 // 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.
4 using System; 4 using System;
5 using System.Collections.Generic; 5 using System.Collections.Generic;
6 using IBBoard.WarFoundry.API.Objects; 6 using IBBoard.WarFoundry.API.Objects;
7 using System.Text;
7 8
8 namespace IBBoard.WarFoundry.API.Objects.Requirement 9 namespace IBBoard.WarFoundry.API.Objects.Requirement
9 { 10 {
10 /// <summary> 11 /// <summary>
11 /// A requirement where a WarFoundryObject requires at least N units of one or more unit types before any number of that object can be taken in an army. 12 /// A requirement where a WarFoundryObject requires at least N units of one or more unit types before any number of that object can be taken in an army.
178 return isValid; 179 return isValid;
179 } 180 }
180 181
181 protected override string GetValidationFailedMessage (Army army) 182 protected override string GetValidationFailedMessage (Army army)
182 { 183 {
183 string message = ""; 184 StringBuilder sb = new StringBuilder();
184 return message; 185 sb.Append("Army must contain: ");
186 sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray()));
187 sb.Append(".");
188 return sb.ToString();
189 }
190
191 private List<string> GetFailedRequirements(Army army)
192 {
193 List<string> failures = new List<string>();
194
195 foreach (UnitCountRequirementData requirement in requiredTypes)
196 {
197 int unitCount = army.GetUnitTypeCount(requirement.UnitType);
198
199 if (unitCount < requirement.Count)
200 {
201 failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")");
202 }
203 }
204
205 return failures;
185 } 206 }
186 } 207 }
187 } 208 }
188 209