Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 360:777725613edb
Re #345: Add failure message to requirements
* Add protected property to hold prefix for validation string, as that is all that changes
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 01 May 2011 14:33:27 +0000 |
parents | 2a9c046be55a |
children | 0dd8dbe8afe9 |
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 | |
42 /// <summary> | |
43 /// Checks whether the supplied WarFoundryObject can be added to the supplied army. | |
44 /// </summary> | |
45 /// <returns> | |
46 /// A <code>Validation</code> enum to show the result of the validation | |
47 /// </returns> | |
48 /// <param name='wfObject'> | |
49 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement | |
50 /// </param> | |
51 /// <param name='toArmy'> | |
52 /// The army to add the object to. | |
53 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
54 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) |
357 | 55 { |
56 return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; | |
57 } | |
58 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
59 protected override bool IsApplicable(Army toArmy) |
357 | 60 { |
61 return false; | |
62 } | |
63 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
64 protected override bool IsApplicable(WarFoundryObject toObject) |
357 | 65 { |
66 bool isApplicable = false; | |
67 UnitType unitType = GetUnitTypeFromObject(toObject); | |
68 | |
69 if (unitType != null) | |
70 { | |
71 isApplicable = IsApplicable(unitType); | |
72 } | |
73 | |
74 return isApplicable; | |
75 } | |
76 | |
77 protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) | |
78 { | |
79 UnitType unitType = null; | |
80 | |
81 if (toObject is UnitType) | |
82 { | |
83 unitType = (UnitType)toObject; | |
84 } | |
85 else if (toObject is Unit) | |
86 { | |
87 unitType = ((Unit)toObject).UnitType; | |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 } |
357 | 89 |
90 return unitType; | |
91 } | |
92 | |
93 private bool IsApplicable (UnitType unitType) | |
94 { | |
95 bool isApplicable = false; | |
96 foreach (UnitCountRequirementData requirement in requiredTypes) | |
97 { | |
98 if (requirement.UnitType.Equals(unitType)) | |
99 { | |
100 isApplicable = true; | |
101 break; | |
102 } | |
103 } | |
104 return isApplicable; | |
105 } | |
106 | |
107 private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) | |
108 { | |
109 Validation isValid = Validation.Passed; | |
110 | |
111 foreach (UnitCountRequirementData requirement in requiredTypes) | |
112 { | |
113 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) | |
114 { | |
115 isValid = Validation.Failed; | |
116 break; | |
117 } | |
118 } | |
119 | |
120 return isValid; | |
121 } | |
122 | |
123 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) | |
124 { | |
125 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); | |
126 } | |
127 | |
128 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) | |
129 { | |
130 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
|
131 } |
357 | 132 |
133 /// <summary> | |
134 /// Adds a requirement for there to be at least minCount of a given UnitType | |
135 /// </summary> | |
136 /// <param name='unitType'> | |
137 /// The unit type to require. | |
138 /// </param> | |
139 /// <param name='minCount'> | |
140 /// The minimum number of that type that must exist. | |
141 /// </param> | |
142 public void AddUnitTypeRequirement(UnitType unitType, int minCount) | |
143 { | |
144 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); | |
145 } | |
146 | |
147 /// <summary> | |
148 /// Adds a requirement for there to be one or more of a given UnitType | |
149 /// </summary> | |
150 /// <param name='unitType'> | |
151 /// The unit type to require. | |
152 /// </param> | |
153 public void AddUnitTypeRequirement (UnitType unitType) | |
154 { | |
155 AddUnitTypeRequirement(unitType, 1); | |
156 } | |
157 | |
158 /// <summary> | |
159 /// Checks whether the supplied army is currently valid according to this requirement. | |
160 /// </summary> | |
161 /// <returns> | |
162 /// A <code>Validation</code> enum to show the result of the validation | |
163 /// </returns> | |
164 /// <param name='toValidate'> | |
165 /// The army to validate | |
166 /// </param> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
167 public override Validation ValidatesArmy(Army toValidate) |
357 | 168 { |
169 Validation isValid = Validation.Passed; | |
170 | |
171 foreach (UnitCountRequirementData requirement in requiredTypes) | |
172 { | |
173 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) | |
174 { | |
175 isValid = Validation.Failed; | |
176 break; | |
177 } | |
178 } | |
179 | |
180 return isValid; | |
181 } | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
182 |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
183 protected string FailureStringPrefix { get; set; } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
184 |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
185 protected override string GetValidationFailedMessage (Army army) |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
186 { |
359
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
187 StringBuilder sb = new StringBuilder(); |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
359
diff
changeset
|
188 sb.Append(FailureStringPrefix); |
359
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
189 sb.Append(String.Join("; ", GetFailedRequirements(army).ToArray())); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
190 sb.Append("."); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
191 return sb.ToString(); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
192 } |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
193 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
194 private List<string> GetFailedRequirements(Army army) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
195 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
196 List<string> failures = new List<string>(); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
197 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
198 foreach (UnitCountRequirementData requirement in requiredTypes) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
199 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
200 int unitCount = army.GetUnitTypeCount(requirement.UnitType); |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
201 |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
202 if (unitCount < requirement.Count) |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
203 { |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
204 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
|
205 } |
2a9c046be55a
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
358
diff
changeset
|
206 } |
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 return failures; |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
209 } |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
210 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
211 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
212 |