Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 358:dbe7ccb1e557
Re #345: Add failure message to requirements
* Add abstract requirement class to handle some of the message commonality
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 01 May 2011 13:56:20 +0000 |
parents | 50d0d3b39a0b |
children | 2a9c046be55a |
rev | line source |
---|---|
357 | 1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
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. |
357 | 4 using System; |
5 using System.Collections.Generic; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using IBBoard.WarFoundry.API.Objects; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 namespace IBBoard.WarFoundry.API.Objects.Requirement |
357 | 9 { |
10 /// <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. | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 /// </summary> |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
13 public class RequiresAtLeastNUnitsRequirement : AbstractRequirement |
357 | 14 { |
15 private List<UnitCountRequirementData> requiredTypes; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes) |
357 | 18 { |
19 requiredTypes = new List<UnitCountRequirementData>(); | |
20 | |
21 foreach (UnitType unitType in requiredUnitTypes) | |
22 { | |
23 AddUnitTypeRequirement(unitType); | |
24 } | |
25 } | |
26 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
27 protected override bool TypeEquals (object obj) |
357 | 28 { |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
29 RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
30 if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes)) |
357 | 31 { |
32 return false; | |
33 } | |
34 else | |
35 { | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
36 return true; |
357 | 37 } |
38 } | |
39 | |
40 /// <summary> | |
41 /// Checks whether the supplied WarFoundryObject can be added to the supplied army. | |
42 /// </summary> | |
43 /// <returns> | |
44 /// A <code>Validation</code> enum to show the result of the validation | |
45 /// </returns> | |
46 /// <param name='wfObject'> | |
47 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement | |
48 /// </param> | |
49 /// <param name='toArmy'> | |
50 /// The army to add the object to. | |
51 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
52 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) |
357 | 53 { |
54 return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; | |
55 } | |
56 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
57 protected override bool IsApplicable(Army toArmy) |
357 | 58 { |
59 return false; | |
60 } | |
61 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
62 protected override bool IsApplicable(WarFoundryObject toObject) |
357 | 63 { |
64 bool isApplicable = false; | |
65 UnitType unitType = GetUnitTypeFromObject(toObject); | |
66 | |
67 if (unitType != null) | |
68 { | |
69 isApplicable = IsApplicable(unitType); | |
70 } | |
71 | |
72 return isApplicable; | |
73 } | |
74 | |
75 protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) | |
76 { | |
77 UnitType unitType = null; | |
78 | |
79 if (toObject is UnitType) | |
80 { | |
81 unitType = (UnitType)toObject; | |
82 } | |
83 else if (toObject is Unit) | |
84 { | |
85 unitType = ((Unit)toObject).UnitType; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 } |
357 | 87 |
88 return unitType; | |
89 } | |
90 | |
91 private bool IsApplicable (UnitType unitType) | |
92 { | |
93 bool isApplicable = false; | |
94 foreach (UnitCountRequirementData requirement in requiredTypes) | |
95 { | |
96 if (requirement.UnitType.Equals(unitType)) | |
97 { | |
98 isApplicable = true; | |
99 break; | |
100 } | |
101 } | |
102 return isApplicable; | |
103 } | |
104 | |
105 private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) | |
106 { | |
107 Validation isValid = Validation.Passed; | |
108 | |
109 foreach (UnitCountRequirementData requirement in requiredTypes) | |
110 { | |
111 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) | |
112 { | |
113 isValid = Validation.Failed; | |
114 break; | |
115 } | |
116 } | |
117 | |
118 return isValid; | |
119 } | |
120 | |
121 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) | |
122 { | |
123 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); | |
124 } | |
125 | |
126 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) | |
127 { | |
128 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
129 } |
357 | 130 |
131 /// <summary> | |
132 /// Adds a requirement for there to be at least minCount of a given UnitType | |
133 /// </summary> | |
134 /// <param name='unitType'> | |
135 /// The unit type to require. | |
136 /// </param> | |
137 /// <param name='minCount'> | |
138 /// The minimum number of that type that must exist. | |
139 /// </param> | |
140 public void AddUnitTypeRequirement(UnitType unitType, int minCount) | |
141 { | |
142 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); | |
143 } | |
144 | |
145 /// <summary> | |
146 /// Adds a requirement for there to be one or more of a given UnitType | |
147 /// </summary> | |
148 /// <param name='unitType'> | |
149 /// The unit type to require. | |
150 /// </param> | |
151 public void AddUnitTypeRequirement (UnitType unitType) | |
152 { | |
153 AddUnitTypeRequirement(unitType, 1); | |
154 } | |
155 | |
156 /// <summary> | |
157 /// Checks whether the supplied army is currently valid according to this requirement. | |
158 /// </summary> | |
159 /// <returns> | |
160 /// A <code>Validation</code> enum to show the result of the validation | |
161 /// </returns> | |
162 /// <param name='toValidate'> | |
163 /// The army to validate | |
164 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
165 public override Validation ValidatesArmy(Army toValidate) |
357 | 166 { |
167 Validation isValid = Validation.Passed; | |
168 | |
169 foreach (UnitCountRequirementData requirement in requiredTypes) | |
170 { | |
171 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) | |
172 { | |
173 isValid = Validation.Failed; | |
174 break; | |
175 } | |
176 } | |
177 | |
178 return isValid; | |
179 } | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
180 |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
181 protected override string GetValidationFailedMessage (Army army) |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
182 { |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
183 string message = ""; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
184 return message; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
185 } |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
186 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
187 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
188 |