comparison API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs @ 447:4fbb7f205f7e

Re #350: Add requirement to allow N of unit for specific other units * Extend factory to support additive unit types * Add three parameter method to requirement to set min, allowed and additive unit types Note: Naughty code (untested) added to requirement to make factory compile! Needs testing.
author IBBoard <dev@ibboard.co.uk>
date Tue, 20 Dec 2011 21:03:33 +0000
parents 5ac76de8ce62
children afc6410e4efc
comparison
equal deleted inserted replaced
446:ca1e8f7c8b73 447:4fbb7f205f7e
2 // 2 //
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. 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.
4 using System; 4 using System;
5 using IBBoard.WarFoundry.API.Objects; 5 using IBBoard.WarFoundry.API.Objects;
6 using IBBoard.WarFoundry.API.Objects.Requirement; 6 using IBBoard.WarFoundry.API.Objects.Requirement;
7 using System.Collections.Generic;
7 8
8 namespace IBBoard.WarFoundry.API.Factories.Requirement 9 namespace IBBoard.WarFoundry.API.Factories.Requirement
9 { 10 {
10 public class UnitRequiresNUnitsForMUnitsRequirementFactory : IRequirementFactory 11 public class UnitRequiresNUnitsForMUnitsRequirementFactory : IRequirementFactory
11 { 12 {
33 private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(RequiresNUnitsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) 34 private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(RequiresNUnitsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
34 { 35 {
35 foreach (string requirement in data.Split('|')) 36 foreach (string requirement in data.Split('|'))
36 { 37 {
37 string[] requirementParts = requirement.Split(':'); 38 string[] requirementParts = requirement.Split(':');
38 string unitID = requirementParts[0]; 39 string unitIDs = requirementParts[0];
39 UnitType unitType = raceFactory.GetUnitType(unitID, race); 40 UnitType[] unitTypes = GetUnitTypes(unitIDs, race, raceFactory);
40
41 if (unitType == null)
42 {
43 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
44 }
45 41
46 if (requirementParts.Length == 2) 42 if (requirementParts.Length == 2)
47 { 43 {
48 string[] amounts = requirementParts[1].Split(';'); 44 string[] amounts = requirementParts[1].Split(';');
49 45
51 int allowedAmounts; 47 int allowedAmounts;
52 48
53 if (amounts.Length == 1) 49 if (amounts.Length == 1)
54 { 50 {
55 minCount = 1; 51 minCount = 1;
56 allowedAmounts = ParseAllowedAmount(amounts[0], unitID); 52 allowedAmounts = ParseAllowedAmount(amounts[0], unitIDs);
57 } 53 }
58 else 54 else
59 { 55 {
60 minCount = ParseMinCount(amounts[0], unitID); 56 minCount = ParseMinCount(amounts[0], unitIDs);
61 allowedAmounts = ParseAllowedAmount(amounts[1], unitID); 57 allowedAmounts = ParseAllowedAmount(amounts[1], unitIDs);
62 } 58 }
63 59
64 req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts); 60 req.AddUnitTypeRequirement(minCount, allowedAmounts, unitTypes);
65 } 61 }
66 else 62 else
67 { 63 {
68 req.AddUnitTypeRequirement(unitType); 64 req.AddUnitTypeRequirement(unitTypes);
69 } 65 }
70 } 66 }
67 }
68
69 private UnitType[] GetUnitTypes<SOURCE_FILE_TYPE, ENTRY_TYPE>(string data, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
70 {
71 List<UnitType> unitTypes = new List<UnitType>();
72
73 foreach (string unitID in data.Split(';'))
74 {
75 UnitType unitType = raceFactory.GetUnitType(unitID, race);
76
77 if (unitType == null)
78 {
79 throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID));
80 }
81
82 unitTypes.Add(unitType);
83 }
84
85 return unitTypes.ToArray();
71 } 86 }
72 87
73 private int ParseMinCount(string minCount, string unitID) 88 private int ParseMinCount(string minCount, string unitID)
74 { 89 {
75 try 90 try
76 { 91 {
77 return Int32.Parse(minCount); 92 return Int32.Parse(minCount);
78 } 93 }
79 catch (FormatException) 94 catch (FormatException)
80 { 95 {
81 throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", minCount, unitID)); 96 throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", minCount, unitID));
82 } 97 }
83 } 98 }
84 99
85 private int ParseAllowedAmount(string allowedAmount, string unitID) 100 private int ParseAllowedAmount(string allowedAmount, string unitID)
86 { 101 {
88 { 103 {
89 return Int32.Parse(allowedAmount); 104 return Int32.Parse(allowedAmount);
90 } 105 }
91 catch (FormatException) 106 catch (FormatException)
92 { 107 {
93 throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID)); 108 throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID));
94 } 109 }
95 } 110 }
96 } 111 }
97 } 112 }
98 113