Mercurial > repos > snowblizz-super-API-ideas
changeset 371:bfdb95906075
Re #351: Add extensible requirement handling method
* First refactor of extensible requirement creation
* Needs to return different types of factory
* Needs to handle unit types that are currently not loaded
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 13 Jun 2011 19:44:39 +0000 |
parents | 077e9be48438 |
children | 2f8b2467ba99 |
files | API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs API/Factories/Xml/WarFoundryXmlRaceFactory.cs IBBoard.WarFoundry.API.csproj |
diffstat | 3 files changed, 37 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Mon Jun 13 19:44:39 2011 +0000 @@ -0,0 +1,24 @@ +// This file (UnitRequiresAtLeastNUnitsRequirementFactory.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; +using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Objects.Requirement; + +namespace IBBoard.WarFoundry.API.Factories.Requirement +{ + public class UnitRequiresAtLeastNUnitsRequirementFactory + { + public UnitRequiresAtLeastNUnitsRequirementFactory() + { + //Do nothing special + } + + public UnitRequiresAtLeastNUnitsRequirement CreateRequirement (UnitType type, string data) + { + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type); + return req; + } + } +} +
--- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Mon Jun 13 15:15:04 2011 +0000 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Mon Jun 13 19:44:39 2011 +0000 @@ -12,7 +12,8 @@ using IBBoard.CustomMath; using ICSharpCode.SharpZipLib.Zip; using IBBoard.WarFoundry.API.Objects; -using IBBoard.WarFoundry.API.Objects.Requirement; +using IBBoard.WarFoundry.API.Objects.Requirement; +using IBBoard.WarFoundry.API.Factories.Requirement; namespace IBBoard.WarFoundry.API.Factories.Xml { @@ -373,19 +374,24 @@ } private void LoadRequirementsForUnitType(XmlElement elem, UnitType type) - { - //TODO: Handle requirements more extensibly + { foreach (XmlElement extraData in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:requirements/race:requirement")) { string name = extraData.GetAttribute("requirementName"); - if (name == "RequiresAtLeastNUnits") { + UnitRequiresAtLeastNUnitsRequirementFactory reqFactory = GetRequirementFactory(name); + + if (reqFactory != null) { string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText; - UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type); - req.AddUnitTypeRequirement(GetUnitTypeFromDocument(elem.OwnerDocument, data, type.Race)); + UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data); type.AddRequirement(req); } } } + + UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name) + { + return new UnitRequiresAtLeastNUnitsRequirementFactory(); + } private void LoadExtraDataForUnitType(XmlElement elem, UnitType type) {
--- a/IBBoard.WarFoundry.API.csproj Mon Jun 13 15:15:04 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Mon Jun 13 19:44:39 2011 +0000 @@ -179,6 +179,7 @@ <Compile Include="API\Objects\Requirement\IRequirement.cs" /> <Compile Include="API\Objects\Requirement\RequirementHandler.cs" /> <Compile Include="API\Objects\Requirement\AbstractRequirement.cs" /> + <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactory.cs" /> </ItemGroup> <ItemGroup> <Reference Include="System.Xml" />