changeset 161:81abc04b3dbe

Re #192: Improve "clean coding" practice * Move some calculation methods for equipment out of the encapsulation objects
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Oct 2009 12:00:16 +0000
parents 41b927998a41
children 624422e91a1c
files IBBoard.WarFoundry.API.csproj api/Objects/Unit.cs api/Objects/UnitEquipmentItem.cs api/Util/UnitEquipmentUtil.cs
diffstat 4 files changed, 119 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj	Sun Oct 04 10:49:17 2009 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Sun Oct 04 12:00:16 2009 +0000
@@ -53,6 +53,8 @@
     </None>
     <None Include="dtds\army.xsd">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      <SubType>
+      </SubType>
     </None>
     <None Include="COPYING" />
     <Compile Include="api\Objects\ICostedWarFoundryObject.cs" />
@@ -117,6 +119,7 @@
     <Compile Include="api\Requirements\UnitRequiresAtLeastRequirement.cs" />
     <Compile Include="api\Savers\IWarFoundryFileSaver.cs" />
     <Compile Include="api\Savers\WarFoundrySaver.cs" />
+    <Compile Include="api\Util\UnitEquipmentUtil.cs" />
     <Compile Include="api\WarFoundryCore.cs" />
     <Compile Include="api\WarFoundryLoader.cs" />
     <Compile Include="AssemblyInfo.cs" />
@@ -152,6 +155,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <Reference Include="System.Data" />
     <Reference Include="System.Xml" />
     <Reference Include="System" />
     <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73">
--- a/api/Objects/Unit.cs	Sun Oct 04 10:49:17 2009 +0000
+++ b/api/Objects/Unit.cs	Sun Oct 04 12:00:16 2009 +0000
@@ -6,7 +6,8 @@
 using System.Collections.Generic;
 using System.Text;
 using System.Xml;
-using IBBoard.Lang;
+using IBBoard.Lang;
+using IBBoard.WarFoundry.API.Util;
 
 namespace IBBoard.WarFoundry.API.Objects
 {
@@ -220,39 +221,10 @@
 			}
 		}
 
+		[Obsolete("Use UnitEquipmentUtil.GetAllowedEquipmentItems(Unit) instead")]
 		public UnitEquipmentItem[] GetAllowedAdditionalEquipment()
-		{
-			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
-			List<string> existingMutexGroups = new List<string>();
-			
-			foreach (UnitEquipmentItem item in GetEquipment())
-			{
-				foreach (string mutex in item.MutexGroups)
-				{
-					existingMutexGroups.Add(mutex);
-				}
-			}
-
-			foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems())
-			{
-				bool mutexMatch = false;
-
-				foreach (string mutex in item.MutexGroups)
-				{
-					if (existingMutexGroups.Contains(mutex))
-					{
-						mutexMatch = true;
-						break;
-					}
-				}
-
-				if (!mutexMatch)
-				{
-					list.Add(item);
-				}
-			}
-
-			return list.ToArray();
+		{
+			return UnitEquipmentUtil.GetAllowedEquipmentItems(this);
 		}
 
 		public UnitEquipmentItem[] GetEquipment()
@@ -468,24 +440,10 @@
 			return canEquip;
 		}
 
-		/// <summary>
-		/// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
-		/// </summary>
-		/// <param name="item">The item to check blocking items for</param>
-		/// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
+		[Obsolete("Use UnitEquipmentUtil.GetBlockingEquipmentItems(Unit, UnitEquipmentItem) instead")]
 		public List<UnitEquipmentItem> GetBlockingEquipmentItems(UnitEquipmentItem item)
-		{
-			List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
-
-			foreach (UnitEquipmentItem unitItem in GetEquipment())
-			{
-				if (unitItem.IsMutuallyExclusive(item))
-				{
-					items.Add(unitItem);
-				}
-			}
-
-			return items;
+		{
+			return UnitEquipmentUtil.GetBlockingEquipmentItems(this, item);
 		}
 
 		public bool CanEquipWithItem(string equipID)
--- a/api/Objects/UnitEquipmentItem.cs	Sun Oct 04 10:49:17 2009 +0000
+++ b/api/Objects/UnitEquipmentItem.cs	Sun Oct 04 12:00:16 2009 +0000
@@ -3,7 +3,8 @@
 // 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.Lang;
+using IBBoard.Lang;
+using IBBoard.WarFoundry.API.Util;
 
 namespace IBBoard.WarFoundry.API.Objects
 {
@@ -229,29 +230,10 @@
 			return EquipmentItem.CanBeUsedWithArmourType(otherItemType);
 		}
 
-		/// <summary>
-		/// Checks the "mutex" (mutual exclusion) groups of the other item against its own and determines whether the two items are mutually exclusive (share at least one mutex group)
-		/// </summary>
-		/// <param name="item">the item to check against</param>
-		/// <returns><code>true</code> if the two items share at least one mutex group, else <code>false</code></returns>
+		[Obsolete("Use UnitEquipmentUtil method instead")]
 		public bool IsMutuallyExclusive(UnitEquipmentItem item)
-		{
-			bool areMutex = false;
-
-			foreach (string mutex in MutexGroups)
-			{
-				foreach (string otherMutex in item.MutexGroups)
-				{
-					if (mutex.Equals(otherMutex))
-					{
-						areMutex = true;
-						goto postLoop;
-					}
-				}
-			}
-			postLoop:
-
-			return areMutex;
+		{
+			return UnitEquipmentUtil.ItemsAreMutuallyExclusive(this, item);
 		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Sun Oct 04 12:00:16 2009 +0000
@@ -0,0 +1,102 @@
+// This file (UnitEquipmentUtil.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 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.Text;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API.Util
+{
+	class UnitEquipmentUtil
+	{
+		/// <summary>
+		/// Gets an array of allowed <see cref="UnitEquipmentItem"/>s based on the current selections of the unit, taking in to account Mutex groups and other limits.
+		/// </summary>
+		/// <param name="unit">The <see cref="Unit"/> to get equipment items for</param>
+		/// <returns>The array of allowed <see cref="UnitEquipmentItem"/>s</returns>
+		public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
+		{
+			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
+			UnitEquipmentItem[] currItems = unit.GetEquipment();
+
+			foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
+			{
+				bool allowed = IsAllowedByMutex(item, currItems);
+
+				if (allowed)
+				{
+					list.Add(item);
+				}
+			}
+
+			return list.ToArray();
+		}
+
+		private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
+		{
+			bool allowed = true;
+
+			foreach (UnitEquipmentItem currItem in currItems)
+			{
+				if (ItemsAreMutuallyExclusive(currItem, item))
+				{
+					allowed = false;
+					break;
+				}
+			}
+
+			return allowed;
+		}
+
+		/// <summary>
+		/// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
+		/// </summary>
+		/// <param name="unit">The unit that wants to take the equipment item</param>
+		/// <param name="item">The item to check blocking items for</param>
+		/// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
+		public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
+		{
+			List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
+			UnitEquipmentItem[] currItems = unit.GetEquipment();
+
+			foreach (UnitEquipmentItem unitItem in unit.GetEquipment())
+			{
+				if (!ItemsAreMutuallyExclusive(unitItem, item))
+				{
+					items.Add(unitItem);
+				}
+			}
+
+			return items;
+		}
+
+		public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
+		{
+			return unit.UnitType.GetEquipmentItems();
+		}
+
+		public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
+		{
+			bool areMutex = false;
+			string[] item1mutex = item1.MutexGroups;
+			string[] item2mutex = item2.MutexGroups;
+
+			foreach (string mutex in item1mutex)
+			{
+				foreach (string otherMutex in item2mutex)
+				{
+					if (mutex.Equals(otherMutex))
+					{
+						areMutex = true;
+						goto postLoop;
+					}
+				}
+			}
+			postLoop:
+
+			return areMutex;
+		}
+	}
+}