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 : IManager { 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 GetAll() => 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 GetOneByName(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 -- does not allow copies yet: if a game with the same name exists, it is overwritten /// /// a game to save /// 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; } /// /// creates a new game /// /// public void StartNewGame(string name, PlayerManager playerManager, IEnumerable> dice) { Game game = new(name, playerManager, dice); Add(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 /// /// the game from which a turn will be played public static void PlayGame(Game game) { 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)); } } }