public void RegeneratePicksConfig() { Random rand = new Random(); bool isGenerationValid = true; _picksConfig = new PicksConfig(); do { _picksConfig.Picks.Clear(); List <string> availablePicks = GameConfig.Names.ToList(); foreach (string name in GameConfig.Names) { List <string> otherNames = availablePicks.Where(n => !name.Equals(n)).ToList(); if (otherNames.Count == 0) { isGenerationValid = false; break; } string pick = otherNames[rand.Next(0, otherNames.Count)]; availablePicks.Remove(pick); _picksConfig.Picks[MD5Hash(name)] = MD5Hash(pick); } } while (!isGenerationValid); // Validation HashSet <string> uniquePicks = new HashSet <string>(_gameConfig.Names.Count); foreach (var pick in _picksConfig.Picks) { if (uniquePicks.Contains(pick.Value)) { throw new InvalidDataException("DUPLICATE PICKS!!!"); } uniquePicks.Add(pick.Value); } DataContractJsonSerializer picksConfigSerializer = new DataContractJsonSerializer(typeof(PicksConfig)); using (StreamWriter streamWriter = new StreamWriter(_picksFileName)) { picksConfigSerializer.WriteObject(streamWriter.BaseStream, _picksConfig); } }
public void LoadConfigurations() { DataContractJsonSerializer gameConfigSerializer = new DataContractJsonSerializer(typeof(GameConfig)); using (StreamReader streamReader = new StreamReader(_configFileName)) { _gameConfig = (GameConfig)gameConfigSerializer.ReadObject(streamReader.BaseStream); } if (File.Exists(_picksFileName)) { DataContractJsonSerializer picksConfigSerializer = new DataContractJsonSerializer(typeof(PicksConfig)); using (StreamReader streamReader = new StreamReader(_picksFileName)) { _picksConfig = (PicksConfig)picksConfigSerializer.ReadObject(streamReader.BaseStream); } } }