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