view api/Objects/Category.cs @ 38:548cfc776f54

Fixes #34 - Remove "Choices" and "Base/Increment" from Category * Remove Choices and Base/Increment from code Re #47: Remove magic numbers * Replace "-1" magic number with WarFoundryCore.INFINITY * Use INFINITY instead of -1 in code * Use INF in schemas instead of -1 * Handle and parse INF as a special value in XML Factory
author IBBoard <dev@ibboard.co.uk>
date Thu, 19 Mar 2009 20:11:07 +0000
parents 306558904c2a
children a5855fcd75ab
line wrap: on
line source

// This file (Category.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 System.Xml;
using IBBoard.Logging;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for Category.
	/// </summary>
	public class Category : WarFoundryObject
	{
		private int minPts = 0;
		private int maxPts = WarFoundryCore.INFINITY;
		private int minPc = 0;
		private int maxPc = 100;

		
		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)
		{
			MinimumPoints = minPoints;
			MaximumPoints = maxPoints;
			MinimumPercentage = minPercent;
			MaximumPercentage = maxPercent;
		}

		protected override string DefaultName()
		{
			return "";
		}

		public int MinimumPoints
		{
			get { return minPts; }
			set
			{
				minPts = (value >= 0 ? value : 0);
				CheckMinimumPoints();
			}
		}

		public int MaximumPoints
		{
			get { return maxPts; }
			set
			{
				maxPts = (value >= 0 ? value : WarFoundryCore.INFINITY);
				CheckMinimumPoints();
			}
		}

		private void CheckMinimumPoints()
		{
			if (MinimumPoints > MaximumPoints && MaximumPoints!=WarFoundryCore.INFINITY)
			{
				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 >= 0 ? value : 0);
				CheckMinimumPercentage();
			}
		}

		public int MaximumPercentage
		{
			get { return maxPc; }
			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);
			}
		}
	}
}