public IActionResult GetContry(string name) { int?contryId = ContryService.GetIdByName(name); if (contryId == null) { return(NotFound()); } IQueryable <Population> populations = ContryService.GetPopulationOfContry(contryId.Value, out Region region); string json; using (var stream = new MemoryStream()) { using (var writer = new Utf8JsonWriter(stream)) { writer.WriteStartObject(); writer.WriteString("populations", ((long)populations.Sum(p => p.PopLevel)).ToString()); writer.WriteString("money", ((long)populations.Sum(p => p.Money)).ToString()); WriteResourcePrice(region, writer); WriteRecoureces(populations, writer); WriteExternalRecoureces(populations, writer); writer.WriteEndObject(); } json = Encoding.UTF8.GetString(stream.ToArray()); } return(Ok(json)); }
public IActionResult GetContryTradePartners() { List <string> contriesPop = ContryService.GetAllContryTradePartners(); string result = ""; foreach (var contry in contriesPop) { result += contry + ";"; } return(Ok(result)); }
public MapController(EcoContext context, ContryService contryService, PopulationService populationService, ResourceService resourceService) { Context = context; ContryService = contryService; PopulationService = populationService; ResourceService = resourceService; string CultureName = Thread.CurrentThread.CurrentCulture.Name; CultureInfo ci = new CultureInfo(CultureName); if (ci.NumberFormat.NumberDecimalSeparator != ".") { // Forcing use of decimal separator for numerical values ci.NumberFormat.NumberDecimalSeparator = "."; Thread.CurrentThread.CurrentCulture = ci; } }
private void WriteExternalRecoureces(IQueryable <Population> populations, Utf8JsonWriter writer) { var ownedExternalResources = ResourceService.GetExternalResourcesForContry(populations.ToList()); if (ownedExternalResources.Count == 0) { return; } foreach (var resourceType in ownedExternalResources.GroupBy(r => r.ResourceType)) { double amountORresourceType = resourceType.Sum(r => r.Amount); writer.WriteString("External " + resourceType.Key.ToString(), (amountORresourceType).ToString()); } foreach (var resourceType in ownedExternalResources.GroupBy(r => new { r.ResourceType, r.Destination })) { double amountORresourceType = resourceType.Sum(r => r.Amount); var value = resourceType.ToArray()[0]; string contryNmae = ContryService.GetContry(value.Destination.MarketDestination.Id)?.Name; writer.WriteString("External " + value.ResourceType.ToString(), "\n" + amountORresourceType.ToString() + " in " + contryNmae); } }
public IActionResult GetTradingPartner(string name) { int?contryId = ContryService.GetIdByName(name); if (contryId == null) { return(NotFound()); } IQueryable <Population> populations = ContryService.GetPopulationOfContry(contryId.Value, out Region outRegion); ICollection <Region> regions = ContryService.GetTradingPartners(populations); if (regions == null) { return(Ok()); } string json; using (var stream = new MemoryStream()) { using (var writer = new Utf8JsonWriter(stream)) { writer.WriteStartObject(); writer.WriteString("home", outRegion.CenterX + "|" + outRegion.CenterY); int i = 0; foreach (Region region in regions) { writer.WriteString(i.ToString(), region.CenterX + "|" + region.CenterY); i++; } writer.WriteEndObject(); } json = Encoding.UTF8.GetString(stream.ToArray()); } return(Ok(json)); }