diff --git a/BackendTests/Game/Items/DiceCupTests.cs b/BackendTests/Game/Items/DiceCupTests.cs index 577b492..2c691d7 100644 --- a/BackendTests/Game/Items/DiceCupTests.cs +++ b/BackendTests/Game/Items/DiceCupTests.cs @@ -8,7 +8,8 @@ public class DiceCupTests public void Roll_ReturnsTwoElements() { var diceCup = new DiceCup(); - var roll = diceCup.Roll; + diceCup.Roll(); + var roll = diceCup.Values; Assert.That(roll, Has.Count.EqualTo(2)); } @@ -16,10 +17,15 @@ public class DiceCupTests public void Roll_ReturnsNumbersInRange1To6() { var diceCup = new DiceCup(); - var roll = diceCup.Roll; - Assert.That(roll[0], Is.GreaterThanOrEqualTo(1)); - Assert.That(roll[0], Is.LessThanOrEqualTo(6)); - Assert.That(roll[1], Is.GreaterThanOrEqualTo(1)); - Assert.That(roll[1], Is.LessThanOrEqualTo(6)); + diceCup.Roll(); + var roll = diceCup.Values; + + Assert.Multiple(() => + { + Assert.That(roll[0], Is.GreaterThanOrEqualTo(1)); + Assert.That(roll[0], Is.LessThanOrEqualTo(6)); + Assert.That(roll[1], Is.GreaterThanOrEqualTo(1)); + Assert.That(roll[1], Is.LessThanOrEqualTo(6)); + }); } } diff --git a/BackendTests/Game/Items/DiceTests.cs b/BackendTests/Game/Items/DiceTests.cs index 7938e32..9d2313f 100644 --- a/BackendTests/Game/Items/DiceTests.cs +++ b/BackendTests/Game/Items/DiceTests.cs @@ -8,7 +8,8 @@ public class DiceTests public void Roll_ReturnsNumberBetween1And6() { var dice = new Dice(); - var roll = dice.Roll; + dice.Roll(); + var roll = dice.Value; Assert.That(roll, Is.GreaterThanOrEqualTo(1)); Assert.That(roll, Is.LessThanOrEqualTo(6)); } diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs index 279ec3b..04f87b8 100644 --- a/BackendTests/Services/ActionServiceTests.cs +++ b/BackendTests/Services/ActionServiceTests.cs @@ -60,6 +60,7 @@ public class ActionServiceTests [Test] public void RollDice_ReturnsListOfIntegers() { + _service.Group = new pacMan.Services.Game(new Queue()); var dices = _service.RollDice(); Assert.Multiple(() => { diff --git a/pac-man-board-game/GameStuff/Items/Dice.cs b/pac-man-board-game/GameStuff/Items/Dice.cs index 1ea4648..9c593b5 100644 --- a/pac-man-board-game/GameStuff/Items/Dice.cs +++ b/pac-man-board-game/GameStuff/Items/Dice.cs @@ -1,13 +1,12 @@ +using System.Text.Json.Serialization; + namespace pacMan.GameStuff.Items; -public interface IDice -{ - int Roll { get; } -} - -public class Dice : IDice +public class Dice { private readonly Random _random = new(); - public int Roll => _random.Next(1, 7); + [JsonInclude] public int Value { get; private set; } + + public void Roll() => Value = _random.Next(1, 7); } diff --git a/pac-man-board-game/GameStuff/Items/DiceCup.cs b/pac-man-board-game/GameStuff/Items/DiceCup.cs index 7692f0a..1e624a0 100644 --- a/pac-man-board-game/GameStuff/Items/DiceCup.cs +++ b/pac-man-board-game/GameStuff/Items/DiceCup.cs @@ -1,11 +1,8 @@ +using System.Text.Json.Serialization; + namespace pacMan.GameStuff.Items; -public interface IDiceCup -{ - List Roll { get; } -} - -public class DiceCup : IDiceCup +public class DiceCup { private readonly List _dices; @@ -16,5 +13,7 @@ public class DiceCup : IDiceCup new() }; - public List Roll => _dices.Select(d => d.Roll).ToList(); + [JsonInclude] public List Values => _dices.Select(dice => dice.Value).ToList(); + + public void Roll() => _dices.ForEach(dice => dice.Roll()); } diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index 93e65d5..3766c4b 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -19,14 +19,12 @@ public interface IActionService public class ActionService : IActionService { - private readonly IDiceCup _diceCup; private readonly GameService _gameService; private readonly ILogger _logger; public ActionService(ILogger logger, GameService gameService) { _logger = logger; - _diceCup = new DiceCup(); _gameService = gameService; } @@ -48,7 +46,8 @@ public class ActionService : IActionService public List RollDice() { - var rolls = _diceCup.Roll; + Group?.DiceCup.Roll(); + var rolls = Group?.DiceCup.Values ?? new List(); _logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls)); return rolls; diff --git a/pac-man-board-game/Services/Game.cs b/pac-man-board-game/Services/Game.cs index 6102cc9..a803b48 100644 --- a/pac-man-board-game/Services/Game.cs +++ b/pac-man-board-game/Services/Game.cs @@ -18,6 +18,8 @@ public class Game // TODO handle disconnects and reconnects [JsonIgnore] private Queue Spawns { get; } + [JsonIgnore] public DiceCup DiceCup { get; } = new(); + [JsonInclude] public int Count => Players.Count; [JsonInclude]