From 9d92f3b84fdc13e5f654af09ec05459a70eff576 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 28 Sep 2022 20:40:03 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Make=20GameRunner=20implement?= =?UTF-8?q?=20the=20IManager=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit since it already did (pretty much) --- Sources/App/Program.cs | 8 ++++---- Sources/Model/Games/GameRunner.cs | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Sources/App/Program.cs b/Sources/App/Program.cs index c609a4d..95cc927 100644 --- a/Sources/App/Program.cs +++ b/Sources/App/Program.cs @@ -48,7 +48,7 @@ namespace App case "l": string loadName = ChooseGame(gameRunner); - if (gameRunner.GetOneGameByName(loadName) != null) + if (gameRunner.GetOneByName(loadName) != null) { Play(gameRunner, loadName); } @@ -75,7 +75,7 @@ namespace App case "d": string deleteName = ChooseGame(gameRunner); - gameRunner.DeleteGame(gameRunner.GetOneGameByName(deleteName)); + gameRunner.Remove(gameRunner.GetOneByName(deleteName)); break; case "c": @@ -130,7 +130,7 @@ namespace App string menuChoicePlay = ""; while (menuChoicePlay != "q") { - Game game = gameRunner.GetOneGameByName(name); + Game game = gameRunner.GetOneByName(name); Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" + "q... quit\n" + "h... show history\n" + @@ -158,7 +158,7 @@ namespace App { string name; Console.WriteLine("which of these games?\n(choose by name)\n>"); - foreach (Game game in gameRunner.GetAllGames()) + foreach (Game game in gameRunner.GetAll()) { Console.WriteLine(game); } diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 8d771f1..1289157 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -9,7 +9,7 @@ using Model.Players; namespace Model.Games { - public class GameRunner + public class GameRunner : IManager { private readonly IManager globalPlayerManager; private readonly IManager>>> globalDieManager; @@ -22,7 +22,7 @@ namespace Model.Games this.games = games ?? new(); } - public IEnumerable GetAllGames() => games.AsEnumerable(); + public IEnumerable GetAll() => games.AsEnumerable(); /// /// finds the game with that name and returns it @@ -31,7 +31,7 @@ namespace Model.Games /// /// a games's name /// game with said name, or null if no such game was found - public Game GetOneGameByName(string name) + public Game GetOneByName(string name) { if (!string.IsNullOrWhiteSpace(name)) { @@ -46,15 +46,16 @@ namespace Model.Games /// /// a game to save /// - public void SaveGame(Game game) + public Game Add(Game game) { if (game != null) { games.Remove(games.FirstOrDefault(g => g.Name == game.Name)); // will often be an update: if game with that name exists, it is removed, else, nothing happens above games.Add(game); + return game; } - + return null; } /// @@ -64,14 +65,19 @@ namespace Model.Games public void StartNewGame(string name, PlayerManager playerManager, IEnumerable> dice) { Game game = new(name, playerManager, dice); - SaveGame(game); + Add(game); } - public void DeleteGame(Game game) + public void Remove(Game game) { games.Remove(game); } + public Game Update(Game oldGame, Game newGame) + { + return Add(newGame); + } + /// /// plays one turn of the game /// -- 2.36.3 From c59eaceaff19f9973949708176745f77c4676474 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 28 Sep 2022 21:01:16 +0200 Subject: [PATCH 2/2] :recycle: Refactor CRUD on players and die into Manager properties --- Sources/App/Program.cs | 14 +++---- Sources/Model/Games/GameRunner.cs | 69 ++----------------------------- 2 files changed, 11 insertions(+), 72 deletions(-) diff --git a/Sources/App/Program.cs b/Sources/App/Program.cs index 95cc927..e00de33 100644 --- a/Sources/App/Program.cs +++ b/Sources/App/Program.cs @@ -56,7 +56,7 @@ namespace App case "n": - if (!gameRunner.GetGlobalDiceGroups().Any()) + if (!gameRunner.GlobalDieManager.GetAll().Any()) { Console.WriteLine("make at least one dice group first, then try again"); break; @@ -114,7 +114,7 @@ namespace App newGroupDice.Add(die); } } - gameRunner.AddGlobalDiceGroup(newGroupName, newGroupDice); + gameRunner.GlobalDieManager.Add(new KeyValuePair>>(newGroupName, newGroupDice)); break; default: @@ -227,7 +227,7 @@ namespace App List> result = new(); Console.WriteLine("add dice to the game"); Console.WriteLine("all known dice or groups of dice:"); - foreach ((string name, IEnumerable> dice) in gameRunner.GetGlobalDiceGroups()) + foreach ((string name, IEnumerable> dice) in gameRunner.GlobalDieManager.GetAll()) { Console.WriteLine($"{name} -- {dice}"); } @@ -239,7 +239,7 @@ namespace App // no checks, this is temporary if (!menuChoiceDice.Equals("ok")) { - IEnumerable> chosenDice = gameRunner.GetOneGlobalDiceGroupByName(menuChoiceDice).Value; + IEnumerable> chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value; foreach (AbstractDie die in chosenDice) { result.Add(die); @@ -253,7 +253,7 @@ namespace App PlayerManager result = new(); Console.WriteLine("add players to the game"); Console.WriteLine("all known players:"); - foreach (Player player in gameRunner.GetGlobalPlayers()) + foreach (Player player in gameRunner.GlobalPlayerManager.GetAll()) { Console.WriteLine(player); } @@ -265,10 +265,10 @@ namespace App if (!menuChoicePlayers.Equals("ok")) { Player player = new(menuChoicePlayers); - if (!gameRunner.GetGlobalPlayers().Contains(player)) + if (!gameRunner.GlobalPlayerManager.GetAll().Contains(player)) { // if the player didn't exist, now it does... this is temporary - gameRunner.AddGlobalPlayer(player); + gameRunner.GlobalPlayerManager.Add(player); } // almost no checks, this is temporary result.Add(player); diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 1289157..866bacb 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -11,14 +11,14 @@ namespace Model.Games { public class GameRunner : IManager { - private readonly IManager globalPlayerManager; - private readonly IManager>>> globalDieManager; + public IManager GlobalPlayerManager { get; private set; } + public IManager>>> GlobalDieManager { get; private set; } private readonly List games; public GameRunner(IManager globalPlayerManager, IManager>>> globalDieManager, List games) { - this.globalPlayerManager = globalPlayerManager; - this.globalDieManager = globalDieManager; + GlobalPlayerManager = globalPlayerManager; + GlobalDieManager = globalDieManager; this.games = games ?? new(); } @@ -88,66 +88,5 @@ namespace Model.Games game.PerformTurn(current); game.PrepareNextPlayer(current); } - - public void AddGlobalPlayer(Player player) - { - globalPlayerManager.Add(player); - } - - public IEnumerable GetGlobalPlayers() - { - return globalPlayerManager.GetAll(); - } - - public Player GetOneGlobalPlayerByName(string name) - { - return globalPlayerManager.GetOneByName(name); - } - - public void UpdateGlobalPlayer(Player oldPlayer, Player newPlayer) - { - globalPlayerManager.Update(oldPlayer, newPlayer); - } - - public void DeleteGlobalPlayer(Player oldPlayer) - { - globalPlayerManager.Remove(oldPlayer); - } - - public void AddGlobalDiceGroup(string name, IEnumerable> dice) - { - globalDieManager.Add(new KeyValuePair>>(name, dice)); - } - - public IEnumerable>>> GetGlobalDiceGroups() - { - return globalDieManager.GetAll(); - } - - public KeyValuePair>> GetOneGlobalDiceGroupByName(string name) - { - return globalDieManager.GetOneByName(name); - } - - /// - /// only updates names - /// - /// old name - /// new name - public void UpdateGlobalDiceGroup(string oldName, string newName) - { - KeyValuePair>> oldDiceGroup = GetOneGlobalDiceGroupByName(oldName); - KeyValuePair>> newDiceGroup = new(newName, oldDiceGroup.Value); - globalDieManager.Update(oldDiceGroup, newDiceGroup); - } - - /// - /// will remove those dice groups from other games, potentially breaking them - /// - /// - public void DeleteGlobalDiceGroup(string oldName) - { - globalDieManager.Remove(GetOneGlobalDiceGroupByName(oldName)); - } } } -- 2.36.3