Mercurial > repos > snowblizz-super-API-ideas
changeset 12:ac232763858b
Re #9 - Make WarFoundry API use smaller methods
* Obselete old Category and UnitType constructors and add minimal constructors
* Add setters to properties where needed
* Add value sanity checking to Category and UnitType
* Set default values for percentages, choices and points for Category
* Make XML factory use new constructor and setter properties
* Add DuplicateItemException to project file
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 18 Jan 2009 20:52:29 +0000 |
parents | 5a1df00b0359 |
children | ad8eaed12e66 |
files | IBBoard.WarFoundry.API.mdp api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/Category.cs api/Objects/UnitType.cs |
diffstat | 4 files changed, 216 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.mdp Sun Jan 18 16:24:03 2009 +0000 +++ b/IBBoard.WarFoundry.API.mdp Sun Jan 18 20:52:29 2009 +0000 @@ -87,6 +87,7 @@ <File name="dtds/system.dtd" subtype="Code" buildaction="Nothing" /> <File name="api/Objects/WarFoundryStagedLoadingObject.cs" subtype="Code" buildaction="Compile" /> <File name="api/Objects/IWarFoundryStagedLoadObject.cs" subtype="Code" buildaction="Compile" /> + <File name="api/Objects/DuplicateItemException.cs" subtype="Code" buildaction="Compile" /> </Contents> <References> <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> @@ -96,4 +97,5 @@ <ProjectReference type="Gac" localcopy="True" refto="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <ProjectReference type="Assembly" localcopy="True" refto="libs/ICSharpCode.SharpZipLib.dll" /> </References> + <GtkDesignInfo /> </Project> \ No newline at end of file
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 18 16:24:03 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 18 20:52:29 2009 +0000 @@ -385,49 +385,28 @@ { string id = elem.GetAttribute("id"); string name = elem.GetAttribute("typeName"); - string mainCatID = elem.GetAttribute("cat"); - int minNum, maxNum, minSize, maxSize, baseSize;//TODO: Add base size - float points, unitPoints; - Stats stats; - List<UnitRequirement> unitRequirements = new List<UnitRequirement>(); - bool found = false; - List<string> catIDs = new List<string>(); - string catID; - minNum = GetIntValueFromAttribute(elem, "minNum"); - maxNum = GetIntValueFromAttribute(elem, "maxNum"); - minSize = GetIntValueFromAttribute(elem, "minSize"); - maxSize = GetIntValueFromAttribute(elem, "maxSize"); - - if (minSize > maxSize && maxSize!=-1) - { - minSize = maxSize; - } - - points = GetIntValueFromAttribute(elem, "points"); - unitPoints = GetIntValueFromAttribute(elem, "unitPoints"); + UnitType type = new UnitType(id, name, parentRace); + type.MinNumber = GetIntValueFromAttribute(elem, "minNum"); + type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum"); + type.MinSize = GetIntValueFromAttribute(elem, "minSize"); + type.MaxSize = GetIntValueFromAttribute(elem, "maxSize"); + //TODO: Add base size + type.CostPerTrooper = GetIntValueFromAttribute(elem, "points"); + type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints"); XmlNode node = elem.FirstChild; foreach(XmlElement cat in node.ChildNodes) { - catID = cat.GetAttribute("catID"); - catIDs.Add(catID); - - if (catID == mainCatID) - { - found = true; - } + string catID = cat.GetAttribute("catID"); + type.AddCategory(parentRace.GetCategory(catID)); } - - if (!found) - { - throw new InvalidFileException("The main cat "+mainCatID+" was not found in the list of categories for unit "+id); - } - + + string mainCatID = elem.GetAttribute("cat"); + type.MainCategory = parentRace.GetCategory(mainCatID); node = node.NextSibling; - stats = ParseUnitStats((XmlElement)node, system); + type.UnitStats = ParseUnitStats((XmlElement)node, system); //TODO: Add unit requirements - UnitType type = new UnitType(id, name, mainCatID, catIDs.ToArray(), minNum, maxNum, minSize, maxSize, unitPoints, points, stats, unitRequirements.ToArray(), parentRace); return type; }
--- a/api/Objects/Category.cs Sun Jan 18 16:24:03 2009 +0000 +++ b/api/Objects/Category.cs Sun Jan 18 20:52:29 2009 +0000 @@ -1,5 +1,6 @@ using System; using System.Xml; +using IBBoard.Logging; namespace IBBoard.WarFoundry.API.Objects { @@ -8,18 +9,31 @@ /// </summary> public class Category : WarFoundryObject { - private int minPts, maxPts, minPc, maxPc, minChoice, maxChoice, baseVal, incVal, incAmount; - /*private GameSystem system;*/ - + private int minPts = 0; + private int maxPts = -1; + private int minPc = 0; + private int maxPc = 100; + private int minChoice = 0; + private int maxChoice = -1; + private int baseVal = 0; + private int incVal = 0; + private int incAmount = 0; + + + public Category(string id, string name) : base(id, name) + { + } + + [Obsolete("Use the two argument constructor and the appropriate 'set' methods")] public Category(string id, string name, int minPoints, int maxPoints, int minPercent, int maxPercent, int minChoices, int maxChoices, int baseValue, int incrementValue, int incrementAmount) : base(id, name) { - minPts = minPoints; - maxPts = maxPoints; - minPc = minPercent; - maxPc = maxPercent; - baseVal = baseValue; - incVal = incrementValue; - incAmount = incrementAmount; + MinimumPoints = minPoints; + MaximumPoints = maxPoints; + MinimumPercentage = minPercent; + MaximumPercentage = maxPercent; + BaseValue = baseValue; + IncrementValue = incrementValue; + IncrementAmount = incrementAmount; } protected override string DefaultName() @@ -30,55 +44,118 @@ public int MinimumPoints { get { return minPts; } - set { minPts = value; } + set + { + minPts = (value >= 0 ? value : 0); + CheckMinimumPoints(); + } } public int MaximumPoints { get { return maxPts; } - set { maxPts = value; } + set + { + maxPts = (value >= -1 ? value : -1); + CheckMinimumPoints(); + } + } + + private void CheckMinimumPoints() + { + if (MinimumPoints > MaximumPoints && MaximumPoints!=-1) + { + MinimumPoints = MaximumPoints; + LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum points limit greater than its maximum points limit.", Name, ID); + } } public int MinimumPercentage { get { return minPc; } - set { minPc = value; } + set + { + minPc = (value >= 0 ? value : 0); + CheckMinimumPercentage(); + } } public int MaximumPercentage { get { return maxPc; } - set { maxPc = value; } + set + { + if (value < 0) + { + maxPc = 0; + } + else if (value > 100) + { + maxPc = 100; + } + else + { + maxPc = value; + } + + CheckMinimumPercentage(); + } + } + + private void CheckMinimumPercentage() + { + if (MinimumPercentage > MaximumPercentage) + { + MinimumPercentage = MaximumPercentage; + LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum percentage limit greater than its maximum percentage limit.", Name, ID); + } } public int MinimumChoices { get { return minChoice; } - set { minChoice = value; } + set + { + minChoice = (value >= 0 ? value : 0); + CheckMinimumChoices(); + } } public int MaximumChoices { get { return maxChoice; } - set { maxChoice = value; } + set + { + maxChoice = (value >= -1 ? value : -1); + CheckMinimumChoices(); + } + } + + private void CheckMinimumChoices() + { + if (MinimumPercentage > MaximumPercentage && MaximumPercentage!=-1) + { + MinimumPercentage = MaximumPercentage; + LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum number of choices greater than its maximum number of choices.", Name, ID); + } } public int BaseValue { get { return baseVal; } - set { baseVal = value; } + set { baseVal = (value >= 0 ? value : 0); } } public int IncrementValue { get { return incVal; } - set { incVal = value; } + set { incVal = (value >= 0 ? value : 0); } } public int IncrementAmount { get { return incAmount; } - set { incAmount = value; } + set { incAmount = (value >= 0 ? value : 0); } } } }
--- a/api/Objects/UnitType.cs Sun Jan 18 16:24:03 2009 +0000 +++ b/api/Objects/UnitType.cs Sun Jan 18 20:52:29 2009 +0000 @@ -12,54 +12,48 @@ /// </summary> public class UnitType : WarFoundryObject { - protected Category mainCat; - protected string mainCatID; - protected Category[] categories; - protected string[] categoryIDs; + protected Category mainCat; + protected List<Category> categories = new List<Category>(); protected Race race; protected int min, max, baseSize = 0; protected int minSize, maxSize; protected double baseUnitCost; protected double costPerTrooper; protected Stats stats; - protected UnitRequirement[] requirements; + protected List<UnitRequirement> requirements = new List<UnitRequirement>(); protected Hashtable equipment = new Hashtable(); protected Hashtable equipmentExclusionGroups = new Hashtable(); protected ArrayList equipmentKeyOrder = new ArrayList(); - + + + public UnitType(string id, string typeName, Race parentRace) : base(id, typeName) + { + race = parentRace; + } + + [Obsolete("Use three parameter constructor and setters")] public UnitType(string id, string typeName, string mainCategoryID, string[] allCategoryIDs, int minNum, int maxNum, int minimumSize, int maximumSize, double unitCost, double trooperCost, Stats unitStats, UnitRequirement[] unitRequirements, Race parentRace) : base(id, typeName) { - mainCatID = mainCategoryID; - categoryIDs = allCategoryIDs; race = parentRace; - - if (minNum > maxNum && maxNum!=-1) - { - min = maxNum; - LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", typeName, id); - } - else - { - min = (minNum >= 0 ? minNum : 0); + mainCat = race.GetCategory(mainCategoryID); + + foreach (string catID in allCategoryIDs) + { + categories.Add(race.GetCategory(catID)); } - max = maxNum; - - if (minimumSize > maximumSize && maximumSize!=-1) - { - minSize = maximumSize; - LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", typeName, id); + MinNumber = minNum; + MaxNumber = maxNum; + MinSize = minimumSize; + MaxSize = maximumSize; + BaseUnitCost = unitCost; + CostPerTrooper = trooperCost; + UnitStats = unitStats; + + foreach (UnitRequirement requirement in requirements) + { + AddRequirement(requirement); } - else - { - minSize = (minimumSize >= 0 ? minimumSize : 0); - } - - maxSize = maximumSize; - baseUnitCost = unitCost; - costPerTrooper = trooperCost; - requirements = unitRequirements; - stats = unitStats; } public Race Race @@ -70,42 +64,56 @@ public virtual Category MainCategory { get - { - if (mainCat == null) - { - mainCat = Race.GetCategory(mainCatID); - } - + { return mainCat; } + set + { + if (value!=null && categories.Contains(value)) + { + mainCat = value; + } + else + { + throw new ArgumentException("MainCategory must exist in list of Categories"); + } + } } public virtual Category[] Categories { get - { - if (categories == null) - { - categories = new Category[categoryIDs.Length]; - - for (int i = 0; i<categoryIDs.Length; i++) - { - categories[i] = Race.GetCategory(categoryIDs[i]); - } - } - - return categories; + { + return categories.ToArray(); } } + public void AddCategory(Category cat) + { + if (!categories.Contains(cat)) + { + categories.Add(cat); + } + } + public int MinSize { get { return minSize; } + set + { + minSize = (value >= 0 ? value : 0); + CheckMinimumSize(); + } } public int MaxSize { get { return maxSize; } + set + { + maxSize = (value >= -1 ? value : -1); + CheckMinimumSize(); + } } public int BaseSize @@ -116,21 +124,51 @@ public int MinNumber { get { return min; } + set + { + min = (value >= 0 ? value : 0); + CheckMinimumNumber(); + } } public int MaxNumber { get { return max; } + set + { + max = (value >= -1 ? value : -1); + CheckMinimumNumber(); + } + } + + private void CheckMinimumNumber() + { + if (MinNumber > MaxNumber && MaxNumber!=-1) + { + MinNumber = MaxNumber; + LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", Name, ID); + } + } + + private void CheckMinimumSize() + { + if (MinSize > MaxSize && MaxSize!=-1) + { + MinSize = MaxNumber; + LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", Name, ID); + } } public double BaseUnitCost { - get { return baseUnitCost; } + get { return baseUnitCost; } + set { baseUnitCost = (value >= 0 ? value : 0); } } public double CostPerTrooper { get { return costPerTrooper; } + set { costPerTrooper = (value >= 0 ? value : 0); } } protected override string DefaultName() @@ -144,6 +182,13 @@ { return stats; } + set + { + if (value!=null) + { + stats = value; + } + } } public UnitEquipmentItem GetEquipmentItem(string id) @@ -163,12 +208,17 @@ return items; } + + public void AddRequirement(UnitRequirement requirement) + { + requirements.Add(requirement); + } public List<FailedUnitRequirement> CanAddToArmy(Army army) { List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); - if (requirements!=null && requirements.Length > 0) + if (requirements!=null && requirements.Count > 0) { foreach (UnitRequirement requirement in requirements) { @@ -188,7 +238,7 @@ { List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); - if (requirements!=null && requirements.Length > 0) + if (requirements!=null && requirements.Count > 0) { foreach (UnitRequirement requirement in requirements) {