Removed GameService.AddPlayer and it's usages. Updated tests, added tests for IsGameStarted
This commit is contained in:
parent
d299176a1e
commit
8a3dfb058c
@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging;
|
|||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using pacMan.Exceptions;
|
using pacMan.Exceptions;
|
||||||
using pacMan.GameStuff;
|
using pacMan.GameStuff;
|
||||||
|
using pacMan.GameStuff.Items;
|
||||||
using pacMan.Services;
|
using pacMan.Services;
|
||||||
|
|
||||||
namespace BackendTests.Services;
|
namespace BackendTests.Services;
|
||||||
@ -60,7 +61,7 @@ public class GameServiceTests
|
|||||||
public void JoinById_WhenIdNotExists()
|
public void JoinById_WhenIdNotExists()
|
||||||
{
|
{
|
||||||
var player = Players.Create("white");
|
var player = Players.Create("white");
|
||||||
_service.AddPlayer(player, _spawns); // TODO obsolete
|
_service.Games.Add(new pacMan.Services.Game(_spawns) { Players = new List<Player> { player } });
|
||||||
|
|
||||||
Assert.Throws<GameNotFoundException>(() => _service.JoinById(Guid.NewGuid(), player));
|
Assert.Throws<GameNotFoundException>(() => _service.JoinById(Guid.NewGuid(), player));
|
||||||
}
|
}
|
||||||
@ -69,58 +70,20 @@ public class GameServiceTests
|
|||||||
public void JoinById_WhenIdExists()
|
public void JoinById_WhenIdExists()
|
||||||
{
|
{
|
||||||
var player = Players.Create("white");
|
var player = Players.Create("white");
|
||||||
var group = _service.AddPlayer(player, _spawns); // TODO obsolete
|
var game = new pacMan.Services.Game(_spawns) { Players = new List<Player> { player } };
|
||||||
|
_service.Games.Add(game);
|
||||||
|
|
||||||
|
|
||||||
var player2 = Players.Create("black");
|
var player2 = Players.Create("black");
|
||||||
var result = _service.JoinById(group.Id, player2);
|
var result = _service.JoinById(_service.Games[0].Id, player2);
|
||||||
|
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
{
|
{
|
||||||
Assert.That(result, Is.EqualTo(group));
|
Assert.That(result, Is.EqualTo(game));
|
||||||
Assert.That(group.Players, Has.Count.EqualTo(2));
|
Assert.That(game.Players, Has.Count.EqualTo(2));
|
||||||
Assert.That(_service.Games, Has.Count.EqualTo(1));
|
Assert.That(_service.Games, Has.Count.EqualTo(1));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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<DirectionalPosition>(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
|
|
||||||
}
|
}
|
||||||
|
@ -61,13 +61,54 @@ public class GameTests
|
|||||||
|
|
||||||
#endregion
|
#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)
|
#region AddPlayer(Player player)
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void AddPlayer_WhenEmpty()
|
public void AddPlayer_WhenEmpty()
|
||||||
{
|
{
|
||||||
var added = _game.AddPlayer(_redPlayer);
|
Assert.DoesNotThrow(() => _game.AddPlayer(_redPlayer));
|
||||||
Assert.That(added, Is.True);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -75,8 +116,8 @@ public class GameTests
|
|||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
{
|
{
|
||||||
AddFullParty();
|
AddFullParty();
|
||||||
Assert.That(_game.Players.Count, Is.EqualTo(Rules.MaxPlayers));
|
Assert.That(_game.Players, Has.Count.EqualTo(Rules.MaxPlayers));
|
||||||
Assert.That(_game.AddPlayer(_purplePlayer), Is.False);
|
Assert.Throws<GameNotPlayableException>(() => _game.AddPlayer(_purplePlayer));
|
||||||
});
|
});
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -84,8 +125,7 @@ public class GameTests
|
|||||||
{
|
{
|
||||||
var redClone = _redPlayer.Clone();
|
var redClone = _redPlayer.Clone();
|
||||||
_game.AddPlayer(_redPlayer);
|
_game.AddPlayer(_redPlayer);
|
||||||
var added = _game.AddPlayer(redClone);
|
Assert.DoesNotThrow(() => _game.AddPlayer(redClone));
|
||||||
Assert.That(added, Is.True);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -114,7 +154,7 @@ public class GameTests
|
|||||||
_game.SetReady(_bluePlayer.Username);
|
_game.SetReady(_bluePlayer.Username);
|
||||||
_game.SetAllInGame();
|
_game.SetAllInGame();
|
||||||
|
|
||||||
Assert.That(_game.AddPlayer(_greenPlayer), Is.False);
|
Assert.Throws<GameNotPlayableException>(() => _game.AddPlayer(_greenPlayer));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -19,14 +19,19 @@ public class Game
|
|||||||
public List<Player> Players
|
public List<Player> Players
|
||||||
{
|
{
|
||||||
get => _players;
|
get => _players;
|
||||||
set =>
|
set
|
||||||
_players = _players.Select((player, index) =>
|
{
|
||||||
{
|
if (_players.Count > 0)
|
||||||
player.State = value[index].State;
|
_players = _players.Select((player, index) =>
|
||||||
player.PacMan = value[index].PacMan;
|
{
|
||||||
player.Box = value[index].Box;
|
player.State = value[index].State;
|
||||||
return player;
|
player.PacMan = value[index].PacMan;
|
||||||
}).ToList();
|
player.Box = value[index].Box;
|
||||||
|
return player;
|
||||||
|
}).ToList();
|
||||||
|
else
|
||||||
|
_players = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonIgnore] public List<Character> Ghosts { get; set; } = new(); // TODO include
|
[JsonIgnore] public List<Character> Ghosts { get; set; } = new(); // TODO include
|
||||||
@ -37,6 +42,7 @@ public class Game
|
|||||||
|
|
||||||
[JsonInclude] public int Count => Players.Count;
|
[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);
|
[JsonInclude] public bool IsGameStarted => Count > 0 && Players.Any(player => player.State is State.InGame);
|
||||||
|
|
||||||
public Player NextPlayer()
|
public Player NextPlayer()
|
||||||
@ -57,21 +63,18 @@ public class Game
|
|||||||
|
|
||||||
public event Func<ArraySegment<byte>, Task>? Connections;
|
public event Func<ArraySegment<byte>, Task>? Connections;
|
||||||
|
|
||||||
public bool AddPlayer(Player player)
|
public void AddPlayer(Player player)
|
||||||
{
|
{
|
||||||
if (Players.Count >= Rules.MaxPlayers || IsGameStarted) return false;
|
if (Players.Count >= Rules.MaxPlayers)
|
||||||
/* TODO remove above and uncomment below
|
|
||||||
if (Players.Count >= Rules.MaxPlayers)
|
|
||||||
throw new GameNotPlayableException("Game is full");
|
throw new GameNotPlayableException("Game is full");
|
||||||
if (IsGameStarted)
|
if (IsGameStarted)
|
||||||
throw new GameNotPlayableException("Game has already started");
|
throw new GameNotPlayableException("Game has already started");
|
||||||
*/
|
|
||||||
|
|
||||||
player.State = State.WaitingForPlayers;
|
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);
|
Players.Add(player);
|
||||||
if (player.PacMan.SpawnPosition is null) SetSpawn(player);
|
if (player.PacMan.SpawnPosition is null) SetSpawn(player);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player? RemovePlayer(string username)
|
public Player? RemovePlayer(string username)
|
||||||
|
@ -19,24 +19,6 @@ public class GameService : WebSocketService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public SynchronizedCollection<Game> Games { get; } = new();
|
public SynchronizedCollection<Game> Games { get; } = new();
|
||||||
|
|
||||||
[Obsolete("Use CreateAndJoin instead")]
|
|
||||||
public Game AddPlayer(Player player, Queue<DirectionalPosition> 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];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method tries to find a game with the specified id, add a player to it and return the updated game.
|
/// This method tries to find a game with the specified id, add a player to it and return the updated game.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user