public Pouch Pay(int valueInCopper) { if (TotalValue < valueInCopper) { return(null); } Pouch result = new Pouch(); int tmp; for (int i = 3; i >= 0; --i) { tmp = Math.Min(valueInCopper / (int)Math.Pow(10, i), _coins[i].Ammount); valueInCopper -= tmp * (int)Math.Pow(10, i); _coins[i].Ammount -= tmp; result._coins[i].Ammount += tmp; } if (valueInCopper > 0) { throw new Exception("To nie powinno sie wysypac. Sprawdz Pouch.Pay()"); } return(result); }
public void ReadXml(XmlReader reader) { XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.ReadNode(reader)); XmlElement root = doc.DocumentElement; Name = root.GetAttribute("Name"); PlayerName = root.GetAttribute("PlayerName"); int tmp; CreatureSize cstmp; XmlElement xelm = (XmlElement)root.SelectSingleNode("Class"); string stmp = xelm?.InnerText; if (stmp != null) { foreach (HeroClass hc in HeroClass.AllClasses) { if (stmp == hc.Name) { Class = hc; if (int.TryParse(xelm.GetAttribute("Level"), out tmp)) { ClassLevel = tmp; } break; } } } xelm = (XmlElement)root.SelectSingleNode("SecondaryClass"); stmp = xelm?.InnerText; if (stmp != null) { foreach (HeroClass hc in HeroClass.AllClasses) { if (stmp == hc.Name) { SecondaryClass = hc; if (int.TryParse(xelm.GetAttribute("Level"), out tmp)) { SecondaryClassLevel = tmp; } break; } } } stmp = root.SelectSingleNode("Race")?.InnerText; if (stmp != null) { foreach (Race r in Race.AllRaces) { if (stmp == r.Name) { Race = r; break; } } } if (int.TryParse(root.SelectSingleNode("Speed")?.InnerText, out tmp)) { Speed = tmp; } if (Enum.TryParse(root.SelectSingleNode("Size")?.InnerText, out cstmp)) { Size = cstmp; } if (int.TryParse(root.SelectSingleNode("MaxHealthPoints")?.InnerText, out tmp)) { MaxHealthPoints = tmp; } if (int.TryParse(root.SelectSingleNode("CurrentHealthPoints")?.InnerText, out tmp)) { CurrentHealthPoints = tmp; } { XmlSerializer sss; Pouch pppc = null; List <Item> lit = new List <Item>(); XmlElement el; XmlElement inv = (XmlElement)root.SelectSingleNode("Inventory"); sss = new XmlSerializer(typeof(Pouch)); el = (XmlElement)inv.SelectSingleNode("Pouch"); if (el != null) { pppc = (Pouch)sss.Deserialize(new XmlNodeReader(el)); } XmlElement bag = (XmlElement)inv.SelectSingleNode("Bag"); if (bag != null) { sss = new XmlSerializer(typeof(Item)); foreach (XmlElement it in bag.SelectNodes("Item")) { lit.Add((Item)sss.Deserialize(new XmlNodeReader(it))); } sss = new XmlSerializer(typeof(Weapon)); foreach (XmlElement it in bag.SelectNodes("Weapon")) { lit.Add((Item)sss.Deserialize(new XmlNodeReader(it))); } sss = new XmlSerializer(typeof(Armor)); foreach (XmlElement it in bag.SelectNodes("Armor")) { lit.Add((Item)sss.Deserialize(new XmlNodeReader(it))); } } Inventory = new Inventory(lit, pppc); Inventory.Name = inv.GetAttribute("Name"); el = (XmlElement)inv.SelectSingleNode("MainWeapon")?.SelectSingleNode("Weapon"); sss = new XmlSerializer(typeof(Weapon)); if (el != null) { MainWeapon = (Weapon)sss.Deserialize(new XmlNodeReader(el)); } el = (XmlElement)inv.SelectSingleNode("MainArmor")?.SelectSingleNode("Armor"); sss = new XmlSerializer(typeof(Armor)); if (el != null) { MainArmor = (Armor)sss.Deserialize(new XmlNodeReader(el)); } el = (XmlElement)inv.SelectSingleNode("MainShield")?.SelectSingleNode("Armor"); if (el != null) { MainShield = (Armor)sss.Deserialize(new XmlNodeReader(el)); } } xelm = (XmlElement)root.SelectSingleNode("Attributes"); if (xelm != null) { foreach (XmlElement el in xelm) { HeroAttribute.Type ttt; if (Enum.TryParse(el.Name, out ttt) && int.TryParse(el.GetAttribute("Value"), out tmp)) { _atr[(int)ttt] = tmp; } } } xelm = (XmlElement)root.SelectSingleNode("SkillMarks"); if (xelm != null) { foreach (XmlElement el in xelm) { Skill.Type ttt; if (Enum.TryParse(el.Name, out ttt) && int.TryParse(el.GetAttribute("Value"), out tmp)) { _marks[(int)ttt] = tmp; } } } if (int.TryParse(root.SelectSingleNode("Age")?.InnerText, out tmp)) { Age = tmp; } if (int.TryParse(root.SelectSingleNode("Height")?.InnerText, out tmp)) { Height = tmp; } if (int.TryParse(root.SelectSingleNode("Weight")?.InnerText, out tmp)) { Weight = tmp; } Sex = root.SelectSingleNode("Sex")?.InnerText; EyesColor = root.SelectSingleNode("EyesColor")?.InnerText; HairColor = root.SelectSingleNode("HairColor")?.InnerText; }