using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using Model.Dice.Faces; using Model.Players; namespace Model.Games { /// /// a Turn consists of a Player, a DateTime, and a IEnumerable of AbstractDieFace /// Like a turn in some game. ///
/// Two turns are equal if they are litterally the same instance in RAM /// (default behaviors Equals() and GetHashCode()) ///
public class Turn { /// /// the date and time, adjusted to UTC /// public readonly DateTime when; /// /// the Player who rolled the dice /// public readonly Player player; /// /// the collection of Face that were rolled /// private readonly IEnumerable faces; /// /// this private constructor is to be used only by factories /// /// date and time of the turn /// player who played the turn /// faces that were rolled private Turn(DateTime when, Player player, IEnumerable faces) { this.when = when; this.player = player; this.faces = faces; } /// /// creates a Turn with a specified time, passed as a parameter. ///
/// whatever the DateTimeKind of might be, /// it will become UTC during construction ///
/// date and time of the turn /// player who played the turn /// faces that were rolled /// a new Turn object public static Turn CreateWithSpecifiedTime(DateTime when, Player player, IEnumerable faces) { if (faces is null || !faces.Any()) { throw new ArgumentException("param should not be null or empty", nameof(faces)); } if (player is null) { throw new ArgumentNullException(nameof(player), "param should not be null"); } if (when.Kind != DateTimeKind.Utc) { when = when.ToUniversalTime(); } return new Turn(when, player, faces); } /// /// creates a Turn with a default time, which is "now" in UTC. /// /// player who played the turn /// faces that were rolled /// a new Turn object public static Turn CreateWithDefaultTime(Player player, IEnumerable faces) { return CreateWithSpecifiedTime(DateTime.UtcNow, player, faces); } /// /// represents a turn in string format /// /// a turn in string format public override string ToString() { string[] datetime = when.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T"); string date = datetime[0]; string time = datetime[1]; StringBuilder sb = new(); sb.AppendFormat("{0} {1} -- {2} rolled:", date, time, player.ToString()); foreach (AbstractDieFace face in faces) { sb.Append(" " + face.ToString()); } return sb.ToString(); } } }