annotate API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs @ 455:afc6410e4efc

Re #379: Fix validation of requirements to check for unit * Move to "Unit" requirements, since we assume things depend on units * Rename some classes to more meaningful names from unit-based change * Rename "requires N for M" requirement as we can make it more flexible
author IBBoard <dev@ibboard.co.uk>
date Wed, 22 Feb 2012 20:45:39 +0000
parents 4fbb7f205f7e
children
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;
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
7 using System.Collections.Generic;
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 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
10 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 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
12 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 public UnitRequiresNUnitsForMUnitsRequirementFactory()
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 //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
16 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 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
19 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 get
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 447
diff changeset
22 return UnitRequiresNUnitsForMUnitsRequirement.REQUIREMENT_ID;
441
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
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 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
27 {
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 447
diff changeset
28 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(type);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 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
30 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
31 return req;
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
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 447
diff changeset
34 private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresNUnitsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 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
37 {
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 string[] requirementParts = requirement.Split(':');
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
39 string unitIDs = requirementParts[0];
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
40 UnitType[] unitTypes = GetUnitTypes(unitIDs, race, raceFactory);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 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
43 {
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
44 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
45
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
46 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
47 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
48
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
49 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
50 {
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
51 minCount = 1;
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
52 allowedAmounts = ParseAllowedAmount(amounts[0], unitIDs);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
54 else
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 {
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
56 minCount = ParseMinCount(amounts[0], unitIDs);
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
57 allowedAmounts = ParseAllowedAmount(amounts[1], unitIDs);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
59
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
60 req.AddUnitTypeRequirement(minCount, allowedAmounts, unitTypes);
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 else
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 {
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
64 req.AddUnitTypeRequirement(unitTypes);
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 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 }
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
68
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
69 private UnitType[] GetUnitTypes<SOURCE_FILE_TYPE, ENTRY_TYPE>(string data, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
70 {
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
71 List<UnitType> unitTypes = new List<UnitType>();
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
72
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
73 foreach (string unitID in data.Split(';'))
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
74 {
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
75 UnitType unitType = raceFactory.GetUnitType(unitID, race);
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
76
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
77 if (unitType == null)
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
78 {
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
79 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
80 }
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
81
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
82 unitTypes.Add(unitType);
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
83 }
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
84
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
85 return unitTypes.ToArray();
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
86 }
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
87
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
88 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
89 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
90 try
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
91 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
92 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
93 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
94 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
95 {
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
96 throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
97 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
98 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
99
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
100 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
101 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
102 try
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
103 {
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
104 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
105 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
106 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
107 {
447
4fbb7f205f7e Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 442
diff changeset
108 throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
442
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
109 }
5ac76de8ce62 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 441
diff changeset
110 }
441
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
112 }
d2331ee59d74 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113