diff api/Objects/Category.cs @ 12:ac232763858b

Re #9 - Make WarFoundry API use smaller methods * Obselete old Category and UnitType constructors and add minimal constructors * Add setters to properties where needed * Add value sanity checking to Category and UnitType * Set default values for percentages, choices and points for Category * Make XML factory use new constructor and setter properties * Add DuplicateItemException to project file
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 20:52:29 +0000
parents 613bc5eaac59
children 306558904c2a
line wrap: on
line diff
--- a/api/Objects/Category.cs	Sun Jan 18 16:24:03 2009 +0000
+++ b/api/Objects/Category.cs	Sun Jan 18 20:52:29 2009 +0000
@@ -1,5 +1,6 @@
 using System;
 using System.Xml;
+using IBBoard.Logging;
 
 namespace IBBoard.WarFoundry.API.Objects
 {
@@ -8,18 +9,31 @@
 	/// </summary>
 	public class Category : WarFoundryObject
 	{
-		private int minPts, maxPts, minPc, maxPc, minChoice, maxChoice, baseVal, incVal, incAmount;
-		/*private GameSystem system;*/
-		
+		private int minPts = 0;
+		private int maxPts = -1;
+		private int minPc = 0;
+		private int maxPc = 100;
+		private int minChoice = 0;
+		private int maxChoice = -1;
+		private int baseVal = 0;
+		private int incVal = 0;
+		private int incAmount = 0;
+
+		
+		public Category(string id, string name) : base(id, name)
+		{
+		}
+
+		[Obsolete("Use the two argument constructor and the appropriate 'set' methods")]
 		public Category(string id, string name, int minPoints, int maxPoints, int minPercent, int maxPercent, int minChoices, int maxChoices, int baseValue, int incrementValue, int incrementAmount) : base(id, name)
 		{
-			minPts = minPoints;
-			maxPts = maxPoints;
-			minPc = minPercent;
-			maxPc = maxPercent;
-			baseVal = baseValue;
-			incVal = incrementValue;
-			incAmount = incrementAmount;
+			MinimumPoints = minPoints;
+			MaximumPoints = maxPoints;
+			MinimumPercentage = minPercent;
+			MaximumPercentage = maxPercent;
+			BaseValue = baseValue;
+			IncrementValue = incrementValue;
+			IncrementAmount = incrementAmount;
 		}
 
 		protected override string DefaultName()
@@ -30,55 +44,118 @@
 		public int MinimumPoints
 		{
 			get { return minPts; }
-			set { minPts = value; }
+			set
+			{
+				minPts = (value >= 0 ? value : 0);
+				CheckMinimumPoints();
+			}
 		}
 
 		public int MaximumPoints
 		{
 			get { return maxPts; }
-			set { maxPts = value; }
+			set
+			{
+				maxPts = (value >= -1 ? value : -1);
+				CheckMinimumPoints();
+			}
+		}
+
+		private void CheckMinimumPoints()
+		{
+			if (MinimumPoints > MaximumPoints && MaximumPoints!=-1)
+			{
+				MinimumPoints = MaximumPoints;
+				LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum points limit greater than its maximum points limit.", Name, ID);
+			}
 		}
 		
 		public int MinimumPercentage
 		{
 			get { return minPc; }
-			set { minPc = value; }
+			set
+			{
+				minPc = (value >= 0 ? value : 0);
+				CheckMinimumPercentage();
+			}
 		}
 
 		public int MaximumPercentage
 		{
 			get { return maxPc; }
-			set { maxPc = value; }
+			set
+			{
+				if (value < 0)
+				{
+					maxPc = 0;
+				}
+				else if (value > 100)
+				{
+					maxPc = 100;
+				}
+				else
+				{
+					maxPc = value;
+				}
+				
+				CheckMinimumPercentage();
+			}
+		}
+
+		private void CheckMinimumPercentage()
+		{
+			if (MinimumPercentage > MaximumPercentage)
+			{
+				MinimumPercentage = MaximumPercentage;
+				LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum percentage limit greater than its maximum percentage limit.", Name, ID);
+			}
 		}
 
 		public int MinimumChoices
 		{
 			get { return minChoice; }
-			set { minChoice = value; }
+			set
+			{
+				minChoice = (value >= 0 ? value : 0);
+				CheckMinimumChoices();
+			}
 		}
 
 		public int MaximumChoices
 		{
 			get { return maxChoice; }
-			set { maxChoice = value; }
+			set
+			{
+				maxChoice = (value >= -1 ? value : -1);
+				CheckMinimumChoices();
+			}
+		}
+
+		private void CheckMinimumChoices()
+		{
+			if (MinimumPercentage > MaximumPercentage && MaximumPercentage!=-1)
+			{
+				MinimumPercentage = MaximumPercentage;
+				LogNotifier.WarnFormat(GetType(), "Category {0} ({1}) had a minimum number of choices greater than its maximum number of choices.", Name, ID);
+			}
 		}
 
 		public int BaseValue
 		{
 			get { return baseVal; }
-			set { baseVal = value; }
+			set { baseVal = (value >= 0 ? value : 0); }
 		}
 
 		public int IncrementValue
 		{
 			get { return incVal; }
-			set { incVal = value; }
+			set { incVal = (value >= 0 ? value : 0); }
 		}
 
 		public int IncrementAmount
 		{
 			get { return incAmount; }
-			set { incAmount = value; }
+			set { incAmount = (value >= 0 ? value : 0); }
 		}
 	}
 }