using System; using System.Collections.Generic; using System.Linq; namespace PingPong.GameEngine.Components { public class Team : IList, IScorable { #region Members /// /// The collection. /// private Lazy> collection; /// /// The score. /// private Lazy score; /// /// The type of the team rotation. /// private Lazy teamRotationType; #endregion #region Properties /// /// Gets the Collection. /// /// /// The collection. /// private List Collection { get { return this.collection.Value; } } /// /// Gets the type of the team rotation. /// /// /// The type of the team rotation. /// public TeamRotationType TeamRotationType { get { return this.teamRotationType.Value; } } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public Team() { this.collection = new Lazy>(() => new List()); this.score = new Lazy(() => new Score()); } #endregion #region Methods /// /// Gets the player. /// /// /// The player. /// /// /// Name. /// public Player GetPlayer(string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); } return this.Where(player => player.Name.Equals( name, StringComparison.OrdinalIgnoreCase ) ) .FirstOrDefault(); } /// /// Sets the type of the team rotation. /// /// /// The team rotation type. /// /// /// Team rotation type. /// public Team SetTeamRotationType(TeamRotationType teamRotationType) { this.teamRotationType = new Lazy(() => teamRotationType); return this; } /// /// Rotates the team. /// /// /// The team. /// public Team RotateTeam() { switch (this.TeamRotationType) { case TeamRotationType.RoundRobin: PerformRoundRobinRotation(); break; case TeamRotationType.Random: PerformRandomRotation(); break; case TeamRotationType.None: default: break; } return this; } /// /// Performs the round robin rotation. /// private void PerformRoundRobinRotation() { if (this.Collection.Any()) { var player = this.Collection.LastOrDefault(); var lastIndex = this.Collection.Count - 1; this.Collection.RemoveAt(lastIndex); this.Collection.Insert(0,player); } } /// /// Performs the random rotation. /// private void PerformRandomRotation() { if (this.Collection.Any()) { this.collection = new Lazy>(() => this.Collection.Select(player => new { GUID = new Guid(), Player = player }) .OrderBy(anon => anon.GUID) .Select(anon => anon.Player) .ToList() ); } } #endregion #region IEnumerable implementation /// /// Gets the enumerator. /// /// /// The enumerator. /// public System.Collections.IEnumerator GetEnumerator() { return this.Collection.GetEnumerator(); } #endregion #region IEnumerable implementation /// /// Gets the enumerator. /// /// /// The enumerator. /// IEnumerator IEnumerable.GetEnumerator() { return this.Collection.GetEnumerator(); } #endregion #region ICollection implementation /// /// The item to add to the current collection. /// /// /// Adds an item to the current collection. /// /// /// To be added. /// /// /// The current collection is read-only. /// /// /// Add the specified item. /// /// /// Item. /// public void Add(Player item) { this.Collection.Add(item); } /// /// Clear this instance. /// public void Clear() { this.Collection.Clear(); } /// /// The object to locate in the current collection. /// /// /// Determines whether the current collection contains a specific value. /// /// /// Contains the specified item. /// /// /// If set to true item. /// public bool Contains(Player item) { return this.Collection.Contains(item); } /// /// Copies to. /// /// /// Array. /// /// /// Array index. /// public void CopyTo(Player[] array, int arrayIndex) { this.Collection.CopyTo(array,arrayIndex); } /// /// The item to remove from the current collection. /// /// /// Removes the first occurrence of an item from the current collection. /// /// /// Remove the specified item. /// /// /// If set to true item. /// public bool Remove(Player item) { return this.Collection.Remove(item); } /// /// Gets the count. /// /// /// The count. /// public int Count { get { return this.Collection.Count; } } /// /// Gets a value indicating whether this instance is read only. /// /// /// true if this instance is read only; otherwise, false. /// public bool IsReadOnly { get { return false; } } #endregion #region IScorable implementation /// /// Gets the score. /// /// /// The score. /// public Score Score { get { return this.score.Value; } } /// /// Sets the score. /// /// /// The score. /// /// /// Score. /// public Team SetScore(Score score) { this.score = new Lazy(() => score); return this; } #endregion #region IList implementation /// /// To be added. /// /// /// Determines the index of a specific item in the current instance. /// /// /// Indexs the of. /// /// /// The of. /// /// /// Item. /// public int IndexOf(Player item) { return this.Collection.IndexOf(item); } /// /// Insert the specified index and item. /// /// /// Index. /// /// /// Item. /// public void Insert(int index, Player item) { this.Collection.Insert(index,item); } /// /// Removes at index. /// /// /// Index. /// public void RemoveAt(int index) { this.RemoveAt(index); } /// /// Gets or sets the at the specified index. /// /// /// Index. /// public Player this [int index] { get { return this.Collection[index]; } set { this.Collection[index] = value; } } #endregion } }