namespace MauMau_Server.Mau.Managers; public class TurnManager { private const int CLOCKWISE = 1; private const int COUNTER_CLOCKWISE = -1; private int _currentDirection { get; set; } = CLOCKWISE; public List Players; public Player CurrentPlayer; /** * * Initialize the turn manager with a list of players. * * The list of players to initialize the turn manager with. */ public void Initialize(IEnumerable players) { Players = players.OrderBy(x => Guid.NewGuid()).ToList(); ShufflePlayers(); CurrentPlayer = Players.First(); CurrentPlayer.State = PlayerState.TURN; } /** * * Change the direction of the turn. * If the direction is clockwise, it will change to counter-clockwise and vice versa. * */ public void ChangeDirection() { _currentDirection *= COUNTER_CLOCKWISE; } /** * * Change the turn to another player. * If no player is given, the next player in the current direction is chosen. * * The player to change the turn to. Defaults to the next player in the current direction. */ public void ChangeTurn(Player? nextPlayer = null) { var player = nextPlayer ?? GetNextPlayer(); CurrentPlayer.State = PlayerState.WAIT; player.State = PlayerState.TURN; CurrentPlayer = player; } /** * * Get a player that is a given amount of players away from the current player in the current direction. * * The amount of players to skip. Defaults to 1 */ public Player GetNextPlayer(int numberOfPlayers = 1) { var playerIndex = Players.IndexOf(CurrentPlayer); for (var i = 0; i < numberOfPlayers; i++) { playerIndex += _currentDirection; if (playerIndex >= Players.Count) playerIndex = 0; if (playerIndex < 0) playerIndex = Players.Count - 1; } return Players[playerIndex]; } /** * * Shuffle the list of players for a play random order. * */ private void ShufflePlayers() { Players = Players.OrderBy(x => Guid.NewGuid()).ToList(); } }