コード例 #1
0
        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));
        }
コード例 #2
0
        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));
        }