From 7af502f570cc8e567e87c45dbd0d6802b36d19a1 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Thu, 20 Jul 2023 22:56:35 +0200 Subject: [PATCH] fixed state not updating when joining from lobby --- BackendTests/Services/GameTests.cs | 10 +++++----- pac-man-board-game/ClientApp/src/utils/actions.ts | 1 + pac-man-board-game/Controllers/GameController.cs | 2 +- pac-man-board-game/GameStuff/Actions.cs | 3 ++- pac-man-board-game/Services/ActionService.cs | 6 +++--- pac-man-board-game/Services/Game.cs | 5 +++-- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/BackendTests/Services/GameTests.cs b/BackendTests/Services/GameTests.cs index 0c6b448..0d7b17b 100644 --- a/BackendTests/Services/GameTests.cs +++ b/BackendTests/Services/GameTests.cs @@ -110,8 +110,8 @@ public class GameTests _game.AddPlayer(_redPlayer); _game.AddPlayer(_bluePlayer); - _game.SetReady(_redPlayer); - _game.SetReady(_bluePlayer); + _game.SetReady(_redPlayer.Username); + _game.SetReady(_bluePlayer.Username); _game.SetAllInGame(); Assert.That(_game.AddPlayer(_greenPlayer), Is.False); @@ -154,7 +154,7 @@ public class GameTests _game.AddPlayer(_redPlayer); _game.AddPlayer(_bluePlayer); - var players = _game.SetReady(_redPlayer).ToList(); + var players = _game.SetReady(_redPlayer.Username).ToList(); Assert.Multiple(() => { @@ -171,7 +171,7 @@ public class GameTests Assert.That(_redPlayer.State, Is.Not.EqualTo(State.Ready)); - _game.SetReady(_redPlayer); + _game.SetReady(_redPlayer.Username); Assert.That(_redPlayer.State, Is.EqualTo(State.Ready)); } @@ -179,7 +179,7 @@ public class GameTests [Test] public void SetReady_WhenPlayerIsNotInPlayers() { - Assert.Throws(() => _game.SetReady(_redPlayer)); + Assert.Throws(() => _game.SetReady(_redPlayer.Username)); } #endregion diff --git a/pac-man-board-game/ClientApp/src/utils/actions.ts b/pac-man-board-game/ClientApp/src/utils/actions.ts index d5f3bb7..7dac3f5 100644 --- a/pac-man-board-game/ClientApp/src/utils/actions.ts +++ b/pac-man-board-game/ClientApp/src/utils/actions.ts @@ -12,6 +12,7 @@ export enum GameAction { playerInfo, ready, nextPlayer, + disconnect, } const store = getDefaultStore(); diff --git a/pac-man-board-game/Controllers/GameController.cs b/pac-man-board-game/Controllers/GameController.cs index 80a96cb..b496466 100644 --- a/pac-man-board-game/Controllers/GameController.cs +++ b/pac-man-board-game/Controllers/GameController.cs @@ -82,7 +82,7 @@ public class GameController : GenericController protected override Task Echo() { - _gameService.Connections += WsServiceOnFire; + _gameService.Connections += WsServiceOnFire; // TODO move to ActionService // _actionService.Game.Connections += WsServiceOnFire; return base.Echo(); } diff --git a/pac-man-board-game/GameStuff/Actions.cs b/pac-man-board-game/GameStuff/Actions.cs index eeeb847..94d1e41 100644 --- a/pac-man-board-game/GameStuff/Actions.cs +++ b/pac-man-board-game/GameStuff/Actions.cs @@ -9,7 +9,8 @@ public enum GameAction MoveCharacter, PlayerInfo, Ready, - NextPlayer + NextPlayer, + Disconnect } public class ActionMessage diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index bd09d77..a4b2237 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -38,10 +38,10 @@ public class ActionService : IActionService message.Data = message.Action switch { GameAction.RollDice => RollDice(), + GameAction.MoveCharacter => HandleMoveCharacter(message.Data), GameAction.PlayerInfo => SetPlayerInfo(message.Data), GameAction.Ready => Ready(), GameAction.NextPlayer => FindNextPlayer(), - GameAction.MoveCharacter => HandleMoveCharacter(message.Data), _ => message.Data }; } @@ -55,7 +55,7 @@ public class ActionService : IActionService return rolls; } - public List SetPlayerInfo(JsonElement? jsonElement) + public List SetPlayerInfo(JsonElement? jsonElement) // TODO split up into two actions { var data = jsonElement?.Deserialize() ?? throw new NullReferenceException("Data is null"); Player = data.Player; @@ -83,7 +83,7 @@ public class ActionService : IActionService object data; if (Player != null && Game != null) { - var players = Game.SetReady(Player).ToArray(); + var players = Game.SetReady(Player.Username).ToArray(); // TODO roll to start Game.Shuffle(); var allReady = players.All(p => p.State == State.Ready); diff --git a/pac-man-board-game/Services/Game.cs b/pac-man-board-game/Services/Game.cs index a58ec20..25eda66 100644 --- a/pac-man-board-game/Services/Game.cs +++ b/pac-man-board-game/Services/Game.cs @@ -81,9 +81,10 @@ public class Game // TODO handle disconnects and reconnects public void SendToAll(ArraySegment segment) => Connections?.Invoke(segment); - public IEnumerable SetReady(Player player) + public IEnumerable SetReady(string username) { - if (!Players.Contains(player)) + var player = Players.FirstOrDefault(p => p.Username == username); + if (player is null) throw new PlayerNotFoundException("The player was not found in the game group."); player.State = State.Ready; return Players;