internal static CardGameSettings Create(IDependencyResolver dependencyResolver) { Guard.AgainstNull(dependencyResolver, nameof(dependencyResolver)); var fileName = Constants.SettingsFilePath; var settings = new CardGameSettings(); settings.dependencyResolver = dependencyResolver; if (File.Exists(fileName)) { try { var storedSettings = LoadStoredSettings(settings, fileName); return(storedSettings); } catch (Exception) // occurs when the file has been modified outside of the program, likely by a user { settings = new CardGameSettings(); settings.dependencyResolver = dependencyResolver; System.Console.WriteLine("Error: Settings.xml file has been corrupted. Loading default settings."); System.Console.WriteLine(); } } settings.LoadDefaultSettings(); settings.SaveFile(fileName); return(settings); }
private static CardGameSettings LoadStoredSettings(CardGameSettings gameSettings, string fileName) { var doc = XDocument.Load(fileName); var settingsElement = doc.Element("Settings"); var properties = typeof(CardGameSettings).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(p => p.PropertyType.IsValueType).ToList(); settingsElement.Attributes().ToList().ForEach(attr => { var property = properties.FirstOrDefault(p => p.Name == attr.Name); if (property != null) { gameSettings.SetValue(property, attr.Value); } }); gameSettings.Cards = new CardCollection(); gameSettings.suitData = new Dictionary <ISuit, CardMemberData>(); gameSettings.faceValueData = new Dictionary <IFaceValue, CardMemberData>(); gameSettings.specialCards = new List <SpecialCardData>(); foreach (var faceValueElement in settingsElement.Element("Suits").Elements("Suit")) { gameSettings.AddSuit(faceValueElement.Attribute("Name").Value, Int32.Parse(faceValueElement.Attribute("Rank").Value)); } foreach (var faceValueElement in settingsElement.Element("FaceValues").Elements("FaceValue")) { gameSettings.AddFaceValue(faceValueElement.Attribute("Value").Value, Int32.Parse(faceValueElement.Attribute("Rank").Value)); } foreach (var specialCardElement in settingsElement.Element("SpecialCards").Elements("Card")) { var suitAttribute = specialCardElement.Attribute("Suit").Value; var faceValueAttribute = specialCardElement.Attribute("Value").Value; var scoreChange = Int32.Parse(specialCardElement.Attribute("ScoreChange").Value); var count = Int32.Parse(specialCardElement.Attribute("Count").Value); var card = gameSettings.GetNewCard( faceValueAttribute == Constants.NullValue ? null : gameSettings.GetNewFaceValue(faceValueAttribute), suitAttribute == Constants.NullValue ? null : gameSettings.GetNewSuit(suitAttribute)); var data = new SpecialCardData(card, scoreChange, count); gameSettings.specialCards.Add(data); } gameSettings.LoadCards(); return(gameSettings); }
private void LoadSettings() { var dependencyResolver = new NinjectDependencyResolver(this.Kernel); var settings = CardGameSettings.Create(dependencyResolver); this.Bind <PlayerSettings>().ToConstant( new PlayerSettings() { MinimumPlayerCount = settings.PlayerCountMinimum, MaximumPlayerCount = settings.PlayerCountMaximum, MinimumNameLength = settings.PlayerNameMinimumLength, MaximumNameLength = settings.PlayerNameMaximumLength }); this.Bind <KeyInputSettings>().ToConstant( new KeyInputSettings() { DrawCardKey = settings.DrawCardKey, ContinueRoundKey = settings.ContinueRoundKey, ExitKey = settings.ExitKey }); this.Bind <DeckSettings>().ToConstant( new DeckSettings() { Cards = settings.Cards }); this.Bind <ScoringSettings>().ToConstant( new ScoringSettings() { GetCardValueDelegate = settings.GetCardValueDelegate, WinnerBonusPointTotal = settings.HighestCardScoreChange }); this.Bind <VictoryCheckerSettings>().ToConstant( new VictoryCheckerSettings() { MinimumVictoryScoreTotal = settings.MinimumVictoryScoreTotal, RequiredScoreLead = settings.RequiredVictoryScoreLead, ScoreLeadMustBeExactValue = settings.VictoryScoreLeadMustBeAnExactValue }); }