Mercurial > repos > snowblizz-super-API-ideas
changeset 357:50d0d3b39a0b
Re #140: Add equality methods
* Add equality method for RequiresAtLeastNUnitsRequirement
* Update equality method on GameSystem and No More Than limit to use new helper class
* Add equality methods on Race and UnitType as a dependent of equality of requirements
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 26 Apr 2011 19:19:08 +0000 |
parents | 51cccccf3669 |
children | dbe7ccb1e557 |
files | API/Objects/GameSystem.cs API/Objects/Race.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/UnitType.cs |
diffstat | 5 files changed, 567 insertions(+), 501 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Objects/GameSystem.cs Mon Apr 25 15:11:20 2011 +0000 +++ b/API/Objects/GameSystem.cs Tue Apr 26 19:19:08 2011 +0000 @@ -4,6 +4,7 @@ using System.Collections.Generic; using IBBoard.WarFoundry.API.Factories; +using Col = IBBoard.Collections; namespace IBBoard.WarFoundry.API.Objects { @@ -221,8 +222,14 @@ if (obj.GetType().Equals(this.GetType())) { GameSystem otherSystem = (GameSystem)obj; - - return this.ID == otherSystem.ID && this.Name == otherSystem.Name && ((this.RawCategories == null && otherSystem.RawCategories == null) || this.RawCategories.Equals(otherSystem.RawCategories)); + if (!ID.Equals(otherSystem.ID) || !Name.Equals(otherSystem.Name) || !Col.Collections.AreEqual(RawCategories, otherSystem.RawCategories)) + { + return false; + } + else + { + return true; + } } else {
--- a/API/Objects/Race.cs Mon Apr 25 15:11:20 2011 +0000 +++ b/API/Objects/Race.cs Tue Apr 26 19:19:08 2011 +0000 @@ -1,323 +1,348 @@ -// This file (Race.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 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 System.Collections.Generic; -using System.IO; -using System.Xml; -using IBBoard.IO; -using IBBoard.WarFoundry.API.Factories; -using IBBoard.WarFoundry.API.Objects.Requirement; - -namespace IBBoard.WarFoundry.API.Objects -{ - public class Race : WarFoundryStagedLoadingObject - { - public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; - - private string subID; - private GameSystem system; - private string defaultArmyName = ""; - private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; - private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>(); - private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>(); - private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>(); - private Dictionary<string, Category> categories = new Dictionary<string,Category>(); - private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>(); - - public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory) - { - } - - public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID != "" ? "_" + raceSubID : ""), raceName, creatingFactory) - { - subID = (raceSubID == null ? "" : raceSubID); - system = gameSystem; - } - - public string SubID - { - get { return subID; } - set { subID = (value == null ? "" : value.Trim()); } - } - - public GameSystem GameSystem - { - get { return system; } - set - { - if (value == null) - { - throw new ArgumentException("Game system for a race cannot be null"); - } - - system = value; - } - } - - public string ArmyDefaultName - { - get { return defaultArmyName; } - set - { - if (value == null) - { - throw new ArgumentException("No default army name"); - } - - defaultArmyName = value; - } - } - - public void AddCategory(Category cat) - { - categories[cat.ID] = cat; - } - - /// <summary> - /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID. - /// </summary> - /// <param name="id"> - /// The ID of the category to get - /// </param> - /// <returns> - /// The <code>Category</code> with the specified ID, or null if one doesn't exist. - /// </returns> - public Category GetCategory(string id) - { - EnsureFullyLoaded(); - Category cat = null; - categories.TryGetValue(id, out cat); - - if (cat == null) - { - cat = GameSystem.GetCategory(id); - } - - return cat; - } - - public Category[] Categories - { - get - { - EnsureFullyLoaded(); - Category[] cats; - - if (!HasCategoryOverrides()) - { - cats = GameSystem.Categories; - } - else - { - cats = DictionaryUtils.ToArray<string, Category>(categories); - } - - return cats; - } - } - - public bool HasCategoryOverrides() - { - EnsureFullyLoaded(); - return categories.Count > 0; - } - - public void AddEquipmentItem(EquipmentItem item) - { - //TODO: Throw DuplicateItemException - equipment.Add(item.ID, item); - } - - public EquipmentItem GetEquipmentItem(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(equipment, id); - } - - public List<EquipmentItem> GetEquipmentList() - { - EnsureFullyLoaded(); - List<EquipmentItem> items = new List<EquipmentItem>(); - - foreach (EquipmentItem item in equipment.Values) - { - items.Add(item); - } - - return items; - } - - public void AddUnitType(UnitType type) - { - CacheUnitType(type); - unitTypes.Add(type.ID, type); - } - - public UnitType[] GetUnitTypes(Category cat) - { - EnsureFullyLoaded(); - BuildUnitTypesByCategoryCache(); - Dictionary<string, UnitType> unitTypesDictionary; - unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); - UnitType[] unitTypesArray; - - if (unitTypesDictionary == null) - { - unitTypesArray = new UnitType[0]; - } - else - { - unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary); - } - - return unitTypesArray; - } - - private void CacheUnitType(UnitType unit) - { - BuildUnitTypesByCategoryCache(); - - foreach (Category cat in unit.Categories) - { - Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat); - - if (catUnitTypes == null) - { - throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID)); - } - - catUnitTypes.Add(unit.ID, unit); - } - } - - private void BuildUnitTypesByCategoryCache() - { - if (unitTypesByCat == null) - { - DoBuildUnitTypesByCategoryCache(); - } - } - - private void DoBuildUnitTypesByCategoryCache() - { - unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); - - foreach (Category category in Categories) - { - unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); - } - - foreach (UnitType unit in unitTypes.Values) - { - CacheUnitType(unit); - } - } - - public UnitType GetUnitType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(unitTypes, id); - } - - public List<Ability> GetAbilityList() - { - EnsureFullyLoaded(); - List<Ability> items = new List<Ability>(); - items.AddRange(abilities.Values); - return items; - } - - public void AddAbility(Ability newAbility) - { - //TODO: Throw DuplicateItemException - abilities.Add(newAbility.ID, newAbility); - } - - public Ability GetAbility(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(abilities, id); - } - - protected virtual Dictionary<string, UnitType> RaceUnitTypes - { - get { return RaceRawUnitTypes; } - set { RaceRawUnitTypes = value; } - } - - protected virtual Dictionary<string, EquipmentItem> RaceEquipment - { - get { return RaceRawEquipment; } - set { RaceRawEquipment = value; } - } - - protected virtual Dictionary<string, Ability> RaceAbilities - { - get { return RaceRawAbilities; } - set { RaceRawAbilities = value; } - } - - protected Dictionary<string, UnitType> RaceRawUnitTypes - { - get { return unitTypes; } - set { unitTypes = value; } - } - - protected Dictionary<string, EquipmentItem> RaceRawEquipment - { - get { return equipment; } - set { equipment = value; } - } - - protected Dictionary<string, Ability> RaceRawAbilities - { - get { return abilities; } - set { abilities = value; } - } - - public void AddUnitMemberType(UnitMemberType memberType) - { - memberTypes[memberType.ID] = memberType; - } - - /// <summary> - /// Gets a unit member type by its ID. - /// </summary> - /// <param name="id"> - /// The ID of the unit member type to get - /// </param> - /// <returns> - /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist. - /// </returns> - public UnitMemberType GetUnitMemberType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(memberTypes, id); - } - - public UnitMemberType[] UnitMemberTypes - { - get - { - EnsureFullyLoaded(); - return DictionaryUtils.ToArray(memberTypes); - } - } - - public ICollection<IRequirement> GetRequirements() - { - ICollection<IRequirement> reqs = new List<IRequirement>(); - - foreach (UnitType unitType in unitTypes.Values) - { - foreach (IRequirement requirement in unitType.GetRequirements()) - { - reqs.Add(requirement); - } - } - - return reqs; - } - } -} +// This file (Race.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 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 System.Collections.Generic; +using System.IO; +using System.Xml; +using IBBoard.IO; +using IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Objects.Requirement; + +namespace IBBoard.WarFoundry.API.Objects +{ + public class Race : WarFoundryStagedLoadingObject + { + public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; + + private string subID; + private GameSystem system; + private string defaultArmyName = ""; + private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; + private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>(); + private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>(); + private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>(); + private Dictionary<string, Category> categories = new Dictionary<string,Category>(); + private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>(); + + public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory) + { + } + + public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID != "" ? "_" + raceSubID : ""), raceName, creatingFactory) + { + subID = (raceSubID == null ? "" : raceSubID); + system = gameSystem; + } + + public override bool Equals (object obj) + { + if (obj == null) + { + return false; + } + else if (!(obj is Race)) + { + return false; + } + else + { + Race other = (Race)obj; + + if (!ID.Equals(other.ID) || !SubID.Equals(other.SubID) || !GameSystem.Equals(other.GameSystem)) + { + return false; + } + else + { + return true; + } + } + } + + public string SubID + { + get { return subID; } + set { subID = (value == null ? "" : value.Trim()); } + } + + public GameSystem GameSystem + { + get { return system; } + set + { + if (value == null) + { + throw new ArgumentException("Game system for a race cannot be null"); + } + + system = value; + } + } + + public string ArmyDefaultName + { + get { return defaultArmyName; } + set + { + if (value == null) + { + throw new ArgumentException("No default army name"); + } + + defaultArmyName = value; + } + } + + public void AddCategory(Category cat) + { + categories[cat.ID] = cat; + } + + /// <summary> + /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID. + /// </summary> + /// <param name="id"> + /// The ID of the category to get + /// </param> + /// <returns> + /// The <code>Category</code> with the specified ID, or null if one doesn't exist. + /// </returns> + public Category GetCategory(string id) + { + EnsureFullyLoaded(); + Category cat = null; + categories.TryGetValue(id, out cat); + + if (cat == null) + { + cat = GameSystem.GetCategory(id); + } + + return cat; + } + + public Category[] Categories + { + get + { + EnsureFullyLoaded(); + Category[] cats; + + if (!HasCategoryOverrides()) + { + cats = GameSystem.Categories; + } + else + { + cats = DictionaryUtils.ToArray<string, Category>(categories); + } + + return cats; + } + } + + public bool HasCategoryOverrides() + { + EnsureFullyLoaded(); + return categories.Count > 0; + } + + public void AddEquipmentItem(EquipmentItem item) + { + //TODO: Throw DuplicateItemException + equipment.Add(item.ID, item); + } + + public EquipmentItem GetEquipmentItem(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(equipment, id); + } + + public List<EquipmentItem> GetEquipmentList() + { + EnsureFullyLoaded(); + List<EquipmentItem> items = new List<EquipmentItem>(); + + foreach (EquipmentItem item in equipment.Values) + { + items.Add(item); + } + + return items; + } + + public void AddUnitType(UnitType type) + { + CacheUnitType(type); + unitTypes.Add(type.ID, type); + } + + public UnitType[] GetUnitTypes(Category cat) + { + EnsureFullyLoaded(); + BuildUnitTypesByCategoryCache(); + Dictionary<string, UnitType> unitTypesDictionary; + unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); + UnitType[] unitTypesArray; + + if (unitTypesDictionary == null) + { + unitTypesArray = new UnitType[0]; + } + else + { + unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary); + } + + return unitTypesArray; + } + + private void CacheUnitType(UnitType unit) + { + BuildUnitTypesByCategoryCache(); + + foreach (Category cat in unit.Categories) + { + Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat); + + if (catUnitTypes == null) + { + throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID)); + } + + catUnitTypes.Add(unit.ID, unit); + } + } + + private void BuildUnitTypesByCategoryCache() + { + if (unitTypesByCat == null) + { + DoBuildUnitTypesByCategoryCache(); + } + } + + private void DoBuildUnitTypesByCategoryCache() + { + unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); + + foreach (Category category in Categories) + { + unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); + } + + foreach (UnitType unit in unitTypes.Values) + { + CacheUnitType(unit); + } + } + + public UnitType GetUnitType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(unitTypes, id); + } + + public List<Ability> GetAbilityList() + { + EnsureFullyLoaded(); + List<Ability> items = new List<Ability>(); + items.AddRange(abilities.Values); + return items; + } + + public void AddAbility(Ability newAbility) + { + //TODO: Throw DuplicateItemException + abilities.Add(newAbility.ID, newAbility); + } + + public Ability GetAbility(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(abilities, id); + } + + protected virtual Dictionary<string, UnitType> RaceUnitTypes + { + get { return RaceRawUnitTypes; } + set { RaceRawUnitTypes = value; } + } + + protected virtual Dictionary<string, EquipmentItem> RaceEquipment + { + get { return RaceRawEquipment; } + set { RaceRawEquipment = value; } + } + + protected virtual Dictionary<string, Ability> RaceAbilities + { + get { return RaceRawAbilities; } + set { RaceRawAbilities = value; } + } + + protected Dictionary<string, UnitType> RaceRawUnitTypes + { + get { return unitTypes; } + set { unitTypes = value; } + } + + protected Dictionary<string, EquipmentItem> RaceRawEquipment + { + get { return equipment; } + set { equipment = value; } + } + + protected Dictionary<string, Ability> RaceRawAbilities + { + get { return abilities; } + set { abilities = value; } + } + + public void AddUnitMemberType(UnitMemberType memberType) + { + memberTypes[memberType.ID] = memberType; + } + + /// <summary> + /// Gets a unit member type by its ID. + /// </summary> + /// <param name="id"> + /// The ID of the unit member type to get + /// </param> + /// <returns> + /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist. + /// </returns> + public UnitMemberType GetUnitMemberType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(memberTypes, id); + } + + public UnitMemberType[] UnitMemberTypes + { + get + { + EnsureFullyLoaded(); + return DictionaryUtils.ToArray(memberTypes); + } + } + + public ICollection<IRequirement> GetRequirements() + { + ICollection<IRequirement> reqs = new List<IRequirement>(); + + foreach (UnitType unitType in unitTypes.Values) + { + foreach (IRequirement requirement in unitType.GetRequirements()) + { + reqs.Add(requirement); + } + } + + return reqs; + } + } +}
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Mon Apr 25 15:11:20 2011 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Tue Apr 26 19:19:08 2011 +0000 @@ -1,174 +1,198 @@ -// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard +// This file (UnitRequiresAtLeastNUnitsRequirement.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 System.Collections.Generic; +using System; +using System.Collections.Generic; using IBBoard.WarFoundry.API.Objects; namespace IBBoard.WarFoundry.API.Objects.Requirement -{ - /// <summary> - /// A requirement where a WarFoundryObject requires at least N units of one or more unit types before any number of that object can be taken in an army. +{ + /// <summary> + /// A requirement where a WarFoundryObject requires at least N units of one or more unit types before any number of that object can be taken in an army. /// </summary> public class RequiresAtLeastNUnitsRequirement : IRequirement - { - private List<UnitCountRequirementData> requiredTypes; + { + private List<UnitCountRequirementData> requiredTypes; public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes) - { - requiredTypes = new List<UnitCountRequirementData>(); - - foreach (UnitType unitType in requiredUnitTypes) - { - AddUnitTypeRequirement(unitType); + { + requiredTypes = new List<UnitCountRequirementData>(); + + foreach (UnitType unitType in requiredUnitTypes) + { + AddUnitTypeRequirement(unitType); + } + } + + public override bool Equals (object obj) + { + if (obj == null) + { + return false; + } + else if (obj.GetType().Equals(this.GetType())) + { + RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj; + if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes)) + { + return false; + } + else + { + return true; + } + } + else + { + return false; + } + } + + /// <summary> + /// Checks whether the supplied WarFoundryObject can be added to the supplied army. + /// </summary> + /// <returns> + /// A <code>Validation</code> enum to show the result of the validation + /// </returns> + /// <param name='wfObject'> + /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement + /// </param> + /// <param name='toArmy'> + /// The army to add the object to. + /// </param> + public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + { + return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy) + { + return IsApplicable(toArmy) || IsApplicable(toObjectAdded); + } + + protected virtual bool IsApplicable(Army toArmy) + { + return false; + } + + protected virtual bool IsApplicable(WarFoundryObject toObject) + { + bool isApplicable = false; + UnitType unitType = GetUnitTypeFromObject(toObject); + + if (unitType != null) + { + isApplicable = IsApplicable(unitType); + } + + return isApplicable; + } + + protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) + { + UnitType unitType = null; + + if (toObject is UnitType) + { + unitType = (UnitType)toObject; + } + else if (toObject is Unit) + { + unitType = ((Unit)toObject).UnitType; } - } - - /// <summary> - /// Checks whether the supplied WarFoundryObject can be added to the supplied army. - /// </summary> - /// <returns> - /// A <code>Validation</code> enum to show the result of the validation - /// </returns> - /// <param name='wfObject'> - /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement - /// </param> - /// <param name='toArmy'> - /// The army to add the object to. - /// </param> - public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) - { - return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; - } - - private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy) - { - return IsApplicable(toArmy) || IsApplicable(toObjectAdded); - } - - protected virtual bool IsApplicable(Army toArmy) - { - return false; - } - - protected virtual bool IsApplicable(WarFoundryObject toObject) - { - bool isApplicable = false; - UnitType unitType = GetUnitTypeFromObject(toObject); - - if (unitType != null) - { - isApplicable = IsApplicable(unitType); - } - - return isApplicable; - } - - protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) - { - UnitType unitType = null; - - if (toObject is UnitType) - { - unitType = (UnitType)toObject; - } - else if (toObject is Unit) - { - unitType = ((Unit)toObject).UnitType; - } - - return unitType; - } - - private bool IsApplicable (UnitType unitType) - { - bool isApplicable = false; - foreach (UnitCountRequirementData requirement in requiredTypes) - { - if (requirement.UnitType.Equals(unitType)) - { - isApplicable = true; - break; - } - } - return isApplicable; - } - - private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) - { - Validation isValid = Validation.Passed; - - foreach (UnitCountRequirementData requirement in requiredTypes) - { - if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) - { - isValid = Validation.Failed; - break; - } - } - - return isValid; - } - - private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) - { - return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); - } - - private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) - { - return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; - } - - /// <summary> - /// Adds a requirement for there to be at least minCount of a given UnitType - /// </summary> - /// <param name='unitType'> - /// The unit type to require. - /// </param> - /// <param name='minCount'> - /// The minimum number of that type that must exist. - /// </param> - public void AddUnitTypeRequirement(UnitType unitType, int minCount) - { - requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); + + return unitType; + } + + private bool IsApplicable (UnitType unitType) + { + bool isApplicable = false; + foreach (UnitCountRequirementData requirement in requiredTypes) + { + if (requirement.UnitType.Equals(unitType)) + { + isApplicable = true; + break; + } + } + return isApplicable; + } + + private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) + { + Validation isValid = Validation.Passed; + + foreach (UnitCountRequirementData requirement in requiredTypes) + { + if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) + { + isValid = Validation.Failed; + break; + } + } + + return isValid; + } + + private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) + { + return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); + } + + private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) + { + return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; } - - /// <summary> - /// Adds a requirement for there to be one or more of a given UnitType - /// </summary> - /// <param name='unitType'> - /// The unit type to require. - /// </param> - public void AddUnitTypeRequirement (UnitType unitType) - { - AddUnitTypeRequirement(unitType, 1); - } - - /// <summary> - /// Checks whether the supplied army is currently valid according to this requirement. - /// </summary> - /// <returns> - /// A <code>Validation</code> enum to show the result of the validation - /// </returns> - /// <param name='toValidate'> - /// The army to validate - /// </param> - public virtual Validation ValidatesArmy(Army toValidate) - { - Validation isValid = Validation.Passed; - - foreach (UnitCountRequirementData requirement in requiredTypes) - { - if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) - { - isValid = Validation.Failed; - break; - } - } - - return isValid; - } + + /// <summary> + /// Adds a requirement for there to be at least minCount of a given UnitType + /// </summary> + /// <param name='unitType'> + /// The unit type to require. + /// </param> + /// <param name='minCount'> + /// The minimum number of that type that must exist. + /// </param> + public void AddUnitTypeRequirement(UnitType unitType, int minCount) + { + requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); + } + + /// <summary> + /// Adds a requirement for there to be one or more of a given UnitType + /// </summary> + /// <param name='unitType'> + /// The unit type to require. + /// </param> + public void AddUnitTypeRequirement (UnitType unitType) + { + AddUnitTypeRequirement(unitType, 1); + } + + /// <summary> + /// Checks whether the supplied army is currently valid according to this requirement. + /// </summary> + /// <returns> + /// A <code>Validation</code> enum to show the result of the validation + /// </returns> + /// <param name='toValidate'> + /// The army to validate + /// </param> + public virtual Validation ValidatesArmy(Army toValidate) + { + Validation isValid = Validation.Passed; + + foreach (UnitCountRequirementData requirement in requiredTypes) + { + if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) + { + isValid = Validation.Failed; + break; + } + } + + return isValid; + } } }
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Mon Apr 25 15:11:20 2011 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Tue Apr 26 19:19:08 2011 +0000 @@ -126,22 +126,7 @@ { RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj; - if (limitedTypes.Count != other.limitedTypes.Count) - { - return false; - } - else - { - foreach (UnitCountRequirementData req in limitedTypes) - { - if (!other.limitedTypes.Contains(req)) - { - - } - } - - return true; - } + return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes); } }
--- a/API/Objects/UnitType.cs Mon Apr 25 15:11:20 2011 +0000 +++ b/API/Objects/UnitType.cs Tue Apr 26 19:19:08 2011 +0000 @@ -45,6 +45,31 @@ race = parentRace; } + public override bool Equals (object obj) + { + if (obj == null) + { + return false; + } + else if (!(obj is UnitType)) + { + return false; + } + else + { + UnitType other = (UnitType)obj; + + if (!ID.Equals(other.ID) || !Name.Equals(other.Name) || !Race.Equals(other.Race)) + { + return false; + } + else + { + return true; + } + } + } + public GameSystem GameSystem { get { return Race.GameSystem; }