# HG changeset patch # User IBBoard # Date 1239118086 0 # Node ID e53ed2d613a137ebdf3ce1222fb70ace52484bcf # Parent e0ce5578e7c2a40bfc3cad16bb6fd6a0429bfb97 Re #61 - Complete structure of WarFoundry API objects * Migrate AddUnit/RemoveUnit methods to Army for easier army loading * Migrate requirement checking call to Army, since ArmyCategory just called parent army anyway Also: * Use genericed collections in Army * Remove failed unit requirements from ArmyCategory * Alter army.xsd to stop negatives in equipment amounts diff -r e0ce5578e7c2 -r e53ed2d613a1 api/Commands/CreateAndAddUnitCommand.cs --- a/api/Commands/CreateAndAddUnitCommand.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Commands/CreateAndAddUnitCommand.cs Tue Apr 07 15:28:06 2009 +0000 @@ -13,16 +13,20 @@ { private UnitType addedUnitType; private Army army; - private ArmyCategory cat; - private Unit addedUnit; - - public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) + private Unit addedUnit; + + public CreateAndAddUnitCommand(UnitType toAdd, Army armyTo) + { + addedUnitType = toAdd; + army = armyTo; + } + + [Obsolete("Use two parameter constructor instead")] + public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, armyTo) { - addedUnitType = toAdd; - cat = catTo; - army = armyTo; } - + + [Obsolete("Use two parameter constructor instead")] public CreateAndAddUnitCommand(UnitType toAdd, Category catTo, Army armyTo) : this (toAdd, armyTo.GetCategory(catTo), armyTo) { } @@ -51,12 +55,12 @@ public override void Redo() { - cat.AddUnit(addedUnit); + army.AddUnit(addedUnit); } public override void Undo() { - cat.RemoveUnit(addedUnit); + army.RemoveUnit(addedUnit); } diff -r e0ce5578e7c2 -r e53ed2d613a1 api/Objects/Army.cs --- a/api/Objects/Army.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Objects/Army.cs Tue Apr 07 15:28:06 2009 +0000 @@ -3,8 +3,7 @@ // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. using System; -using System.IO; -using System.Collections; +using System.IO; using System.Collections.Generic; using System.Text; using System.Xml; @@ -24,7 +23,7 @@ private Race armyRace; private int maxPoints; private double pointsTotal; - private ArmyCategory[] categories; + private Dictionary categories; public event ObjectAddDelegate UnitAdded; public event ObjectRemoveDelegate UnitRemoved; @@ -46,40 +45,42 @@ public ArmyCategory GetCategory(Category cat) { - foreach (ArmyCategory armyCat in Categories) - { - if (armyCat.Category.Equals(cat)) - { - return armyCat; - } - } - - return null; + ArmyCategory armyCat = null; + ArmyCategories.TryGetValue(cat, out armyCat); + return armyCat; + } + + private Dictionary ArmyCategories + { + get + { + if (categories==null) + { + categories = new Dictionary(); + Category[] raceCats = Race.Categories; + ArmyCategory cat; + int raceCatCount = raceCats.Length; + + for (int i = 0; i < raceCatCount; i++) + { + Category raceCat = raceCats[i]; + cat = new ArmyCategory(this, raceCat); + categories[raceCat] = cat; + cat.PointsValueChanged+= PointsValueChangedMethod; + cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); + cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); + } + } + + return categories; + } } public ArmyCategory[] Categories { get { - if (categories==null) - { - Category[] raceCats = Race.Categories; - ArmyCategory cat; - int raceCatCount = raceCats.Length; - categories = new ArmyCategory[raceCatCount]; - - for (int i = 0; i < raceCatCount; i++) - { - cat = new ArmyCategory(this, raceCats[i]); - categories[i] = cat; - cat.PointsValueChanged+= PointsValueChangedMethod; - cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); - cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); - cat.RequirementsFailed+=new FailedUnitRequirementDelegate(Army_FailedRequirement); - } - } - - return categories; + return DictionaryUtils.ToArray(ArmyCategories); } } @@ -155,6 +156,24 @@ public double PointsTotal { get { return TotalPoints; } + } + + public void AddUnit(Unit unit) + { + List failedReqs = CanAddUnit(unit); + unit.Army = this; + ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); + armyCat.AddUnit(unit); + OnUnitAdded(unit, failedReqs); + } + + public void RemoveUnit(Unit unit) + { + List failedReqs = CanRemoveUnit(unit); + unit.Army = null; + ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); + armyCat.RemoveUnit(unit); + OnUnitRemoved(unit, failedReqs); } public Unit[] GetUnits(Category cat) @@ -169,20 +188,14 @@ public Unit[] GetUnits() { - ArrayList fullList = new ArrayList(); + List fullList = new List(); foreach(ArmyCategory cat in Categories) - { - foreach(Unit unit in cat.GetUnits()) - { - if (!fullList.Contains(unit)) - { - fullList.Add(unit); - } - } + { + fullList.AddRange(cat.GetUnits()); } - return (Unit[])fullList.ToArray(typeof(Unit)); + return fullList.ToArray(); } public int MaxPoints @@ -253,13 +266,5 @@ { OnUnitRemoved((Unit)val); } - - private void Army_FailedRequirement(List failedRequirements) - { - if (FailedRequirement!=null) - { - FailedRequirement(failedRequirements); - } - } } } diff -r e0ce5578e7c2 -r e53ed2d613a1 api/Objects/ArmyCategory.cs --- a/api/Objects/ArmyCategory.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Objects/ArmyCategory.cs Tue Apr 07 15:28:06 2009 +0000 @@ -22,7 +22,6 @@ public event DoubleValChangedDelegate PointsValueChanged; public event ObjectAddDelegate UnitAdded; public event ObjectRemoveDelegate UnitRemoved; - public event FailedUnitRequirementDelegate RequirementsFailed; public ArmyCategory(Army army, Category cat) : base() { @@ -65,30 +64,25 @@ } } - public void AddUnit(Unit unit) + internal void AddUnit(Unit unit) { - List failedReqs = ParentArmy.CanAddUnit(unit); - units.Add(unit); - unit.Army = ParentArmy; unit.Category = this; unit.PointsValueChanged+= PointsValueChangedMethod; int unitTypeCount; unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; - TotalPoints+= unit.PointsValue; - OnUnitAdded(unit, failedReqs); + TotalPoints+= unit.PointsValue; + OnUnitAdded(unit); } - public void RemoveUnit(Unit unit) + internal void RemoveUnit(Unit unit) { - List failedReqs = ParentArmy.CanRemoveUnit(unit); - units.Remove(unit); - unit.Army = null; + units.Remove(unit); unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; TotalPoints-= unit.PointsValue; - unit.PointsValueChanged-= PointsValueChangedMethod; - OnUnitRemoved(unit, failedReqs); + unit.PointsValueChanged-= PointsValueChangedMethod; + OnUnitRemoved(unit); } public int GetUnitTypeCount(UnitType unitType) @@ -130,30 +124,20 @@ } } - private void OnUnitAdded(Unit unit, List failedReqs) + private void OnUnitAdded(Unit unit) { if (UnitAdded!=null) { UnitAdded(unit); } - - if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count > 0) - { - RequirementsFailed(failedReqs); - } } - private void OnUnitRemoved(Unit unit, List failedReqs) + private void OnUnitRemoved(Unit unit) { if (UnitRemoved!=null) { UnitRemoved(unit); } - - if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count>0) - { - RequirementsFailed(failedReqs); - } } protected virtual void OnPointsValueChanged(double oldValue, double newValue) diff -r e0ce5578e7c2 -r e53ed2d613a1 dtds/army.xsd --- a/dtds/army.xsd Tue Apr 07 14:43:04 2009 +0000 +++ b/dtds/army.xsd Tue Apr 07 15:28:06 2009 +0000 @@ -35,7 +35,7 @@ - +