using DominionBase.Cards; using DominionBase.Enums; using DominionBase.Players; using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Xml; namespace DominionBase.Piles { public class SupplyCollection : Dictionary { public SupplyCollection() { } public SupplyCollection(IEnumerable> keyValuePairs) { Contract.Requires(keyValuePairs != null, "keyValuePairs cannot be null"); foreach (var kvp in keyValuePairs) { if (kvp.Value is ISupply) this[kvp.Key] = kvp.Value; } } public SupplyCollection(IEnumerable supplies) { foreach (var supply in supplies.OfType()) this[supply.TableableType] = supply; } public ISupply this[Card card] => (ISupply)this[card?.BaseType]; internal void AddPlayer(Player player) { var supplies = new List(Values); foreach (var supply in supplies) supply.AddPlayer(player); } internal void RemovePlayer(Player player) { var supplies = new List(Values); foreach (var supply in supplies) supply.RemovePlayer(player); } public int EmptySupplyPiles { get { return Values.Count(s => s.Count == 0 && (s.Location == Location.General || s.Location == Location.Kingdom)); } } public void Reset() { foreach (var supply in Values) supply.Reset(); } internal void Setup() { var supplies = new List(Values); foreach (var supply in supplies) supply.Setup(); foreach (var supply in supplies) supply.SnapshotSetup(); } internal void FinalizeSetup() { var supplies = new List(Values); foreach (var supply in supplies) supply.FinalizeSetup(); } public SupplyCollection FindAll(Func predicate) { var supplies = new SupplyCollection(); foreach (var type in Keys.Where(k => predicate(this[k]))) supplies[type] = this[type]; return supplies; } public SupplyCollection FindAll(Func predicate) { var supplies = new SupplyCollection(); foreach (var supply in Values.OfType().Where(predicate)) supplies[supply.TableableType] = supply; return supplies; } public bool ContainsKey(Card card) { Contract.Requires(card != null, "card cannot be null"); return ContainsKey(card.BaseType); } internal void TearDown(IGame game) { foreach (var supply in Values) supply.TearDown(game); } internal XmlNode GenerateXml(XmlDocument doc, string nodeName) { var xeSupplies = doc.CreateElement(nodeName); foreach (var supply in this) xeSupplies.AppendChild(supply.Value.GenerateXml(doc)); return xeSupplies; } internal void Load(Game game, XmlNode xnSupplies) { if (xnSupplies == null) return; foreach (XmlNode xnSupply in xnSupplies.SelectNodes("supply")) { var supply = Supply.Load(game, xnSupply); Add(supply.Type, supply); } } } }