view api/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 257:435eb28b4549

Re #270: Add multiple categories to API * Add section for multiple categories to schema * Add category loading to factory Also: * Do more sensible check on whether we need to set MainCategory when adding category to unit type
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 May 2010 19:59:17 +0000
parents 65553d2c8612
children 650bbe79b884
line wrap: on
line source

//  This file (AbstractSetUnitEquipmentAmountCommand.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 IBBoard.Commands;
using IBBoard.Lang;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;

namespace IBBoard.WarFoundry.API.Commands
{
	/// <summary>
	/// Abstract parent class for commands that set the amount of an equipment item a unit has to a fixed numeric or ratio value
	/// </summary>
	public abstract class AbstractSetUnitEquipmentAmountCommand : Command
	{
		private Unit unit;
		private UnitEquipmentItem equip;
		private double oldAmount;
		private bool oldAmountWasRatio;
		private string description;
		private string undoDescription;
		
		public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item)
		{
			this.unit = unit;
			equip = item;
			oldAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);
			oldAmountWasRatio = UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip);
		}

		public override bool CanExecute()
		{
			return (unit!=null && equip!=null);
		}

		public override string Description
		{
			get
			{
				if (description == null)
				{
					description = Translation.GetTranslation("setEquipmentAmountCommandDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetNewAmountString());
				}
				
				return description;
			}
		}

		/// <summary>
		/// Gets the string representation for the new amount of the equipment item to take
		/// </summary>
		/// <returns>
		/// the string representation for the new amount of the equipment item to take
		/// </returns>
		protected abstract string GetNewAmountString();

		public override string UndoDescription
		{
			get
			{
				if (undoDescription == null)
				{
					undoDescription = Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetOldAmountString());
				}
				
				return undoDescription;
			}
		}

		/// <summary>
		/// Gets the string representation for the old amount of the equipment item to take
		/// </summary>
		/// <returns>
		/// the string representation for the old amount of the equipment item to take
		/// </returns>
		protected string GetOldAmountString()
		{
			return oldAmountWasRatio ? GetRatioAmountString(oldAmount, UnitEquipmentRatioSelection.CalculateNumberTaken(Unit, EquipItem, oldAmount)) : GetNumberAmountString((int)oldAmount);
		}
		
		protected string GetNumberAmountString(int number)
		{
			return Translation.GetTranslation ("equipmentAmountNumber", "{0}", number);
		}

		protected string GetRatioAmountString (double amount, int number)
		{
			string amountString;
			
			if (amount == 100)
			{
				amountString = Translation.GetTranslation ("equipmentAmountAll", "all ({1})", amount, number);
			}
			else
			{
				amountString = Translation.GetTranslation ("equipmentAmountPercentage", "{0}% ({1})", amount, number);
			}
			
			return amountString;
		}

		public override bool Execute()
		{
			this.Redo();
			return true;
		}

		public override void Undo ()
		{
			if (oldAmountWasRatio)
			{
				unit.SetEquipmentRatio(equip, oldAmount);
			}
			else
			{
				unit.SetEquipmentAmount(equip, (int)oldAmount);
			}
		}


		public UnitEquipmentItem EquipItem
		{
			get { return equip; }
		}

		public Unit Unit
		{
			get { return unit; }
		}
	}
}