diff --git a/pac-man-board-game/Controllers/GameController.cs b/pac-man-board-game/Controllers/GameController.cs index b63e52d..ac5186f 100644 --- a/pac-man-board-game/Controllers/GameController.cs +++ b/pac-man-board-game/Controllers/GameController.cs @@ -5,6 +5,7 @@ using pacMan.Game; using pacMan.Game.Interfaces; using pacMan.Game.Items; using pacMan.Interfaces; +using pacMan.Services; using pacMan.Utils; namespace pacMan.Controllers; @@ -14,6 +15,8 @@ namespace pacMan.Controllers; public class GameController : GenericController { private readonly IDiceCup _diceCup; + private GameGroup _group = new(); + private IPlayer? _player; public GameController(ILogger logger, IWebSocketService wsService) : base(logger, wsService) { @@ -48,13 +51,17 @@ public class GameController : GenericController message.Data = rolls; break; case GameAction.PlayerInfo: - Player player = JsonSerializer.Deserialize(message.Data); - var group = WsService.AddPlayer(player); // TODO missing some data? + _player = JsonSerializer.Deserialize(message.Data); + _group = WsService.AddPlayer(_player); // TODO missing some data? - message.Data = group.Players; + message.Data = _group.Players; break; case GameAction.Ready: - // TODO select starter player + if (_player != null) _group.SetReady(_player); + if (_group.AllReady()) + { + // TODO select starter player and send response + } break; default: Logger.Log(LogLevel.Information, "Forwarding message to all clients"); diff --git a/pac-man-board-game/Game/Interfaces/IPlayer.cs b/pac-man-board-game/Game/Interfaces/IPlayer.cs index 114ceee..d7c2611 100644 --- a/pac-man-board-game/Game/Interfaces/IPlayer.cs +++ b/pac-man-board-game/Game/Interfaces/IPlayer.cs @@ -8,4 +8,12 @@ public interface IPlayer Character PacMan { get; init; } string Colour { get; init; } Box Box { get; init; } + State State { get; set; } +} + +public enum State +{ + WaitingForPlayers, + Ready, + InGame } \ No newline at end of file diff --git a/pac-man-board-game/Game/Items/Player.cs b/pac-man-board-game/Game/Items/Player.cs index d39490f..da2db30 100644 --- a/pac-man-board-game/Game/Items/Player.cs +++ b/pac-man-board-game/Game/Items/Player.cs @@ -8,4 +8,5 @@ public class Player : IPlayer public required Character PacMan { get; init; } public required string Colour { get; init; } public required Box Box { get; init; } + public State State { get; set; } = State.WaitingForPlayers; } \ No newline at end of file diff --git a/pac-man-board-game/Services/GameGroup.cs b/pac-man-board-game/Services/GameGroup.cs index 4028192..820c07f 100644 --- a/pac-man-board-game/Services/GameGroup.cs +++ b/pac-man-board-game/Services/GameGroup.cs @@ -13,6 +13,7 @@ public class GameGroup if (Players.Count >= Rules.MaxPlayers) return false; if (Players.Exists(p => p.Name == player.Name)) return false; + player.State = State.WaitingForPlayers; Players.Add(player); return true; } @@ -21,4 +22,14 @@ public class GameGroup { Connections?.Invoke(segment); } + + public bool AllReady() + { + return Players.All(p => p.State == State.Ready); + } + + public void SetReady(IPlayer player) + { + player.State = State.Ready; + } } \ No newline at end of file