# HG changeset patch # User IBBoard # Date 1303845548 0 # Node ID 50d0d3b39a0b0067afe895baf612d096fb18736e # Parent 51cccccf36698a2c3fd5362330d46fa4ba18c018 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 diff -r 51cccccf3669 -r 50d0d3b39a0b API/Objects/GameSystem.cs --- 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 { diff -r 51cccccf3669 -r 50d0d3b39a0b API/Objects/Race.cs --- 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> unitTypesByCat; - private Dictionary unitTypes = new Dictionary(); - private Dictionary equipment = new Dictionary(); - private Dictionary abilities = new Dictionary(); - private Dictionary categories = new Dictionary(); - private Dictionary memberTypes = new Dictionary(); - - 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; - } - - /// - /// 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. - /// - /// - /// The ID of the category to get - /// - /// - /// The Category with the specified ID, or null if one doesn't exist. - /// - 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(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 GetEquipmentList() - { - EnsureFullyLoaded(); - List items = new List(); - - 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 unitTypesDictionary; - unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); - UnitType[] unitTypesArray; - - if (unitTypesDictionary == null) - { - unitTypesArray = new UnitType[0]; - } - else - { - unitTypesArray = DictionaryUtils.ToArray(unitTypesDictionary); - } - - return unitTypesArray; - } - - private void CacheUnitType(UnitType unit) - { - BuildUnitTypesByCategoryCache(); - - foreach (Category cat in unit.Categories) - { - Dictionary 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>(); - - foreach (Category category in Categories) - { - unitTypesByCat.Add(category, new Dictionary()); - } - - foreach (UnitType unit in unitTypes.Values) - { - CacheUnitType(unit); - } - } - - public UnitType GetUnitType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(unitTypes, id); - } - - public List GetAbilityList() - { - EnsureFullyLoaded(); - List items = new List(); - 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 RaceUnitTypes - { - get { return RaceRawUnitTypes; } - set { RaceRawUnitTypes = value; } - } - - protected virtual Dictionary RaceEquipment - { - get { return RaceRawEquipment; } - set { RaceRawEquipment = value; } - } - - protected virtual Dictionary RaceAbilities - { - get { return RaceRawAbilities; } - set { RaceRawAbilities = value; } - } - - protected Dictionary RaceRawUnitTypes - { - get { return unitTypes; } - set { unitTypes = value; } - } - - protected Dictionary RaceRawEquipment - { - get { return equipment; } - set { equipment = value; } - } - - protected Dictionary RaceRawAbilities - { - get { return abilities; } - set { abilities = value; } - } - - public void AddUnitMemberType(UnitMemberType memberType) - { - memberTypes[memberType.ID] = memberType; - } - - /// - /// Gets a unit member type by its ID. - /// - /// - /// The ID of the unit member type to get - /// - /// - /// The UnitMemberType with the specified ID, or null if one doesn't exist. - /// - public UnitMemberType GetUnitMemberType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(memberTypes, id); - } - - public UnitMemberType[] UnitMemberTypes - { - get - { - EnsureFullyLoaded(); - return DictionaryUtils.ToArray(memberTypes); - } - } - - public ICollection GetRequirements() - { - ICollection reqs = new List(); - - 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> unitTypesByCat; + private Dictionary unitTypes = new Dictionary(); + private Dictionary equipment = new Dictionary(); + private Dictionary abilities = new Dictionary(); + private Dictionary categories = new Dictionary(); + private Dictionary memberTypes = new Dictionary(); + + 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; + } + + /// + /// 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. + /// + /// + /// The ID of the category to get + /// + /// + /// The Category with the specified ID, or null if one doesn't exist. + /// + 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(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 GetEquipmentList() + { + EnsureFullyLoaded(); + List items = new List(); + + 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 unitTypesDictionary; + unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); + UnitType[] unitTypesArray; + + if (unitTypesDictionary == null) + { + unitTypesArray = new UnitType[0]; + } + else + { + unitTypesArray = DictionaryUtils.ToArray(unitTypesDictionary); + } + + return unitTypesArray; + } + + private void CacheUnitType(UnitType unit) + { + BuildUnitTypesByCategoryCache(); + + foreach (Category cat in unit.Categories) + { + Dictionary 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>(); + + foreach (Category category in Categories) + { + unitTypesByCat.Add(category, new Dictionary()); + } + + foreach (UnitType unit in unitTypes.Values) + { + CacheUnitType(unit); + } + } + + public UnitType GetUnitType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(unitTypes, id); + } + + public List GetAbilityList() + { + EnsureFullyLoaded(); + List items = new List(); + 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 RaceUnitTypes + { + get { return RaceRawUnitTypes; } + set { RaceRawUnitTypes = value; } + } + + protected virtual Dictionary RaceEquipment + { + get { return RaceRawEquipment; } + set { RaceRawEquipment = value; } + } + + protected virtual Dictionary RaceAbilities + { + get { return RaceRawAbilities; } + set { RaceRawAbilities = value; } + } + + protected Dictionary RaceRawUnitTypes + { + get { return unitTypes; } + set { unitTypes = value; } + } + + protected Dictionary RaceRawEquipment + { + get { return equipment; } + set { equipment = value; } + } + + protected Dictionary RaceRawAbilities + { + get { return abilities; } + set { abilities = value; } + } + + public void AddUnitMemberType(UnitMemberType memberType) + { + memberTypes[memberType.ID] = memberType; + } + + /// + /// Gets a unit member type by its ID. + /// + /// + /// The ID of the unit member type to get + /// + /// + /// The UnitMemberType with the specified ID, or null if one doesn't exist. + /// + public UnitMemberType GetUnitMemberType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(memberTypes, id); + } + + public UnitMemberType[] UnitMemberTypes + { + get + { + EnsureFullyLoaded(); + return DictionaryUtils.ToArray(memberTypes); + } + } + + public ICollection GetRequirements() + { + ICollection reqs = new List(); + + foreach (UnitType unitType in unitTypes.Values) + { + foreach (IRequirement requirement in unitType.GetRequirements()) + { + reqs.Add(requirement); + } + } + + return reqs; + } + } +} diff -r 51cccccf3669 -r 50d0d3b39a0b API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- 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 -{ - /// - /// 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. +{ + /// + /// 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. /// public class RequiresAtLeastNUnitsRequirement : IRequirement - { - private List requiredTypes; + { + private List requiredTypes; public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes) - { - requiredTypes = new List(); - - foreach (UnitType unitType in requiredUnitTypes) - { - AddUnitTypeRequirement(unitType); + { + requiredTypes = new List(); + + 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; + } + } + + /// + /// Checks whether the supplied WarFoundryObject can be added to the supplied army. + /// + /// + /// A Validation enum to show the result of the validation + /// + /// + /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement + /// + /// + /// The army to add the object to. + /// + 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; } - } - - /// - /// Checks whether the supplied WarFoundryObject can be added to the supplied army. - /// - /// - /// A Validation enum to show the result of the validation - /// - /// - /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement - /// - /// - /// The army to add the object to. - /// - 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; - } - - /// - /// Adds a requirement for there to be at least minCount of a given UnitType - /// - /// - /// The unit type to require. - /// - /// - /// The minimum number of that type that must exist. - /// - 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; } - - /// - /// Adds a requirement for there to be one or more of a given UnitType - /// - /// - /// The unit type to require. - /// - public void AddUnitTypeRequirement (UnitType unitType) - { - AddUnitTypeRequirement(unitType, 1); - } - - /// - /// Checks whether the supplied army is currently valid according to this requirement. - /// - /// - /// A Validation enum to show the result of the validation - /// - /// - /// The army to validate - /// - 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; - } + + /// + /// Adds a requirement for there to be at least minCount of a given UnitType + /// + /// + /// The unit type to require. + /// + /// + /// The minimum number of that type that must exist. + /// + public void AddUnitTypeRequirement(UnitType unitType, int minCount) + { + requiredTypes.Add(new UnitCountRequirementData(unitType, minCount)); + } + + /// + /// Adds a requirement for there to be one or more of a given UnitType + /// + /// + /// The unit type to require. + /// + public void AddUnitTypeRequirement (UnitType unitType) + { + AddUnitTypeRequirement(unitType, 1); + } + + /// + /// Checks whether the supplied army is currently valid according to this requirement. + /// + /// + /// A Validation enum to show the result of the validation + /// + /// + /// The army to validate + /// + 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; + } } } diff -r 51cccccf3669 -r 50d0d3b39a0b API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- 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); } } diff -r 51cccccf3669 -r 50d0d3b39a0b API/Objects/UnitType.cs --- 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; }