using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Model.Dice;
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 DateTime When { get; private set; }
///
/// the Player who rolled the dice
///
public Player Player { get; private set; }
///
/// the collection of Face that were rolled
///
public IEnumerable, AbstractDieFace>> DiceNFaces => diceNFaces.AsEnumerable();
private readonly Dictionary, AbstractDieFace> diceNFaces;
///
/// 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, Dictionary, AbstractDieFace> diceNFaces)
{
When = when;
Player = player;
this.diceNFaces = diceNFaces;
}
///
/// 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, Dictionary, AbstractDieFace> diceNFaces)
{
if (player is null)
{
throw new ArgumentNullException(nameof(player), "param should not be null");
}
if (diceNFaces is null)
{
throw new ArgumentNullException(nameof(diceNFaces), "param should not be null");
}
if (diceNFaces.Count == 0)
{
throw new ArgumentException("param should not be null", nameof(diceNFaces));
}
if (when.Kind != DateTimeKind.Utc)
{
when = when.ToUniversalTime();
}
return new Turn(when, player, diceNFaces);
}
///
/// 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, Dictionary, AbstractDieFace> diceNFaces)
{
return CreateWithSpecifiedTime(DateTime.UtcNow, player, diceNFaces);
}
///
/// 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 this.diceNFaces.Values)
{
sb.Append(" " + face.ToString());
}
return sb.ToString();
}
public bool Equals(Turn other)
{
return Player.Equals(other.Player)
&& When.Equals(other.When)
&& DiceNFaces.SequenceEqual(other.DiceNFaces);
}
public override bool Equals(object obj)
{
if (obj is not Turn)
{
return false;
}
return Equals(obj as Turn);
}
public override int GetHashCode()
{
return HashCode.Combine(Player, When, DiceNFaces);
}
}
}