Cleaned up error handling in backend, all error should throw exception

This commit is contained in:
martin 2023-08-27 12:39:23 +02:00
parent 25f70b8ae4
commit d299176a1e
2 changed files with 15 additions and 28 deletions

View File

@ -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<string>());
Assert.Throws<PlayerNotFoundException>(() => _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]

View File

@ -16,7 +16,7 @@ public interface IActionService
List<int> RollDice();
List<Player> FindGame(JsonElement? jsonElement);
object? HandleMoveCharacter(JsonElement? jsonElement);
object Ready();
ReadyData Ready();
string FindNextPlayer();
List<Player> LeaveGame();
void SendToAll(ArraySegment<byte> 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<Player> LeaveGame()
{