Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs @ 375:d9bf78a8f517
Re #351: Add extensible requirement handling method
* Wrap exception from integer parsing
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 02 Jul 2011 15:10:17 +0000 |
parents | 13793f3a2a2e |
children | e50682387d63 |
rev | line source |
---|---|
371
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (UnitRequiresAtLeastNUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
bfdb95906075
Re #351: Add extensible requirement handling method
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. |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 using System; |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using IBBoard.WarFoundry.API.Objects; |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using IBBoard.WarFoundry.API.Objects.Requirement; |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 namespace IBBoard.WarFoundry.API.Factories.Requirement |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 { |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 public class UnitRequiresAtLeastNUnitsRequirementFactory |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 { |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 public UnitRequiresAtLeastNUnitsRequirementFactory() |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 { |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 //Do nothing special |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 } |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
17 public UnitRequiresAtLeastNUnitsRequirement CreateRequirement(UnitType type, string data) |
371
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 { |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type); |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
20 Race race = type.Race; |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
21 AddRequirements(req, race, data); |
371
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 return req; |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 } |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
24 |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
25 private void AddRequirements(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data) |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
26 { |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
27 foreach (string requirement in data.Split('|')) |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
28 { |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
29 string[] requirementParts = requirement.Split(':'); |
375
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
30 string unitID = requirementParts[0]; |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
31 UnitType unitType = race.GetUnitType(unitID); |
374
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
32 |
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
33 if (unitType == null) |
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
34 { |
375
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
35 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", unitID)); |
374
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
36 } |
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
37 |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
38 if (requirementParts.Length == 2) |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
39 { |
375
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
40 string amount = requirementParts[1]; |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
41 |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
42 try |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
43 { |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
44 req.AddUnitTypeRequirement(unitType, Int32.Parse(amount)); |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
45 } |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
46 catch (FormatException ex) |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
47 { |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
48 throw new InvalidRequirementException(String.Format("Invalid amount '{0}' for unit type '{1}' for 'Requires at least N units' requirement", amount, unitID)); |
d9bf78a8f517
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
374
diff
changeset
|
49 } |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
50 } |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
51 else |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
52 { |
374
13793f3a2a2e
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
373
diff
changeset
|
53 req.AddUnitTypeRequirement(unitType); |
373
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
54 } |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
55 } |
e10630f39ec2
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
372
diff
changeset
|
56 } |
371
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 } |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 } |
bfdb95906075
Re #351: Add extensible requirement handling method
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 |