Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 419:71fceea2725b
Code tidy-up - remove warnings
* Add missing GetHashcode() implementations
* Remove unused exception variables
* Use unused event
* Remove dead code
* Properly override some methods
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 25 Sep 2011 20:52:27 +0100 |
parents | 0dd8dbe8afe9 |
children | 3882b533d99d |
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; |
359
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
7 using System.Text; |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 namespace IBBoard.WarFoundry.API.Objects.Requirement |
357 | 10 { |
11 /// <summary> | |
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. | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 /// </summary> |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
14 public class RequiresAtLeastNUnitsRequirement : AbstractRequirement |
357 | 15 { |
16 private List<UnitCountRequirementData> requiredTypes; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes) |
357 | 19 { |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
20 FailureStringPrefix = "Army must contain: "; |
357 | 21 requiredTypes = new List<UnitCountRequirementData>(); |
22 | |
23 foreach (UnitType unitType in requiredUnitTypes) | |
24 { | |
25 AddUnitTypeRequirement(unitType); | |
26 } | |
27 } | |
28 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
29 protected override bool TypeEquals (object obj) |
357 | 30 { |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
31 RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
32 if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes)) |
357 | 33 { |
34 return false; | |
35 } | |
36 else | |
37 { | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
38 return true; |
357 | 39 } |
40 } | |
41 | |
419 | 42 public override int GetHashCode() |
43 { | |
44 int hash = 0; | |
45 | |
46 foreach (UnitCountRequirementData req in requiredTypes) | |
47 { | |
48 hash += req.UnitType.GetHashCode(); | |
49 } | |
50 | |
51 return hash; | |
52 } | |
53 | |
357 | 54 /// <summary> |
55 /// Checks whether the supplied WarFoundryObject can be added to the supplied army. | |
56 /// </summary> | |
57 /// <returns> | |
58 /// A <code>Validation</code> enum to show the result of the validation | |
59 /// </returns> | |
60 /// <param name='wfObject'> | |
61 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement | |
62 /// </param> | |
63 /// <param name='toArmy'> | |
64 /// The army to add the object to. | |
65 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
66 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) |
357 | 67 { |
68 return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; | |
69 } | |
70 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
71 protected override bool IsApplicable(Army toArmy) |
357 | 72 { |
73 return false; | |
74 } | |
75 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
76 protected override bool IsApplicable(WarFoundryObject toObject) |
357 | 77 { |
78 bool isApplicable = false; | |
79 UnitType unitType = GetUnitTypeFromObject(toObject); | |
80 | |
81 if (unitType != null) | |
82 { | |
83 isApplicable = IsApplicable(unitType); | |
84 } | |
85 | |
86 return isApplicable; | |
87 } | |
88 | |
89 protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) | |
90 { | |
91 UnitType unitType = null; | |
92 | |
93 if (toObject is UnitType) | |
94 { | |
95 unitType = (UnitType)toObject; | |
96 } | |
97 else if (toObject is Unit) | |
98 { | |
99 unitType = ((Unit)toObject).UnitType; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 } |
357 | 101 |
102 return unitType; | |
103 } | |
104 | |
105 private bool IsApplicable (UnitType unitType) | |
106 { | |
107 bool isApplicable = false; | |
108 foreach (UnitCountRequirementData requirement in requiredTypes) | |
109 { | |
110 if (requirement.UnitType.Equals(unitType)) | |
111 { | |
112 isApplicable = true; | |
113 break; | |
114 } | |
115 } | |
116 return isApplicable; | |
117 } | |
118 | |
119 private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) | |
120 { | |
121 Validation isValid = Validation.Passed; | |
122 | |
123 foreach (UnitCountRequirementData requirement in requiredTypes) | |
124 { | |
125 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) | |
126 { | |
127 isValid = Validation.Failed; | |
128 break; | |
129 } | |
130 } | |
131 | |
132 return isValid; | |
133 } | |
134 | |
135 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) | |
136 { | |
137 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); | |
138 } | |
139 | |
140 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) | |
141 { | |
142 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
|
143 } |
357 | 144 |
145 /// <summary> | |
146 /// Adds a requirement for there to be at least minCount of a given UnitType | |
147 /// </summary> | |
148 /// <param name='unitType'> | |
149 /// The unit type to require. | |
150 /// </param> | |
151 /// <param name='minCount'> | |
152 /// The minimum number of that type that must exist. | |
153 /// </param> | |
154 public void AddUnitTypeRequirement(UnitType unitType, int minCount) | |
155 { | |
156 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); | |
157 } | |
158 | |
159 /// <summary> | |
160 /// Adds a requirement for there to be one or more of a given UnitType | |
161 /// </summary> | |
162 /// <param name='unitType'> | |
163 /// The unit type to require. | |
164 /// </param> | |
165 public void AddUnitTypeRequirement (UnitType unitType) | |
166 { | |
167 AddUnitTypeRequirement(unitType, 1); | |
168 } | |
169 | |
170 /// <summary> | |
171 /// Checks whether the supplied army is currently valid according to this requirement. | |
172 /// </summary> | |
173 /// <returns> | |
174 /// A <code>Validation</code> enum to show the result of the validation | |
175 /// </returns> | |
176 /// <param name='toValidate'> | |
177 /// The army to validate | |
178 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
179 public override Validation ValidatesArmy(Army toValidate) |
357 | 180 { |
181 Validation isValid = Validation.Passed; | |
182 | |
183 foreach (UnitCountRequirementData requirement in requiredTypes) | |
184 { | |
185 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) | |
186 { | |
187 isValid = Validation.Failed; | |
188 break; | |
189 } | |
190 } | |
191 | |
192 return isValid; | |
193 } | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
194 |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
195 protected string FailureStringPrefix { get; set; } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
196 |
364
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
197 protected override string GetValidationFailedMessage(Army army) |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
198 { |
359
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
199 StringBuilder sb = new StringBuilder(); |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
200 sb.Append(FailureStringPrefix); |
359
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
201 sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray())); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
202 sb.Append("."); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
203 return sb.ToString(); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
204 } |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
205 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
206 private List<string> GetFailedRequirements(Army army) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
207 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
208 List<string> failures = new List<string>(); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
209 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
210 foreach (UnitCountRequirementData requirement in requiredTypes) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
211 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
212 int unitCount = army.GetUnitTypeCount(requirement.UnitType); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
213 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
214 if (unitCount < requirement.Count) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
215 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
216 failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")"); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
217 } |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
218 } |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
219 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
220 return failures; |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
221 } |
364
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
222 |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
223 protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy) |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
224 { |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
225 StringBuilder sb = new StringBuilder(); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
226 sb.Append(FailureStringPrefix); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
227 sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray())); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
228 sb.Append("."); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
229 return sb.ToString(); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
230 } |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
231 |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
232 private List<string> GetFailedAddingRequirements(UnitType unitType, Army army) |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
233 { |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
234 List<string> failures = new List<string>(); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
235 |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
236 foreach (UnitCountRequirementData requirement in requiredTypes) |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
237 { |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
238 int unitCount = GetUnitTypeCount(army, requirement.UnitType, unitType); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
239 |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
240 if (unitCount < requirement.Count) |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
241 { |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
242 failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")"); |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
243 } |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
244 } |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
245 |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
246 return failures; |
0dd8dbe8afe9
Fixes #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
360
diff
changeset
|
247 } |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
248 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
249 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
250 |