Files
MauMau-Server/Mau/Managers/TurnManager.cs
2024-05-18 14:16:30 +02:00

80 lines
2.4 KiB
C#

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<Player> Players;
public Player CurrentPlayer;
/**
* <summary>
* Initialize the turn manager with a list of players.
* </summary>
* <param name="players">The list of players to initialize the turn manager with.</param>
*/
public void Initialize(IEnumerable<Player> players)
{
Players = players.OrderBy(x => Guid.NewGuid()).ToList();
ShufflePlayers();
CurrentPlayer = Players.First();
CurrentPlayer.State = PlayerState.TURN;
}
/**
* <summary>
* Change the direction of the turn.
* If the direction is clockwise, it will change to counter-clockwise and vice versa.
* </summary>
*/
public void ChangeDirection()
{
_currentDirection *= COUNTER_CLOCKWISE;
}
/**
* <summary>
* Change the turn to another player.
* If no player is given, the next player in the current direction is chosen.
* </summary>
* <param name="playerayer">The player to change the turn to. Defaults to the next player in the current direction.</param>
*/
public void ChangeTurnTo(Player? player = null, PlayerState nextPlayerState = PlayerState.TURN)
{
var nextPlayer = player ?? GetNextPlayer();
CurrentPlayer.State = PlayerState.WAIT;
nextPlayer.State = nextPlayerState;
CurrentPlayer = nextPlayer;
}
/**
* <summary>
* Get a player that is a given amount of players away from the current player in the current direction.
* </summary>
* <param name="numberOfPlayers">The amount of players to skip. Defaults to 1</param>
*/
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];
}
/**
* <summary>
* Shuffle the list of players for a play random order.
* </summary>
*/
private void ShufflePlayers()
{
Players = Players.OrderBy(x => Guid.NewGuid()).ToList();
}
}