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
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 }
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
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
41
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
42 public static Validation AllowsAdding(UnitType unitType, Army army)
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
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
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
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
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
53 foreach (IRequirement requirement in army.GetRequirements())
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
54 {
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
55 if (!Validates.AsOkay(requirement.AllowsAdding(unitType, army)))
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
56 {
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
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
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
59 }
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
60 }
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
61
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
62 return result;
47712a323cf2 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 353
diff changeset
63 }
353
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 }
65fccdd51aec Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66