public Builder setPopulation(PopulationType populationType, int count) { if (count == 0) { Populations.Remove(populationType); return(this); } if (Populations.ContainsKey(populationType)) { Populations[populationType] = count; } else { Populations.Add(populationType, count); } return(this); }
/// <summary> /// This is for deserializing our unified building json. If fetching from the REST API you should instead use the builder /// </summary> public static Building FromJson(JObject jObject, List <Material> allMaterials) { try { string ticker = jObject.GetValue("Ticker").ToObject <string>(); string name = jObject.GetValue("Name").ToObject <string>(); int area = jObject.GetValue("Area").ToObject <int>(); string expertise = jObject.GetValue("Expertise").ToObject <string>(); Dictionary <PopulationType, int> populations = ((JObject)jObject.GetValue("Populations")).Properties().ToDictionary(property => PopulationType.Parse(property.Name), property => property.Value.ToObject <int>()); Dictionary <Material, int> constructionCost = ((JObject)jObject.GetValue("ConstructionCosts")).Properties().ToDictionary(property => allMaterials.Where(mat => mat.Ticker.Equals(property.Name, StringComparison.OrdinalIgnoreCase)).First(), property => property.Value.ToObject <int>()); return(new Building(ticker, name, area, expertise, populations, constructionCost)); } catch (Exception ex) when(ex is NullReferenceException || ex is ArgumentException || ex is FormatException || ex is JsonSerializationException) { throw new JsonSchemaException(null, ex); } }