/
etc/
lib/
src/Abilities/
src/Abilities/Skills/
src/Abilities/Spells/
src/Abilities/Spells/Enums/
src/Affects/
src/ArtheaConsole/
src/ArtheaConsole/Properties/
src/ArtheaGUI/Properties/
src/Clans/Enums/
src/Commands/Communication/
src/Commands/ItemCommands/
src/Connections/
src/Connections/Colors/
src/Connections/Enums/
src/Connections/Players/
src/Connections/Players/Enums/
src/Continents/
src/Continents/Areas/
src/Continents/Areas/Characters/
src/Continents/Areas/Characters/Enums/
src/Continents/Areas/Items/
src/Continents/Areas/Items/Enums/
src/Continents/Areas/Rooms/
src/Continents/Areas/Rooms/Enums/
src/Continents/Areas/Rooms/Exits/
src/Creation/
src/Creation/Attributes/
src/Creation/Interfaces/
src/Database/
src/Database/Interfaces/
src/Environment/
src/Properties/
src/Scripts/Enums/
src/Scripts/Interfaces/
#region Arthea License

/***********************************************************************
*  Arthea MUD by R. Jennings (2007)      http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses. *
***********************************************************************/

#endregion

using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Arthea.Connections.Players;
using Arthea.Creation;
using Arthea.Environment;
using Arthea.Interfaces;

namespace Arthea.Abilities.Skills
{
    /// <summary>
    /// Implementation of a skill level.
    /// </summary>
    public class SkillValues : Dictionary<string, ushort>, IXmlSerializable, CustomEditType
    {
        #region [rgn] Properties (1)

        /// <summary>
        /// Gets the <see cref="System.Int16"/> with the specified by skill name.
        /// </summary>
        /// <value>the skill value</value>
        public new ushort this[string skill]
        {
            get
            {
                try
                {
                    return base[skill];
                }
                catch
                {
                    return 0;
                }
            }
            set
            {
                if (value == 0)
                {
                    Remove(skill);
                }
                else if (ContainsKey(skill))
                {
                    base[skill] = value;
                }
                else
                {
                    Add(skill, value);
                }
            }
        }

        #endregion [rgn]

        #region CustomEditType Members

        /// <summary>
        /// Sets the value of a skill level.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="editer">Information from the editer.</param>
        /// <param name="argument">The argument.</param>
        public void CustomEdit(Player player, OlcField editer, String argument)
        {
            String arg = argument.FirstArg();

            Ability skill = Lists.Abilities.FindName(arg);

            ushort level;

            if (!ushort.TryParse(argument, out level))
            {
                player.WriteLine("That is not a valid level for a skill.");
                return;
            }

            this[skill.Name] = level;

            player.WriteLine("{0} set to {1}.", skill.Name, level);
        }

        #endregion

        #region IXmlSerializable Members

        ///<summary>
        ///This property is reserved, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"></see> to the class instead. 
        ///</summary>
        ///
        ///<returns>
        ///An <see cref="T:System.Xml.Schema.XmlSchema"></see> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"></see> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"></see> method.
        ///</returns>
        ///
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }

        ///<summary>
        ///Generates an object from its XML representation.
        ///</summary>
        ///
        ///<param name="reader">The <see cref="T:System.Xml.XmlReader"></see> stream from which the object is deserialized. </param>
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (reader.IsEmptyElement)
                return;

            reader.ReadStartElement();

            while (reader.IsStartElement())
            {
                Add(reader.GetAttribute("name"),
                    ushort.Parse(reader.GetAttribute("value")));

                reader.ReadStartElement();
            }

            reader.ReadEndElement();
        }

        ///<summary>
        ///Converts an object into its XML representation.
        ///</summary>
        ///
        ///<param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> stream to which the object is serialized. </param>
        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (Count == 0)
                return;

            foreach (KeyValuePair<string, ushort> entry in this)
            {
                writer.WriteStartElement("Skill");
                writer.WriteAttributeString("name", entry.Key);
                writer.WriteAttributeString("value", entry.Value.ToString());
                writer.WriteEndElement();
            }
        }

        #endregion
    }
}