using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Xml; using DominionBase.Players; namespace DominionBase { // Generic class is any ITableable public class TableEntityCollection : TableEntityCollection { } // Allow more specific constraints public class TableEntityCollection : Dictionary where T : ITableable { public TableEntityCollection() { } public TableEntityCollection(IEnumerable> keyValuePairs) { Contract.Requires(keyValuePairs != null, "keyValuePairs cannot be null"); foreach (var kvp in keyValuePairs) this[kvp.Key] = kvp.Value; } public T this[ICardBase card] { get { Contract.Requires(card != null, "card cannot be null"); if (ContainsKey(card.BaseType)) return this[card.BaseType]; foreach (var tableItem in Values) if (tableItem.Types.Contains(card.BaseType)) return tableItem; throw new KeyNotFoundException(); } } internal void AddPlayer(Player player) { var tableEntities = new List(Values); foreach (var tableEntity in tableEntities) tableEntity.AddPlayer(player); } internal void RemovePlayer(Player player) { var tableEntities = new List(Values); foreach (var tableEntity in tableEntities) tableEntity.RemovePlayer(player); } public void Reset() { foreach (var tableEntity in Values) tableEntity.Reset(); } internal void Setup() { var tableEntities = new List(Values); foreach (var tableEntity in tableEntities) tableEntity.Setup(); foreach (var tableEntity in tableEntities) tableEntity.SnapshotSetup(); } internal void FinalizeSetup(IGame game) { var tableEntities = new List(Values); foreach (var tableEntity in tableEntities) tableEntity.FinalizeSetup(); } public TableEntityCollection FindAll(Func predicate) { var tableEntities = new TableEntityCollection(); foreach (var tableEntity in Values.Where(predicate)) tableEntities[tableEntity.TableableType] = tableEntity; return tableEntities; } public TableEntityCollection FindAll(Func predicate) where TF : T { var tableEntities = new TableEntityCollection(); foreach (var tableEntity in Values.OfType().Where(predicate)) tableEntities[tableEntity.TableableType] = tableEntity; return tableEntities; } public bool ContainsKey(ICardBase card) { Contract.Requires(card != null, "card cannot be null"); return ContainsKey(card.BaseType); } public bool ContainsCardType(ICardBase card) { Contract.Requires(card != null, "card cannot be null"); return ContainsKey(card.BaseType) || Values.Any(tableItem => tableItem.Types.Contains(card.BaseType)); } internal void TearDown(IGame game) { foreach (var tableEntity in Values) tableEntity.TearDown(game); } internal XmlNode GenerateXml(XmlDocument doc, string nodeName) { var xeTableEntities = doc.CreateElement(nodeName); foreach (var tableEntity in this) xeTableEntities.AppendChild(tableEntity.Value.GenerateXml(doc)); return xeTableEntities; } internal void Load(IGame game, XmlNode xnTableEntities) { if (xnTableEntities == null) return; // This needs to be implemented to be able to load each TableEntity (like cards) //foreach (XmlNode xnTableEntity in xnTableEntities.SelectNodes("supply")) //{ // Type entityType = Type.GetType(xnTableEntity.Attributes["type"].Value); // (ITableable)Activator.CreateInstance(entityType); // ITableable tableEntity = Supply.Load(game, xnTableEntity); // this.Add(tableEntity.Type, tableEntity); //} } } }