From 8a3dfb058c1d7be4d78e48b5ef8702b184355c95 Mon Sep 17 00:00:00 2001 From: martin Date: Sun, 27 Aug 2023 13:21:54 +0200 Subject: [PATCH] Removed GameService.AddPlayer and it's usages. Updated tests, added tests for IsGameStarted --- BackendTests/Services/GameServiceTests.cs | 53 ++++----------------- BackendTests/Services/GameTests.cs | 54 +++++++++++++++++++--- pac-man-board-game/Services/Game.cs | 33 +++++++------ pac-man-board-game/Services/GameService.cs | 18 -------- 4 files changed, 73 insertions(+), 85 deletions(-) diff --git a/BackendTests/Services/GameServiceTests.cs b/BackendTests/Services/GameServiceTests.cs index 80e5759..59e9df9 100644 --- a/BackendTests/Services/GameServiceTests.cs +++ b/BackendTests/Services/GameServiceTests.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using NSubstitute; using pacMan.Exceptions; using pacMan.GameStuff; +using pacMan.GameStuff.Items; using pacMan.Services; namespace BackendTests.Services; @@ -60,7 +61,7 @@ public class GameServiceTests public void JoinById_WhenIdNotExists() { var player = Players.Create("white"); - _service.AddPlayer(player, _spawns); // TODO obsolete + _service.Games.Add(new pacMan.Services.Game(_spawns) { Players = new List { player } }); Assert.Throws(() => _service.JoinById(Guid.NewGuid(), player)); } @@ -69,58 +70,20 @@ public class GameServiceTests public void JoinById_WhenIdExists() { var player = Players.Create("white"); - var group = _service.AddPlayer(player, _spawns); // TODO obsolete + var game = new pacMan.Services.Game(_spawns) { Players = new List { player } }; + _service.Games.Add(game); + var player2 = Players.Create("black"); - var result = _service.JoinById(group.Id, player2); + var result = _service.JoinById(_service.Games[0].Id, player2); Assert.Multiple(() => { - Assert.That(result, Is.EqualTo(group)); - Assert.That(group.Players, Has.Count.EqualTo(2)); + Assert.That(result, Is.EqualTo(game)); + Assert.That(game.Players, Has.Count.EqualTo(2)); Assert.That(_service.Games, Has.Count.EqualTo(1)); }); } #endregion - - #region AddPlayer(IPlayer) - - [Test] - public void AddPlayer_ToEmptyGroup() - { - var player = Players.Create("white"); - var group = _service.AddPlayer(player, _spawns); // TODO obsolete - - Assert.Multiple(() => - { - Assert.That(group.Players, Has.Count.EqualTo(1)); - Assert.That(group.NextPlayer, Is.EqualTo(player)); - Assert.That(_service.Games, Has.Count.EqualTo(1)); - }); - } - - [Test] - public void AddPlayer_ToFullGroup() - { - for (var i = 0; i < 4; i++) - { - var player = Players.Create(i.ToString()); - _service.AddPlayer(player, _spawns); // TODO obsolete - } - - var player5 = Players.Create("white"); - - var group = _service.AddPlayer(player5, new Queue(new[] { _spawn3By3Up })); - - Assert.Multiple(() => - { - Assert.That(group.Players, Has.Count.EqualTo(1)); - Assert.That(group.NextPlayer, Is.EqualTo(player5)); - Assert.That(_service.Games, Has.Count.EqualTo(2)); - Assert.That(_service.Games.First(), Has.Count.EqualTo(Rules.MaxPlayers)); - }); - } - - #endregion } diff --git a/BackendTests/Services/GameTests.cs b/BackendTests/Services/GameTests.cs index 0d7b17b..5ceab4b 100644 --- a/BackendTests/Services/GameTests.cs +++ b/BackendTests/Services/GameTests.cs @@ -61,13 +61,54 @@ public class GameTests #endregion + #region IsGameStarted + + [Test] + public void IsGameStarted_WhenEmpty() + { + Assert.That(_game.IsGameStarted, Is.False); + } + + [Test] + public void IsGameStarted_WhenNotAllInGame() + { + AddFullParty(); + Assert.That(_game.IsGameStarted, Is.False); + } + + [Test] + public void IsGameStarted_WhenAllInGame() + { + AddFullParty(); + _game.Players.ForEach(player => player.State = State.InGame); + Assert.That(_game.IsGameStarted, Is.True); + } + + [Test] + public void IsGameStared_WhenAllDisconnected() + { + AddFullParty(); + _game.Players.ForEach(player => player.State = State.Disconnected); + Assert.That(_game.IsGameStarted, Is.False); + } + + [Test] + public void IsGameStarted_WhenHalfInGame() + { + AddFullParty(); + _game.Players.ForEach(player => + player.State = _game.Players.IndexOf(player) % 2 == 0 ? State.InGame : State.Disconnected); + Assert.That(_game.IsGameStarted, Is.True); + } + + #endregion + #region AddPlayer(Player player) [Test] public void AddPlayer_WhenEmpty() { - var added = _game.AddPlayer(_redPlayer); - Assert.That(added, Is.True); + Assert.DoesNotThrow(() => _game.AddPlayer(_redPlayer)); } [Test] @@ -75,8 +116,8 @@ public class GameTests Assert.Multiple(() => { AddFullParty(); - Assert.That(_game.Players.Count, Is.EqualTo(Rules.MaxPlayers)); - Assert.That(_game.AddPlayer(_purplePlayer), Is.False); + Assert.That(_game.Players, Has.Count.EqualTo(Rules.MaxPlayers)); + Assert.Throws(() => _game.AddPlayer(_purplePlayer)); }); [Test] @@ -84,8 +125,7 @@ public class GameTests { var redClone = _redPlayer.Clone(); _game.AddPlayer(_redPlayer); - var added = _game.AddPlayer(redClone); - Assert.That(added, Is.True); + Assert.DoesNotThrow(() => _game.AddPlayer(redClone)); } [Test] @@ -114,7 +154,7 @@ public class GameTests _game.SetReady(_bluePlayer.Username); _game.SetAllInGame(); - Assert.That(_game.AddPlayer(_greenPlayer), Is.False); + Assert.Throws(() => _game.AddPlayer(_greenPlayer)); } #endregion diff --git a/pac-man-board-game/Services/Game.cs b/pac-man-board-game/Services/Game.cs index 16ed112..08d88a7 100644 --- a/pac-man-board-game/Services/Game.cs +++ b/pac-man-board-game/Services/Game.cs @@ -19,14 +19,19 @@ public class Game public List Players { get => _players; - set => - _players = _players.Select((player, index) => - { - player.State = value[index].State; - player.PacMan = value[index].PacMan; - player.Box = value[index].Box; - return player; - }).ToList(); + set + { + if (_players.Count > 0) + _players = _players.Select((player, index) => + { + player.State = value[index].State; + player.PacMan = value[index].PacMan; + player.Box = value[index].Box; + return player; + }).ToList(); + else + _players = value; + } } [JsonIgnore] public List Ghosts { get; set; } = new(); // TODO include @@ -37,6 +42,7 @@ public class Game [JsonInclude] public int Count => Players.Count; + // TODO edge-case when game has started but all players have disconnected, Disconnected property? [JsonInclude] public bool IsGameStarted => Count > 0 && Players.Any(player => player.State is State.InGame); public Player NextPlayer() @@ -57,21 +63,18 @@ public class Game public event Func, Task>? Connections; - public bool AddPlayer(Player player) + public void AddPlayer(Player player) { - if (Players.Count >= Rules.MaxPlayers || IsGameStarted) return false; - /* TODO remove above and uncomment below - if (Players.Count >= Rules.MaxPlayers) + if (Players.Count >= Rules.MaxPlayers) throw new GameNotPlayableException("Game is full"); if (IsGameStarted) throw new GameNotPlayableException("Game has already started"); - */ player.State = State.WaitingForPlayers; - if (Players.Exists(p => p.Username == player.Username)) return true; // TODO change to false + if (Players.Exists(p => p.Username == player.Username)) return; + Players.Add(player); if (player.PacMan.SpawnPosition is null) SetSpawn(player); - return true; } public Player? RemovePlayer(string username) diff --git a/pac-man-board-game/Services/GameService.cs b/pac-man-board-game/Services/GameService.cs index adbc8ab..2eb1d8a 100644 --- a/pac-man-board-game/Services/GameService.cs +++ b/pac-man-board-game/Services/GameService.cs @@ -19,24 +19,6 @@ public class GameService : WebSocketService /// public SynchronizedCollection Games { get; } = new(); - [Obsolete("Use CreateAndJoin instead")] - public Game AddPlayer(Player player, Queue spawns) - { - var index = 0; - try - { - while (!Games[index].AddPlayer(player)) index++; - } - catch (ArgumentOutOfRangeException) - { - var game = new Game(spawns); - game.AddPlayer(player); - Games.Add(game); - } - - return Games[index]; - } - /// /// This method tries to find a game with the specified id, add a player to it and return the updated game. ///