diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs index 31ef4fa..734f282 100644 --- a/BackendTests/Services/ActionServiceTests.cs +++ b/BackendTests/Services/ActionServiceTests.cs @@ -2,6 +2,7 @@ using System.Text.Json; using BackendTests.TestUtils; using Microsoft.Extensions.Logging; using NSubstitute; +using pacMan.Exceptions; using pacMan.GameStuff; using pacMan.GameStuff.Items; using pacMan.Services; @@ -143,8 +144,7 @@ public class ActionServiceTests [Test] public void Ready_PlayerIsNull() { - var result = _service.Ready(); - Assert.That(result, Is.InstanceOf()); + Assert.Throws(() => _service.Ready()); } [Test] @@ -157,19 +157,13 @@ public class ActionServiceTests _service.FindGame(_whiteMessage.Data); // Sets white to ready var result = _service.Ready(); - if (result is ReadyData r1) - Assert.That(r1.AllReady, Is.False); - else - Assert.Fail("Result should be ReadyData"); + Assert.That(result.AllReady, Is.False); _gameService.JoinById(game.Id, _redPlayer); _service.FindGame(_redMessage.Data); // Sets red to ready result = _service.Ready(); - if (result is ReadyData r2) - Assert.That(r2.AllReady, Is.False); - else - Assert.Fail("Result should be ReadyData"); + Assert.That(result.AllReady, Is.False); } [Test] diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index e33ff1f..8f85c17 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -16,7 +16,7 @@ public interface IActionService List RollDice(); List FindGame(JsonElement? jsonElement); object? HandleMoveCharacter(JsonElement? jsonElement); - object Ready(); + ReadyData Ready(); string FindNextPlayer(); List LeaveGame(); void SendToAll(ArraySegment segment); @@ -94,27 +94,20 @@ public class ActionService : IActionService return Game.Players; } - public object Ready() + public ReadyData Ready() { - object data; - if (Player != null && Game != null) - { - var players = Game.SetReady(Player.Username).ToArray(); - // TODO roll to start - Game.Shuffle(); - var allReady = players.All(p => p.State == State.Ready); - if (allReady) Game.SetAllInGame(); - data = new ReadyData { AllReady = allReady, Players = players }; - } - else - { - data = "Player not found, please create a new player"; - } + if (Player == null || Game == null) + throw new PlayerNotFoundException("Player not found, please create a new player"); - return data; + var players = Game.SetReady(Player.Username).ToArray(); + // TODO roll to start + Game.Shuffle(); + var allReady = players.All(p => p.State == State.Ready); + if (allReady) Game.SetAllInGame(); + return new ReadyData { AllReady = allReady, Players = players }; } - public string FindNextPlayer() => Game?.NextPlayer().Username ?? "Error: No group found"; + public string FindNextPlayer() => Game?.NextPlayer().Username ?? throw new GameNotFoundException(); public List LeaveGame() {