Mercurial > repos > IBDev-IBBoard.WarFoundry.API
changeset 374:13793f3a2a2e
Re #351: Add extensible requirement handling method
* Add initial conditional to fetching of factory
* Exception if content isn't correct
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 02 Jul 2011 14:59:45 +0000 |
parents | e10630f39ec2 |
children | d9bf78a8f517 |
files | API/Factories/Requirement/InvalidRequirementException.cs API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs API/Factories/Xml/WarFoundryXmlRaceFactory.cs IBBoard.WarFoundry.API.csproj |
diffstat | 4 files changed, 35 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/Requirement/InvalidRequirementException.cs Sat Jul 02 14:59:45 2011 +0000 @@ -0,0 +1,15 @@ +// This file (InvalidRequirementException.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 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; + +namespace IBBoard.WarFoundry.API.Factories.Requirement +{ + public class InvalidRequirementException : Exception + { + public InvalidRequirementException(string message) : base(message) + { + } + } +} +
--- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Tue Jun 14 14:27:14 2011 +0000 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Sat Jul 02 14:59:45 2011 +0000 @@ -27,13 +27,20 @@ foreach (string requirement in data.Split('|')) { string[] requirementParts = requirement.Split(':'); + UnitType unitType = race.GetUnitType(requirementParts[0]); + + if (unitType == null) + { + throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", requirementParts[0])); + } + if (requirementParts.Length == 2) { - req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0]), Int32.Parse(requirementParts[1])); + req.AddUnitTypeRequirement(unitType, Int32.Parse(requirementParts[1])); } else { - req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0])); + req.AddUnitTypeRequirement(unitType); } } }
--- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Tue Jun 14 14:27:14 2011 +0000 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Sat Jul 02 14:59:45 2011 +0000 @@ -388,9 +388,17 @@ } } - UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name) + private UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name) { - return new UnitRequiresAtLeastNUnitsRequirementFactory(); + + UnitRequiresAtLeastNUnitsRequirementFactory factory = null; + + if ("RequiresAtLeastNUnits".Equals(name)) + { + factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + } + + return factory; } private void LoadExtraDataForUnitType(XmlElement elem, UnitType type)
--- a/IBBoard.WarFoundry.API.csproj Tue Jun 14 14:27:14 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Sat Jul 02 14:59:45 2011 +0000 @@ -180,6 +180,7 @@ <Compile Include="API\Objects\Requirement\RequirementHandler.cs" /> <Compile Include="API\Objects\Requirement\AbstractRequirement.cs" /> <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactory.cs" /> + <Compile Include="API\Factories\Requirement\InvalidRequirementException.cs" /> </ItemGroup> <ItemGroup> <Reference Include="System.Xml" />