Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequirementHandler.cs @ 448:dbd779cdc0f9
Re #350: Add requirement to allow N of unit for specific other units
* Fix limit checking when "limited count" is not 1 (e.g. "need 2 for every 3")
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 23 Dec 2011 15:36:06 +0000 |
parents | 506af4cbe3f5 |
children | 676f5ce04176 |
rev | line source |
---|---|
353 | 1 // This file (RequirementHandler.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard |
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. | |
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 | 6 |
7 namespace IBBoard.WarFoundry.API.Objects.Requirement | |
8 { | |
9 /// <summary> | |
10 /// The requirement handler that tests the validity of armies. | |
11 /// </summary> | |
12 public class RequirementHandler | |
13 { | |
14 private RequirementHandler() | |
15 { | |
16 } | |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
17 |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
18 |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
19 public static Validation ValidateArmy(Army army) |
361
8781e53c59bb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
355
diff
changeset
|
20 { |
8781e53c59bb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
355
diff
changeset
|
21 ICollection<string> ignored; |
8781e53c59bb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
355
diff
changeset
|
22 return ValidateArmy(army, out ignored); |
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 |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
25 public static Validation ValidateArmy(Army army, out ICollection<string> failureMessages) |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
26 { |
361
8781e53c59bb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
355
diff
changeset
|
27 Validation result = Validation.Passed; |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
28 failureMessages = new List<string>(); |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
29 |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
30 foreach (IRequirement requirement in army.GetRequirements()) |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
31 { |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
32 if (!Validates.AsOkay(requirement.ValidatesArmy(army))) |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
33 { |
361
8781e53c59bb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
355
diff
changeset
|
34 result = Validation.Failed; |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
35 failureMessages.Add(requirement.GetValidationMessage(army)); |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
36 } |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
37 } |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
38 |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
39 return result; |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
40 } |
355 | 41 |
42 public static Validation AllowsAdding(UnitType unitType, Army army) | |
43 { | |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
44 ICollection<string> ignored; |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
45 return AllowsAdding(unitType, army, out ignored); |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
46 } |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
47 |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
48 public static Validation AllowsAdding(UnitType unitType, Army army, out ICollection<string> failureMessages) |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
49 { |
355 | 50 Validation result = Validation.Passed; |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
51 failureMessages = new List<string>(); |
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
52 |
355 | 53 foreach (IRequirement requirement in army.GetRequirements()) |
54 { | |
55 if (!Validates.AsOkay(requirement.AllowsAdding(unitType, army))) | |
56 { | |
57 result = Validation.Failed; | |
363
506af4cbe3f5
Fixes #33: Add centralised requirement checker
IBBoard <dev@ibboard.co.uk>
parents:
361
diff
changeset
|
58 failureMessages.Add(requirement.GetAllowsAddingMessage(unitType, army)); |
355 | 59 } |
60 } | |
61 | |
62 return result; | |
63 } | |
353 | 64 } |
65 } | |
66 |