diff --git a/Sources/Data/Stub.cs b/Sources/Data/Stub.cs index 2728ace..5b68c6a 100644 --- a/Sources/Data/Stub.cs +++ b/Sources/Data/Stub.cs @@ -1,4 +1,5 @@ -using Model.Dice; +using Model; +using Model.Dice; using Model.Dice.Faces; using Model.Games; using Model.Players; @@ -19,7 +20,7 @@ namespace Data Player player1 = new("Alice"), player2 = new("Bob"), player3 = new("Clyde"); - DieManager globalDieManager = new DieManager(); + IManager<(string, IEnumerable>)> globalDieManager = new DieManager(); // create at least one fav group in there // ... diff --git a/Sources/Model/Dice/DieManager.cs b/Sources/Model/Dice/DieManager.cs index b6d1812..a26a83d 100644 --- a/Sources/Model/Dice/DieManager.cs +++ b/Sources/Model/Dice/DieManager.cs @@ -17,7 +17,7 @@ namespace Model.Dice throw new System.NotImplementedException(); } - public (string, IEnumerable>) GetOneById(int id) + public (string, IEnumerable>) GetOneByName(string name) { throw new System.NotImplementedException(); } diff --git a/Sources/Model/Games/Game.cs b/Sources/Model/Games/Game.cs index 326aa80..9dd95bf 100644 --- a/Sources/Model/Games/Game.cs +++ b/Sources/Model/Games/Game.cs @@ -33,9 +33,9 @@ namespace Model.Games private string name; /// - /// whether this game is new or not + /// references the position in list of the current player, for a given game. /// - public bool IsFirstTurn { get; private set; } = false; + private int nextIndex = 0; /// /// the turns that have been done so far @@ -51,7 +51,7 @@ namespace Model.Games /// /// the game's player manager, doing CRUD on players and switching whose turn it is /// - private readonly PlayerManager playerManager; + private readonly IManager playerManager; /// /// the group of dice used for this game @@ -66,7 +66,7 @@ namespace Model.Games /// the turns that have been done so far /// the game's player manager, doing CRUD on players and switching whose turn it is /// the group of dice used for this game - public Game(string name, PlayerManager playerManager, IEnumerable> dice, IEnumerable turns) + public Game(string name, IManager playerManager, IEnumerable> dice, IEnumerable turns) { Name = name; this.turns = turns.ToList() ?? new List(); @@ -80,7 +80,7 @@ namespace Model.Games /// the name of the game 😎 /// the game's player manager, doing CRUD on players and switching whose turn it is /// the group of dice used for this game - public Game(string name, PlayerManager playerManager, IEnumerable> dice) + public Game(string name, IManager playerManager, IEnumerable> dice) : this(name, playerManager, dice, null) { } @@ -90,21 +90,61 @@ namespace Model.Games /// the player whose turn it is public void PerformTurn(Player player) { - if (IsFirstTurn) { IsFirstTurn = false; } // only true one time (on the first turn...) Turn turn = Turn.CreateWithDefaultTime( 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); + nextIndex++; } /// - /// finds whose turn it is + /// finds and returns the player whose turn it is /// - /// the Player whose turn it is + /// + /// 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); + } + + /// + /// this feels very dirty + /// + /// the current player + /// + /// + /// + 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++; + } } /// @@ -121,15 +161,6 @@ namespace Model.Games return faces; } - /// - /// asks the PlayerManager to prepare the next Player - /// - /// the Player whose turn it was - public void PrepareNextPlayer(Player currentPlayer) - { - playerManager.PrepareNextPlayer(currentPlayer); - } - public Player AddPlayerToGame(Player player) { return playerManager.Add(player); @@ -162,7 +193,7 @@ namespace Model.Games "Log:\n", Name, playerManager.GetAll().ToString(), - playerManager.WhoPlaysNow(IsFirstTurn)); + GetWhoPlaysNow()); foreach (Turn turn in this.turns) { diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 035a96b..ff91687 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -4,17 +4,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Model.Dice; +using Model.Dice.Faces; using Model.Players; namespace Model.Games { public class GameRunner { - private readonly PlayerManager globalPlayerManager; - private readonly DieManager globalDieManager; + private readonly IManager globalPlayerManager; + private readonly IManager<(string, IEnumerable>)> globalDieManager; private readonly List games; - public GameRunner(PlayerManager globalPlayerManager, DieManager globalDieManager, List games) + public GameRunner(IManager globalPlayerManager, IManager<(string, IEnumerable>)> globalDieManager, List games) { this.globalPlayerManager = globalPlayerManager; this.globalDieManager = globalDieManager; diff --git a/Sources/Model/IManager.cs b/Sources/Model/IManager.cs index 2d7c13e..67eafe0 100644 --- a/Sources/Model/IManager.cs +++ b/Sources/Model/IManager.cs @@ -4,7 +4,7 @@ namespace Model public interface IManager { public T Add(T toAdd); - public T GetOneById(int id); + public T GetOneByName(string name); public IEnumerable GetAll(); public T Update(T before, T after); public void Remove(T toRemove); diff --git a/Sources/Model/Players/PlayerManager.cs b/Sources/Model/Players/PlayerManager.cs index f57159d..c5637da 100644 --- a/Sources/Model/Players/PlayerManager.cs +++ b/Sources/Model/Players/PlayerManager.cs @@ -11,13 +11,6 @@ namespace Model.Players /// private readonly List players; - /// - /// references the position in list of the current player, for a given game. - ///
- /// ASSUMING that each Game made its own instance of PlayerManager - ///
- public int NextIndex { get; private set; } = 0; - public PlayerManager() { players = new(); @@ -41,17 +34,6 @@ namespace Model.Players return toAdd; } - /// - /// might never get implemented in the model, go through GetOneByName() in the meantime - /// - /// - /// - /// - public Player GetOneById(int id) - { - throw new NotImplementedException("might never get implemented\ngo through GetOneByName() in the meantime"); - } - /// /// finds the player with that name and returns A COPY OF IT ///
@@ -77,64 +59,6 @@ namespace Model.Players /// a readonly enumerable of all this manager's players public IEnumerable GetAll() => players.AsEnumerable(); - /// - /// finds and returns the player whose turn it is - /// - /// - /// - 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; - } - - /// - /// this feels very dirty - /// - /// - /// - /// - /// - 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++; - } - } - /// /// update a player from to /// diff --git a/Sources/Tests/Model_UTs/PlayerManagerTest.cs b/Sources/Tests/Model_UTs/PlayerManagerTest.cs index 3058929..fb498d4 100644 --- a/Sources/Tests/Model_UTs/PlayerManagerTest.cs +++ b/Sources/Tests/Model_UTs/PlayerManagerTest.cs @@ -63,19 +63,6 @@ namespace Tests.Model_UTs Assert.Throws(action); } - [Fact] - public void TestGetOneByIdThrowsNotImplemented() - { - // Arrange - PlayerManager playerManager = new(); - - // Act - void action() => playerManager.GetOneById(1); - - // Assert - Assert.Throws(action); - } - [Theory] [InlineData("")] [InlineData(null)]