annotate API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs @ 442:5ac76de8ce62

Re #350: Add requirement to allow N of unit for specific other units * Add code for parsing min numbers as well as allowed numbers
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 20:10:13 +0000
parents d2331ee59d74
children 4fbb7f205f7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (UnitRequiresNUnitsForMUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
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.
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using IBBoard.WarFoundry.API.Objects;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.WarFoundry.API.Objects.Requirement;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.WarFoundry.API.Factories.Requirement
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 public class UnitRequiresNUnitsForMUnitsRequirementFactory : IRequirementFactory
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 public UnitRequiresNUnitsForMUnitsRequirementFactory()
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 //Do nothing special
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 public string AppliesToID
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 get
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 return RequiresNUnitsForMUnitsRequirement.REQUIREMENT_ID;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(type);
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 Race race = type.Race;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 AddRequirements(req, race, data, raceFactory);
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 return req;
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(RequiresNUnitsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 foreach (string requirement in data.Split('|'))
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 string[] requirementParts = requirement.Split(':');
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 string unitID = requirementParts[0];
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 UnitType unitType = raceFactory.GetUnitType(unitID, race);
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 if (unitType == null)
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 if (requirementParts.Length == 2)
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 {
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
48 string[] amounts = requirementParts[1].Split(';');
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
50 int minCount;
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
51 int allowedAmounts;
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
52
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
53 if (amounts.Length == 1)
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 {
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
55 minCount = 1;
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
56 allowedAmounts = ParseAllowedAmount(amounts[0], unitID);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
58 else
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 {
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
60 minCount = ParseMinCount(amounts[0], unitID);
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
61 allowedAmounts = ParseAllowedAmount(amounts[1], unitID);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
63
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
64 req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 else
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 req.AddUnitTypeRequirement(unitType);
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
72
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
73 private int ParseMinCount(string minCount, string unitID)
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
74 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
75 try
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
76 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
77 return Int32.Parse(minCount);
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
78 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
79 catch (FormatException)
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
80 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
81 throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
82 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
83 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
84
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
85 private int ParseAllowedAmount(string allowedAmount, string unitID)
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
86 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
87 try
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
88 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
89 return Int32.Parse(allowedAmount);
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
90 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
91 catch (FormatException)
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
92 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
93 throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
94 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
95 }
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98