view api/Objects/Stats.cs @ 181:8c6f55d289b0

Fixes #200: Stats names should be case-insensitive * ToLower() all stat names/IDs so that they are case-insensitive
author IBBoard <dev@ibboard.co.uk>
date Sat, 24 Oct 2009 15:18:00 +0000
parents 5145b7c61ae0
children ece26f6a62f3
line wrap: on
line source

// This file (Stats.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 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 System.Collections.Generic;
using IBBoard.Lang;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Stats defines the statistic/attribute values for an entity (for example a unit or any of their equipment that has a stat line) paired against a <see cref=" SystemStats"/> stat line definition.
	/// </summary>
	public class Stats
	{
		private List<Stat> stats;
		private SystemStats sysStats;
		
		public Stats(SystemStats systemStats)
		{
			sysStats = systemStats;
			int statCount = sysStats.SlotCount;
			stats = new List<Stat>(statCount);

			for (int i = 0; i < statCount; i++)
			{
				stats.Add(null);
			}
		}
		
		public Stat[] StatsArray
		{
			get { return stats.ToArray(); }
		}

		public void SetStatValue(string statName, string statValue)
		{
			StatSlot slot = sysStats[statName.ToLower()];

			if (slot!=null)
			{
				int pos = sysStats.GetStatSlotPosition(slot);

				if (pos > -1)
				{
					stats[pos] = new Stat(slot, statValue);
				}
			}				
		}
		
		public Stat this[string id]
		{
			get
			{
				StatSlot slot = sysStats[id.ToLower()];
				int pos = sysStats.GetStatSlotPosition(slot);
				Stat stat = null;
				
				try
				{
					stat = this[pos];
				}
				catch (ArgumentException ex)
				{
					throw new ArgumentException(Translation.GetTranslation("InvalidStatPos", "Invalid statistic ID {0} for stats based on system stats set {1}", new object[]{id, sysStats.ID}), ex);
				}
				
				return stat;
			}
		}
		
		public Stat this[int pos]
		{
			get
			{
				if (pos < stats.Count && pos >= 0)
				{
					return stats[pos];
				}
				else
				{
					throw new ArgumentException(Translation.GetTranslation("InvalidStatPos", "Invalid statistic position {0} for stats based on system stats set {1}", new object[]{pos, sysStats.ID})); 
				}
			}
		}

		public string GetStatValue(string id)
		{
			return this[id.ToLower()].SlotValueString;
		}
		
		public int StatCount
		{
			get { return stats.Count; }
		}
		
		public string StatsID
		{
			get
			{
				return sysStats.ID;
			}
		}
	}
}