annotate API/Objects/Requirement/RequirementHandler.cs @ 361:8781e53c59bb

Re #345: Add failure message to requirements * Return message for each failure (don't stop at first one)
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 14:45:56 +0000
parents 47712a323cf2
children 506af4cbe3f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (RequirementHandler.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
361
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
5 using System.Collections.Generic;
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 namespace IBBoard.WarFoundry.API.Objects.Requirement
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 {
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 /// <summary>
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 /// The requirement handler that tests the validity of armies.
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 /// </summary>
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 public class RequirementHandler
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 {
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 private RequirementHandler()
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 public static Validation ValidateArmy (Army army)
361
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
19 {
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
20 ICollection<string> ignored;
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
21 return ValidateArmy(army, out ignored);
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
22 }
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
23
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
24 public static Validation ValidateArmy (Army army, out ICollection<string> failureMessages)
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
361
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
26 Validation result = Validation.Passed;
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
27 failureMessages = new List<string>();
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 foreach (IRequirement requirement in army.GetRequirements())
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 {
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 if (!Validates.AsOkay(requirement.ValidatesArmy(army)))
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 {
361
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
33 result = Validation.Failed;
8781e53c59bb Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents: 355
diff changeset
34 failureMessages.Add(requirement.GetValidationMessage(army));
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 return result;
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
355
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
40
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
41 public static Validation AllowsAdding(UnitType unitType, Army army)
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
42 {
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
43 Validation result = Validation.Passed;
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
44
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
45 foreach (IRequirement requirement in army.GetRequirements())
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
46 {
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
47 if (!Validates.AsOkay(requirement.AllowsAdding(unitType, army)))
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
48 {
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
49 result = Validation.Failed;
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
50 break;
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
51 }
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
52 }
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
53
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
54 return result;
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
55 }
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58