changeset 212:dce340f9cedc

Re #217: Rounding problem with equipment cost calculations * Temporary solution (which I thought I'd already committed yesterday) to make sure that rounding of amount doesn't round outside limit if limit and item rounding directions are different Re #220: Make percentage and numeric amounts more obvious * Deprecate some calculation and presentation methods and move them to UnitEquipmentUtils * Add method to return the number of equipment items taken rather than the amount taken
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Nov 2009 16:40:54 +0000
parents 1f39e325deee
children c6713a1b4c0d
files api/Objects/AbstractUnitEquipmentItemSelection.cs api/Objects/Unit.cs api/Objects/UnitEquipmentNumericSelection.cs api/Objects/UnitEquipmentRatioSelection.cs api/Util/UnitEquipmentUtil.cs
diffstat 5 files changed, 81 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sat Nov 21 11:50:48 2009 +0000
+++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sat Nov 21 16:40:54 2009 +0000
@@ -87,8 +87,9 @@
 		public abstract int NumberTaken
 		{
 			 get;
-		}
-
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public abstract string GetEquipmentAmountString();
 	}
 }
--- a/api/Objects/Unit.cs	Sat Nov 21 11:50:48 2009 +0000
+++ b/api/Objects/Unit.cs	Sat Nov 21 16:40:54 2009 +0000
@@ -248,67 +248,48 @@
 				}
 			}
 
-			return list.ToArray();
+			return list.ToArray();
+		}
+
+		internal AbstractUnitEquipmentItemSelection GetEquipmentSelection(UnitEquipmentItem item)
+		{
+			return DictionaryUtils.GetValue(equipment, item);
 		}
 
+		[Obsolete("Use UnitEquipmentUtil method instead")]
 		public double GetEquipmentAmount(UnitEquipmentItem item)
-		{
-			double amount = 0;
-			AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue(equipment, item);
-			
-			if (selection != null)
-			{
-				amount = selection.AmountTaken;
-			}
-			
-			return amount;
-		}
-
+		{
+			return UnitEquipmentUtil.GetEquipmentAmount(this, item);
+		}
+
+		[Obsolete("Use UnitEquipmentUtil method instead")]
 		public double GetEquipmentAmount(string equipID)
 		{
 			return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID));
-		}
-
+		}
+
+		[Obsolete("Use UnitEquipmentUtil method instead")]
 		public bool GetEquipmentAmountIsRatio(UnitEquipmentItem item)
-		{
-			return IsEquipmentAmountRatio(GetEquipmentSelection(item));
-		}
-
-		private AbstractUnitEquipmentItemSelection GetEquipmentSelection(UnitEquipmentItem item)
-		{
-			return DictionaryUtils.GetValue(equipment, item);
-		}
-
-		private bool IsEquipmentAmountRatio(AbstractUnitEquipmentItemSelection selection)
-		{
-			return (selection is UnitEquipmentRatioSelection);
-		}
-
+		{
+			return UnitEquipmentUtil.GetEquipmentAmountIsRatio(this, item);
+		}
+
+		[Obsolete("Use UnitEquipmentUtil method instead")]
 		public bool GetEquipmentAmountIsRatio(string itemID)
 		{
 			return GetEquipmentAmountIsRatio(UnitType.GetEquipmentItem(itemID));
 		}
 
+		[Obsolete("Implementation is down to the UI")]
 		public string GetEquipmentAmountString(string equipID)
 		{
-			return GetEquipmentAmountString(UnitType.GetEquipmentItem(equipID));
-		}
-
+			return "";
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public string GetEquipmentAmountString(UnitEquipmentItem item)
-		{
-			String amountString = "";
-			AbstractUnitEquipmentItemSelection selection = GetEquipmentSelection(item);
-				
-			if (IsEquipmentAmountRatio(selection))
-			{
-				amountString = UnitEquipmentRatioSelection.GetEquipmentAmountString(GetEquipmentAmount(item));
-			}
-			else
-			{
-				amountString = UnitEquipmentNumericSelection.GetEquipmentAmountString(GetEquipmentAmount(item));
-			}
-
-			return amountString;
+		{
+			return "";
 		}
 		
 		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
@@ -508,9 +489,9 @@
 
 		public int GetEquipmentAmountInSlot (string slotName)
 		{
-			int amount = 0;
-			
-			List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, slotName);
+			int amount = 0;
+
+			List<AbstractUnitEquipmentItemSelection> selections = GetEquipmentSlotSelections(slotName);
 			
 			if (selections != null)
 			{
@@ -518,6 +499,11 @@
 			}			
 			
 			return amount;
+		}
+
+		internal List<AbstractUnitEquipmentItemSelection> GetEquipmentSlotSelections(string slotName)
+		{
+			return DictionaryUtils.GetValue(equipmentSlots, slotName);
 		}
 
 		/// <summary>
--- a/api/Objects/UnitEquipmentNumericSelection.cs	Sat Nov 21 11:50:48 2009 +0000
+++ b/api/Objects/UnitEquipmentNumericSelection.cs	Sat Nov 21 16:40:54 2009 +0000
@@ -35,22 +35,15 @@
 		protected override bool IsValidValue (double newValue)
 		{
 			return base.IsValidValue(newValue) && IsWholeNumber(newValue);
-		}
-
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public override string GetEquipmentAmountString ()
 		{
 			return GetEquipmentAmountString(AmountTaken);
-		}
-
-		/// <summary>
-		/// Gets a string representing the amount of equipment taken when the amount is a numeric selection
-		/// </summary>
-		/// <param name="amount">
-		/// A <see cref="System.Double"/>
-		/// </param>
-		/// <returns>
-		/// A <see cref="System.String"/>
-		/// </returns>
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public static string GetEquipmentAmountString(double amount)
 		{
 			return amount.ToString();
--- a/api/Objects/UnitEquipmentRatioSelection.cs	Sat Nov 21 11:50:48 2009 +0000
+++ b/api/Objects/UnitEquipmentRatioSelection.cs	Sat Nov 21 16:40:54 2009 +0000
@@ -3,7 +3,9 @@
 // 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.CustomMath;
 using IBBoard.Limits;
+using IBBoard.WarFoundry.API.Util;
 
 namespace IBBoard.WarFoundry.API.Objects
 {	
@@ -25,16 +27,21 @@
 		{
 			get
 			{
-				double numberTaken = (AmountTaken / 100) * EquipmentForUnit.Size;
-				return (int) (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken));
+				double exactNumberTaken = (AmountTaken / 100) * EquipmentForUnit.Size;
+				int wholeNumberTaken = (int) IBBMath.Round(exactNumberTaken, EquipmentItem.RoundNumberUp);
+				int maxTaken = UnitEquipmentUtil.GetMaxEquipmentCount(EquipmentForUnit, EquipmentItem);
+				int minTaken = UnitEquipmentUtil.GetMinEquipmentCount(EquipmentForUnit, EquipmentItem);
+				return Math.Min(Math.Max(wholeNumberTaken, minTaken), maxTaken);
 			}
-		}
-
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public override string GetEquipmentAmountString ()
 		{
 			return GetEquipmentAmountString(AmountTaken);
-		}
-
+		}
+
+		[Obsolete("Implementation is down to the UI")]
 		public static string GetEquipmentAmountString(double amount)
 		{
 			string amountString;
--- a/api/Util/UnitEquipmentUtil.cs	Sat Nov 21 11:50:48 2009 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Sat Nov 21 16:40:54 2009 +0000
@@ -178,5 +178,29 @@
 		{
 			return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MinLimit, unit);
 		}
+
+		public static double GetEquipmentAmount(Unit unit, UnitEquipmentItem item)
+		{
+			double amount = 0;
+			AbstractUnitEquipmentItemSelection selection = unit.GetEquipmentSelection(item);
+
+			if (selection != null)
+			{
+				amount = selection.AmountTaken;
+			}
+
+			return amount;
+		}
+
+		public static bool GetEquipmentAmountIsRatio(Unit unit, UnitEquipmentItem item)
+		{
+			return (unit.GetEquipmentSelection(item) is UnitEquipmentRatioSelection);
+		}
+
+		public static int GetEquipmentAmountTaken(Unit unit, UnitEquipmentItem item)
+		{
+			AbstractUnitEquipmentItemSelection selection = unit.GetEquipmentSelection(item);
+			return (selection == null ? 0 : selection.NumberTaken);
+		}
 	}
 }