#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;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Arthea.Connections.Players.Enums;
using Arthea.Creation;
using Arthea.Interfaces;
namespace Arthea.Connections.Players
{
/// <summary>
/// Implementation of player stats.
/// </summary>
public class PlayerStats : IXmlSerializable, CustomEditType
{
#region [rgn] Fields (1)
private short[] stats;
#endregion [rgn]
#region [rgn] Constructors (1)
/// <summary>
/// Initializes a new instance of the <see cref="PlayerStats"/> class.
/// </summary>
public PlayerStats()
{
stats = new short[Enum.GetValues(typeof (Stat)).Length];
}
#endregion [rgn]
#region [rgn] Properties (3)
/// <summary>
/// Gets or sets the stats.
/// </summary>
/// <value>The stats.</value>
public short[] Stats
{
get { return stats; }
set { stats = value; }
}
/// <summary>
/// Gets or sets the specified stat value.
/// </summary>
/// <value>a stat type</value>
public short this[Stat type]
{
get { return stats[(int) type]; }
set { stats[(int) type] = value; }
}
/// <summary>
/// Gets or sets the <see cref="System.Int16"/> at the specified index.
/// </summary>
/// <value>a short integer</value>
public short this[int index]
{
get { return stats[index]; }
set { stats[index] = value; }
}
#endregion [rgn]
#region [rgn] Methods (3)
// [rgn] Public Methods (3)
/// <summary>
/// Sets the value.
/// </summary>
/// <param name="player">The player.</param>
/// <param name="editer">Editer information.</param>
/// <param name="argument">The argument.</param>
public void CustomEdit(Player player, OlcField editer, String argument)
{
String arg = argument.FirstArg();
Stat stat;
try
{
stat = (Stat) Enum.Parse(typeof (Stat), arg.ToString());
}
catch
{
player.WriteLine("Valid stats are:");
Columns.Show(player, 4, 10, Enum.GetNames(typeof (Stat)));
return;
}
short value;
if (!short.TryParse(argument, out value))
{
player.WriteLine("That is not a valid value.");
return;
}
this[stat] = value;
}
/// <summary>
/// Rolls the stats.
/// </summary>
public void Roll()
{
for (int length = Enum.GetValues(typeof (Stat)).Length, i = 0; i < length; i++)
{
stats[i] = (short) Util.Dice(2, 6);
}
}
/// <summary>
/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
/// </returns>
public override string ToString()
{
string[] names = Enum.GetNames(typeof (Stat));
StringBuilder buf = new StringBuilder();
for (int i = 0; i < names.Length; i++)
{
buf.AppendFormat("{0}: {1} ", names[i].Substring(0, 3), stats[i]);
}
return buf.ToString().TrimEnd();
}
#endregion [rgn]
#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>
///
public XmlSchema 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>
public void ReadXml(XmlReader reader)
{
if (reader.IsEmptyElement)
return;
reader.ReadStartElement();
while (reader.IsStartElement())
{
int stat;
try
{
stat = (int) Enum.Parse(typeof (Stat), reader.GetAttribute("name"));
}
catch
{
Log.Error("Error parsing stat name.");
throw;
}
short value;
try
{
value = short.Parse(reader.GetAttribute("value"));
}
catch
{
Log.Error("Error parsing stat value.");
throw;
}
stats[stat] = 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>
public void WriteXml(XmlWriter writer)
{
for (int i = 0; i < stats.Length; i++)
{
writer.WriteStartElement("Stat");
writer.WriteAttributeString("name", Enum.GetName(typeof (Stat), i));
writer.WriteAttributeString("value", stats[i].ToString());
writer.WriteEndElement();
}
}
#endregion
}
}