From 8f42e7577ebe819a151f8e1abaad0b31a258e685 Mon Sep 17 00:00:00 2001 From: "alexis.drai" Date: Wed, 28 Sep 2022 11:32:04 +0200 Subject: [PATCH 1/4] Add comments, break keyboard..? --- Sources/Model/Games/GameRunner.cs | 147 +++++++++++++++++------------- 1 file changed, 83 insertions(+), 64 deletions(-) diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index a3aefdc..b4acd01 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -1,64 +1,83 @@ -using System; -using System.Collections.Generic; -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 IManager globalPlayerManager; - private readonly IManager>>> globalDieManager; - private readonly List games; - - public GameRunner(IManager globalPlayerManager, IManager>>> globalDieManager, List games) - { - this.globalPlayerManager = globalPlayerManager; - this.globalDieManager = globalDieManager; - this.games = games ?? new(); - } - - public IEnumerable GetAllGames() => games.AsEnumerable(); - - /// - /// finds the game with that name and returns it - ///
- /// that copy does not belong to this gamerunner's games, so it should not be modified - ///
- /// a games's name - /// game with said name, or null if no such game was found - public Game GetOneGameByName(string name) - { - if (!string.IsNullOrWhiteSpace(name)) - { - Game result = games.FirstOrDefault(g => g.Name == name); - return result; // may return null - } - throw new ArgumentException("param should not be null or blank", nameof(name)); - } - - public void SaveGame(Game game) - { - throw new NotSupportedException(); - } - - public Game LoadGame(string name) - { - throw new NotSupportedException(); - } - - public void StartNewGame() - { - throw new NotSupportedException(); - } - - public void DeleteGame(Game game) - { - throw new NotSupportedException(); - } - } -} +using System; +using System.Collections.Generic; +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 IManager globalPlayerManager; + private readonly IManager>>> globalDieManager; + private readonly List games; + + public GameRunner(IManager globalPlayerManager, IManager>>> globalDieManager, List games) + { + this.globalPlayerManager = globalPlayerManager; + this.globalDieManager = globalDieManager; + this.games = games ?? new(); + } + + public IEnumerable GetAllGames() => games.AsEnumerable(); + + /// + /// finds the game with that name and returns it + ///
+ /// that copy does not belong to this gamerunner's games, so it should not be modified + ///
+ /// a games's name + /// game with said name, or null if no such game was found + public Game GetOneGameByName(string name) + { + if (!string.IsNullOrWhiteSpace(name)) + { + Game result = games.FirstOrDefault(g => g.Name == name); + return result; // may return null + } + throw new ArgumentException("param should not be null or blank", nameof(name)); + } + + /// + /// saves a given game + /// + /// a game to save + /// + public void SaveGame(Game game) + { + throw new NotSupportedException(); + } + + /// + /// loads a game by name + /// + /// name of game to be loaded + /// loaded game + /// + public Game LoadGame(string name) + { + throw new NotSupportedException(); + } + + /// + /// creates a new game + /// + /// + public void StartNewGame() + { + throw new NotSupportedException(); + } + + public void DeleteGame(Game game) + { + throw new NotSupportedException(); + } + private void PlayGame(Game game) + { + throw new NotSupportedException(); + } + } +} From 2ee91862cf0707f0980b10969b74495e983455cd Mon Sep 17 00:00:00 2001 From: "alexis.drai" Date: Wed, 28 Sep 2022 12:00:16 +0200 Subject: [PATCH 2/4] :construction: WIP --- Sources/Model/Games/GameRunner.cs | 35 +++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index b4acd01..1e80c4d 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -48,36 +48,53 @@ namespace Model.Games /// public void SaveGame(Game game) { - throw new NotSupportedException(); + 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); + } + } /// - /// loads a game by name + /// loads a game by name to start playing again /// /// name of game to be loaded /// loaded game /// - public Game LoadGame(string name) + public void LoadGame(string name) { - throw new NotSupportedException(); + Game game = GetOneGameByName(name); + PlayGame(game); } /// - /// creates a new game + /// creates a new game -- copies are OK /// /// - public void StartNewGame() + public void StartNewGame(string name, PlayerManager playerManager, IEnumerable> dice) { - throw new NotSupportedException(); + Game game = new(name, playerManager, dice); + SaveGame(game); + PlayGame(game); } public void DeleteGame(Game game) { - throw new NotSupportedException(); + games.Remove(game); } + private void PlayGame(Game game) { - throw new NotSupportedException(); + while(true) + { + Player current = game.GetWhoPlaysNow(); + game.PerformTurn(current); + game.PrepareNextPlayer(current); + // TODO + break; + } } } } From daf468463ce13254d931ace02b0cc3aeef29b37c Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 28 Sep 2022 19:11:39 +0200 Subject: [PATCH 3/4] :necktie: Finish GameRunner class for now --- Sources/Model/Games/GameRunner.cs | 88 +++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 1e80c4d..a477c74 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -42,19 +42,19 @@ namespace Model.Games } /// - /// saves a given game + /// saves a given game -- does not allow copies yet: if a game with the same name exists, it is overwritten /// /// a game to save /// public void SaveGame(Game game) { - if(game != null) + 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); } - + } /// @@ -66,18 +66,16 @@ namespace Model.Games public void LoadGame(string name) { Game game = GetOneGameByName(name); - PlayGame(game); } /// - /// creates a new game -- copies are OK + /// creates a new game /// /// public void StartNewGame(string name, PlayerManager playerManager, IEnumerable> dice) { Game game = new(name, playerManager, dice); SaveGame(game); - PlayGame(game); } public void DeleteGame(Game game) @@ -85,16 +83,76 @@ namespace Model.Games games.Remove(game); } - private void PlayGame(Game game) + /// + /// plays one turn of the game + /// + /// the game from which a turn will be played + public void PlayGame(Game game) { - while(true) - { - Player current = game.GetWhoPlaysNow(); - game.PerformTurn(current); - game.PrepareNextPlayer(current); - // TODO - break; - } + Player current = game.GetWhoPlaysNow(); + 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)); } } } From 855d697572fa1808acad1f70f6e01822e73fa46e Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 28 Sep 2022 19:16:53 +0200 Subject: [PATCH 4/4] :adhesive_bandage: Make method static, remove Load PlayGame was never using any instance data, so it could be made static for performance gains --- Sources/Model/Games/GameRunner.cs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index a477c74..8d771f1 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -57,17 +57,6 @@ namespace Model.Games } - /// - /// loads a game by name to start playing again - /// - /// name of game to be loaded - /// loaded game - /// - public void LoadGame(string name) - { - Game game = GetOneGameByName(name); - } - /// /// creates a new game /// @@ -87,7 +76,7 @@ namespace Model.Games /// plays one turn of the game /// /// the game from which a turn will be played - public void PlayGame(Game game) + public static void PlayGame(Game game) { Player current = game.GetWhoPlaysNow(); game.PerformTurn(current);