From 0dc4a7b846cbc5086b0295f8420ad62fa34e0906 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Wed, 12 Jul 2023 21:33:52 +0200 Subject: [PATCH] GameGroup implements IEnumerable, removed Getters from ActionService Properties --- BackendTests/Services/ActionServiceTests.cs | 4 ++-- pac-man-board-game/Services/ActionService.cs | 6 +++--- pac-man-board-game/Services/GameGroup.cs | 7 ++++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs index 88f2367..279df64 100644 --- a/BackendTests/Services/ActionServiceTests.cs +++ b/BackendTests/Services/ActionServiceTests.cs @@ -141,8 +141,8 @@ public class ActionServiceTests [Test] public void Ready_TwoReady() { - _service.Group.AddPlayer(_blackPlayer); - _service.Group.AddPlayer(_whitePlayer); + var group = new GameGroup { Players = { _blackPlayer, _whitePlayer } }; + _service.Group = group; _service.Player = _blackPlayer; var result = _service.Ready(); diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index f9153fb..6a7de16 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -9,15 +9,15 @@ namespace pacMan.Services; public interface IActionService { - IPlayer Player { get; set; } - GameGroup Group { get; set; } + IPlayer Player { set; } + GameGroup Group { set; } void DoAction(ActionMessage message); List RollDice(); List SetPlayerInfo(ActionMessage message); object Ready(); } -public class ActionService : IActionService // TODO tests +public class ActionService : IActionService { private readonly IDiceCup _diceCup; private readonly ILogger _logger; diff --git a/pac-man-board-game/Services/GameGroup.cs b/pac-man-board-game/Services/GameGroup.cs index 632dc22..21d8a4c 100644 --- a/pac-man-board-game/Services/GameGroup.cs +++ b/pac-man-board-game/Services/GameGroup.cs @@ -1,15 +1,20 @@ +using System.Collections; using pacMan.Exceptions; using pacMan.Game; using pacMan.Game.Interfaces; namespace pacMan.Services; -public class GameGroup +public class GameGroup : IEnumerable { private readonly Random _random = new(); public List Players { get; } = new(); public IPlayer RandomPlayer => Players[_random.Next(Players.Count)]; + + public IEnumerator GetEnumerator() => Players.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public event Func, Task>? Connections; public bool AddPlayer(IPlayer player) // TODO if name exists, use that player instead