changeset 97:95746083d037

Fixes #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts * Add extra "NumericSelection" type so that ratio selections check the absolute value is within their ratio range * Update structure of checking for valid values * Move cost calculation for equipment selection in to abstract class and add abstract method to get "number of items taken" * Handle numeric selection and numeric selection for ratio differently in Unit
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Aug 2009 12:02:35 +0000
parents ced5a18d9f52
children 4dd1c41c95b4
files api/Objects/AbstractUnitEquipmentItemSelection.cs api/Objects/Unit.cs api/Objects/UnitEquipmentNumericForRatioSelection.cs api/Objects/UnitEquipmentNumericSelection.cs api/Objects/UnitEquipmentRatioSelection.cs
diffstat 5 files changed, 94 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 11:09:12 2009 +0000
+++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 12:02:35 2009 +0000
@@ -38,11 +38,6 @@
 			}
 		}
 		
-		public abstract double TotalCost
-		{
-			get;
-		}
-		
 		public double AmountTaken
 		{
 			get 
@@ -62,6 +57,24 @@
 			}
 		}
 		
-		protected abstract bool IsValidValue(double newValue);
+		protected bool IsValidValue(double newValue)
+		{
+			return IsInRange(newValue);
+		}
+		
+		protected abstract bool IsInRange(double newValue);
+		
+		public double TotalCost
+		{
+			get
+			{
+				return NumberTaken * EquipmentItem.Cost;
+			}
+		}
+		
+		public abstract int NumberTaken
+		{
+			 get;
+		}
 	}
 }
--- a/api/Objects/Unit.cs	Sun Aug 09 11:09:12 2009 +0000
+++ b/api/Objects/Unit.cs	Sun Aug 09 12:02:35 2009 +0000
@@ -288,7 +288,18 @@
 				{
 					if (oldAmount == 0)
 					{
-						equipment[equip] = new UnitEquipmentNumericSelection(this, equip, amount);
+						AbstractUnitEquipmentItemSelection newItem = null;
+						
+						if (equip.IsRatioLimit)
+						{
+							newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount);
+						}
+						else
+						{
+							newItem = new UnitEquipmentNumericSelection(this, equip, amount);
+						}
+						
+						equipment[equip] = newItem;
 					}
 					else
 					{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Objects/UnitEquipmentNumericForRatioSelection.cs	Sun Aug 09 12:02:35 2009 +0000
@@ -0,0 +1,33 @@
+// This file (UnitEquipmentNumericForRatioSelection.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 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 IBBoard.Lang;
+
+namespace IBBoard.WarFoundry.API.Objects
+{
+	/// <summary>
+	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number and the
+	/// equipment item has a ratio limit
+	/// </summary>
+	public class UnitEquipmentNumericForRatioSelection : UnitEquipmentNumericSelection
+	{
+		public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
+		{
+		}
+		
+		public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, IBBMath.Round(unit.Size * item.MinPercentage, item.RoundNumberUp))
+		{
+		}
+		
+		protected override bool IsInRange (double newValue)
+		{
+			int minLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MinPercentage, EquipmentItem.RoundNumberUp);
+			int maxLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MaxPercentage, EquipmentItem.RoundNumberUp);
+			bool isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
+			newValue = (newValue == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : newValue);
+			return isInRange && IsWholeNumber(newValue);
+		}
+	}
+}
--- a/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 11:09:12 2009 +0000
+++ b/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 12:02:35 2009 +0000
@@ -6,6 +6,9 @@
 
 namespace IBBoard.WarFoundry.API.Objects
 {	
+	/// <summary>
+	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number
+	/// </summary>
 	public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection
 	{	
 		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
@@ -15,23 +18,36 @@
 		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber)
 		{
 		}
-		
-		public override double TotalCost
+					
+		public override int NumberTaken
 		{
 			get
 			{
-				return CalculateAmount() * EquipmentItem.Cost;
+				return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken);
 			}
 		}
-					
-		private double CalculateAmount()
-		{
-			return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : AmountTaken);
-		}
 		
-		protected override bool IsValidValue (double newValue)
+		protected bool IsWholeNumber(double newValue)
 		{
 			return newValue == Math.Round(newValue);
 		}
+		
+		protected override bool IsInRange(double newValue)
+		{
+			bool isInRange = IsWholeNumber(newValue);
+			
+			if (newValue == WarFoundryCore.INFINITY)
+			{
+				isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY);
+			}
+			else if (isInRange)
+			{
+				int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber);
+				int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber);
+				isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
+			}
+			
+			return isInRange;
+		}
 	}
 }
--- a/api/Objects/UnitEquipmentRatioSelection.cs	Sun Aug 09 11:09:12 2009 +0000
+++ b/api/Objects/UnitEquipmentRatioSelection.cs	Sun Aug 09 12:02:35 2009 +0000
@@ -19,22 +19,17 @@
 		public UnitEquipmentRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinPercentage)
 		{
 		}
-		
-		public override double TotalCost
+					
+		public override int NumberTaken
 		{
 			get
 			{
-				return CalculateAmount() * EquipmentItem.Cost;
+				double numberTaken = AmountTaken * EquipmentForUnit.Size;
+				return (int) (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken));
 			}
 		}
-					
-		private double CalculateAmount()
-		{
-			double numberTaken = AmountTaken * EquipmentForUnit.Size;
-			return (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken));
-		}
 		
-		protected override bool IsValidValue (double newValue)
+		protected override bool IsInRange (double newValue)
 		{
 			return (EquipmentItem.MinPercentage <= newValue) && (newValue <= EquipmentItem.MaxPercentage);
 		}