Mercurial > repos > IBBoard.WarFoundry.API
view API/Factories/Requirement/UnitRequiresNParentModelsForMUnitsRequirementFactory.cs @ 490:e35fc85eda2b
Re #410: Create "N units per M models in parent unit" requirement
* Add factory to create requirements
* Add methods to allow things other than 1:1 values for N and M
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 29 Jul 2012 21:02:59 +0100 |
parents | |
children |
line wrap: on
line source
// This file (UnitRequiresNParentModelsForMUnitsRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 IBBoard // // 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. using System; using IBBoard.WarFoundry.API.Objects.Requirement; using IBBoard.WarFoundry.API.Objects; namespace IBBoard.WarFoundry.API.Factories.Requirement { public class UnitRequiresNParentModelsForMUnitsRequirementFactory : IRequirementFactory { public UnitRequiresNParentModelsForMUnitsRequirementFactory() { } public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(IBBoard.WarFoundry.API.Objects.UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) { UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(type); Race race = type.Race; AddRequirements(req, race, data, raceFactory); return req; } private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresNParentModelsForMUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) { foreach (string requirement in data.Split('|')) { string[] requirementParts = requirement.Split(':'); string unitID = requirementParts[0]; UnitType unitType = GetUnitTypeFromID(unitID, race, raceFactory); if (requirementParts.Length == 2) { string[] amounts = requirementParts[1].Split(';'); if (amounts.Length == 1) { req.AddUnitTypeRequirement(unitType, ParseAllowedAmount(amounts[0], unitID)); } else { req.AddUnitTypeRequirement(unitType, ParseAllowedAmount(amounts[0], unitID), ParseModelCount(amounts[1], unitID)); } } else { req.AddUnitTypeRequirement(unitType); } } } private UnitType GetUnitTypeFromID<SOURCE_FILE_TYPE, ENTRY_TYPE>(string unitID, Race race, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) { UnitType unitType = raceFactory.GetUnitType(unitID, race); if (unitType == null) { throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N models in parent unit for M units' requirement", unitID)); } return unitType; } private int ParseModelCount(string minCount, string unitID) { try { return Int32.Parse(minCount); } catch (FormatException) { throw new InvalidRequirementException(String.Format("Invalid model amount '{0}' for unit types '{1}' for 'Requires N models in parent unit for M units' requirement", minCount, unitID)); } } private int ParseAllowedAmount(string allowedAmount, string unitID) { try { return Int32.Parse(allowedAmount); } catch (FormatException) { throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N models in parent unit for M units' requirement", allowedAmount, unitID)); } } public string AppliesToID { get { return UnitRequiresNParentModelsForMUnitsRequirement.REQUIREMENT_ID; } } } }