Partial rewrite, missing:
- Correctly parsing incoming messages - Sending the gamestate after relevant actions
This commit is contained in:
80
Mau/Managers/TurnManager.cs
Normal file
80
Mau/Managers/TurnManager.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
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="nextPlayer">The player to change the turn to. Defaults to the next player in the current direction.</param>
|
||||
*/
|
||||
public void ChangeTurn(Player? nextPlayer = null)
|
||||
{
|
||||
var player = nextPlayer ?? GetNextPlayer();
|
||||
CurrentPlayer.State = PlayerState.WAIT;
|
||||
player.State = PlayerState.TURN;
|
||||
CurrentPlayer = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* <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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user