🔥 ⚰️ Fix #60
continuous-integration/drone/push Build is passing Details

pull/76/head
Alexis Drai 2 years ago
parent 97c888b895
commit 86ff2fe752

@ -1,4 +1,5 @@
using Model.Dice; using Model;
using Model.Dice;
using Model.Dice.Faces; using Model.Dice.Faces;
using Model.Games; using Model.Games;
using Model.Players; using Model.Players;
@ -19,7 +20,7 @@ namespace Data
Player player1 = new("Alice"), player2 = new("Bob"), player3 = new("Clyde"); Player player1 = new("Alice"), player2 = new("Bob"), player3 = new("Clyde");
DieManager globalDieManager = new DieManager(); IManager<(string, IEnumerable<AbstractDie<AbstractDieFace>>)> globalDieManager = new DieManager();
// create at least one fav group in there // create at least one fav group in there
// ... // ...

@ -17,7 +17,7 @@ namespace Model.Dice
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public (string, IEnumerable<AbstractDie<AbstractDieFace>>) GetOneById(int id) public (string, IEnumerable<AbstractDie<AbstractDieFace>>) GetOneByName(string name)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }

@ -33,9 +33,9 @@ namespace Model.Games
private string name; private string name;
/// <summary> /// <summary>
/// whether this game is new or not /// references the position in list of the current player, for a given game.
/// </summary> /// </summary>
public bool IsFirstTurn { get; private set; } = false; private int nextIndex = 0;
/// <summary> /// <summary>
/// the turns that have been done so far /// the turns that have been done so far
@ -51,7 +51,7 @@ namespace Model.Games
/// <summary> /// <summary>
/// the game's player manager, doing CRUD on players and switching whose turn it is /// the game's player manager, doing CRUD on players and switching whose turn it is
/// </summary> /// </summary>
private readonly PlayerManager playerManager; private readonly IManager<Player> playerManager;
/// <summary> /// <summary>
/// the group of dice used for this game /// the group of dice used for this game
@ -66,7 +66,7 @@ namespace Model.Games
/// <param name="turns">the turns that have been done so far</param> /// <param name="turns">the turns that have been done so far</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param> /// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param> /// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, PlayerManager playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice, IEnumerable<Turn> turns) public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice, IEnumerable<Turn> turns)
{ {
Name = name; Name = name;
this.turns = turns.ToList() ?? new List<Turn>(); this.turns = turns.ToList() ?? new List<Turn>();
@ -80,7 +80,7 @@ namespace Model.Games
/// <param name="name">the name of the game 😎</param> /// <param name="name">the name of the game 😎</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param> /// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param> /// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, PlayerManager playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice) public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice)
: this(name, playerManager, dice, null) : this(name, playerManager, dice, null)
{ } { }
@ -90,21 +90,61 @@ namespace Model.Games
/// <param name="player">the player whose turn it is</param> /// <param name="player">the player whose turn it is</param>
public void PerformTurn(Player player) public void PerformTurn(Player player)
{ {
if (IsFirstTurn) { IsFirstTurn = false; } // only true one time (on the first turn...)
Turn turn = Turn.CreateWithDefaultTime( Turn turn = Turn.CreateWithDefaultTime(
new Player(player), new Player(player),
ThrowAll() //using a copy so that next line doesn't "change history" ThrowAll() //using a copy so that next lines can't "change history"
); );
turns.Add(turn); turns.Add(turn);
nextIndex++;
} }
/// <summary> /// <summary>
/// finds whose turn it is /// finds and returns the player whose turn it is
/// </summary> /// </summary>
/// <returns>the Player whose turn it is</returns> /// <returns></returns>
/// <exception cref="Exception"></exception>
public Player GetWhoPlaysNow() public Player GetWhoPlaysNow()
{ {
return playerManager.WhoPlaysNow(IsFirstTurn); if (!playerManager.GetAll().Any())
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
return playerManager.GetAll().ElementAt(nextIndex);
}
/// <summary>
/// this feels very dirty
/// </summary>
/// <param name="current">the current player</param>
/// <exception cref="MemberAccessException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void PrepareNextPlayer(Player current)
{
if (!playerManager.GetAll().Any())
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
if (current == null)
{
throw new ArgumentNullException(nameof(current), "param should not be null");
}
if (!playerManager.GetAll().Contains(current))
{
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
}
if (playerManager.GetAll().Last() == current)
{
// if we've reached the last player, we need the index to loop back around
nextIndex = 0;
}
else
{
// else we can just move up by one from the current index
nextIndex++;
}
} }
/// <summary> /// <summary>
@ -121,15 +161,6 @@ namespace Model.Games
return faces; return faces;
} }
/// <summary>
/// asks the PlayerManager to prepare the next Player
/// </summary>
/// <param name="currentPlayer">the Player whose turn it was</param>
public void PrepareNextPlayer(Player currentPlayer)
{
playerManager.PrepareNextPlayer(currentPlayer);
}
public Player AddPlayerToGame(Player player) public Player AddPlayerToGame(Player player)
{ {
return playerManager.Add(player); return playerManager.Add(player);
@ -162,7 +193,7 @@ namespace Model.Games
"Log:\n", "Log:\n",
Name, Name,
playerManager.GetAll().ToString(), playerManager.GetAll().ToString(),
playerManager.WhoPlaysNow(IsFirstTurn)); GetWhoPlaysNow());
foreach (Turn turn in this.turns) foreach (Turn turn in this.turns)
{ {

@ -4,17 +4,18 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Model.Dice; using Model.Dice;
using Model.Dice.Faces;
using Model.Players; using Model.Players;
namespace Model.Games namespace Model.Games
{ {
public class GameRunner public class GameRunner
{ {
private readonly PlayerManager globalPlayerManager; private readonly IManager<Player> globalPlayerManager;
private readonly DieManager globalDieManager; private readonly IManager<(string, IEnumerable<AbstractDie<AbstractDieFace>>)> globalDieManager;
private readonly List<Game> games; private readonly List<Game> games;
public GameRunner(PlayerManager globalPlayerManager, DieManager globalDieManager, List<Game> games) public GameRunner(IManager<Player> globalPlayerManager, IManager<(string, IEnumerable<AbstractDie<AbstractDieFace>>)> globalDieManager, List<Game> games)
{ {
this.globalPlayerManager = globalPlayerManager; this.globalPlayerManager = globalPlayerManager;
this.globalDieManager = globalDieManager; this.globalDieManager = globalDieManager;

@ -4,7 +4,7 @@ namespace Model
public interface IManager<T> public interface IManager<T>
{ {
public T Add(T toAdd); public T Add(T toAdd);
public T GetOneById(int id); public T GetOneByName(string name);
public IEnumerable<T> GetAll(); public IEnumerable<T> GetAll();
public T Update(T before, T after); public T Update(T before, T after);
public void Remove(T toRemove); public void Remove(T toRemove);

@ -11,13 +11,6 @@ namespace Model.Players
/// </summary> /// </summary>
private readonly List<Player> players; private readonly List<Player> players;
/// <summary>
/// references the position in list of the current player, for a given game.
/// <br/>
/// ASSUMING that each Game made its own instance of PlayerManager
/// </summary>
public int NextIndex { get; private set; } = 0;
public PlayerManager() public PlayerManager()
{ {
players = new(); players = new();
@ -41,17 +34,6 @@ namespace Model.Players
return toAdd; return toAdd;
} }
/// <summary>
/// might never get implemented in the model, go through GetOneByName() in the meantime
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Player GetOneById(int id)
{
throw new NotImplementedException("might never get implemented\ngo through GetOneByName() in the meantime");
}
/// <summary> /// <summary>
/// finds the player with that name and returns A COPY OF IT /// finds the player with that name and returns A COPY OF IT
/// <br/> /// <br/>
@ -77,64 +59,6 @@ namespace Model.Players
/// <returns>a readonly enumerable of all this manager's players</returns> /// <returns>a readonly enumerable of all this manager's players</returns>
public IEnumerable<Player> GetAll() => players.AsEnumerable(); public IEnumerable<Player> GetAll() => players.AsEnumerable();
/// <summary>
/// finds and returns the player whose turn it is
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public Player WhoPlaysNow(bool isFirstTurn)
{
if (players.Count == 0)
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
Player result;
if (isFirstTurn)
{
result = players[0];
}
else
{
result = players[NextIndex];
}
return result;
}
/// <summary>
/// this feels very dirty
/// </summary>
/// <param name="current"></param>
/// <exception cref="Exception"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void PrepareNextPlayer(Player current)
{
if (players.Count == 0)
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
if (current == null)
{
throw new ArgumentNullException(nameof(current), "param should not be null");
}
if (!players.Contains(current))
{
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
}
if (players.Last() == current)
{
// if we've reached the last index, we need to loop back around
NextIndex = 0;
}
else
{
// else we can just move up one from current
NextIndex++;
}
}
/// <summary> /// <summary>
/// update a player from <paramref name="before"/> to <paramref name="after"/> /// update a player from <paramref name="before"/> to <paramref name="after"/>
/// </summary> /// </summary>

@ -63,19 +63,6 @@ namespace Tests.Model_UTs
Assert.Throws<ArgumentNullException>(action); Assert.Throws<ArgumentNullException>(action);
} }
[Fact]
public void TestGetOneByIdThrowsNotImplemented()
{
// Arrange
PlayerManager playerManager = new();
// Act
void action() => playerManager.GetOneById(1);
// Assert
Assert.Throws<NotImplementedException>(action);
}
[Theory] [Theory]
[InlineData("")] [InlineData("")]
[InlineData(null)] [InlineData(null)]

Loading…
Cancel
Save