Beispiel #1
0
        private static Body ParseEddbBody(object response)
        {
            JObject bodyJson = ((JObject)response);

            Body Body = new Body
            {
                // General items
                EDDBID       = (long)bodyJson["id"],
                updatedat    = (long)(Dates.fromDateTimeStringToSeconds((string)bodyJson["updated_at"])),
                name         = (string)bodyJson["name"],
                Type         = BodyType.FromEDName((string)bodyJson["group_name"]),
                systemEDDBID = (long)bodyJson["system_id"],

                // Orbital data
                distance         = (decimal?)bodyJson["distance_to_arrival"],                                // Light seconds
                temperature      = (decimal?)bodyJson["surface_temperature"],                                //Kelvin
                tidallylocked    = (bool?)bodyJson["is_rotational_period_tidally_locked"] ?? false,          // Days
                rotationalperiod = (decimal?)(double?)bodyJson["rotational_period"],                         // Days
                tilt             = (decimal?)(double?)bodyJson["axis_tilt"],                                 // Degrees
                semimajoraxis    = ConstantConverters.au2ls((decimal?)(double?)bodyJson["semi_major_axis"]), // Light Seconds
                orbitalperiod    = (decimal?)(double?)bodyJson["orbital_period"],                            // Days
                periapsis        = (decimal?)(double?)bodyJson["arg_of_periapsis"],                          // Degrees
                eccentricity     = (decimal?)(double?)bodyJson["orbital_eccentricity"],
                inclination      = (decimal?)(double?)bodyJson["orbital_inclination"]                        // Degrees
            };

            if ((string)bodyJson["group_name"] == "belt")
            {
                // Not interested in asteroid belts,
                // no need to add additional information at this time.
            }

            else if ((string)bodyJson["group_name"] == "star")
            {
                // Star-specific items
                Body.stellarclass    = ((string)bodyJson["spectral_class"])?.ToUpperInvariant();
                Body.luminosityclass = ((string)bodyJson["luminosity_class"])?.ToUpperInvariant();
                Body.solarmass       = (decimal?)(double?)bodyJson["solar_masses"];
                Body.solarradius     = (decimal?)(double?)bodyJson["solar_radius"];
                Body.age             = (long?)bodyJson["age"]; // MegaYears
                Body.mainstar        = (bool?)bodyJson["is_main_star"];
                Body.landable        = false;
                Body.setStellarExtras();
            }

            else if ((string)bodyJson["group_name"] == "planet")
            {
                // Planet-specific items
                Body.planetClass    = PlanetClass.FromEDName((string)bodyJson["type_name"]);
                Body.landable       = (bool?)bodyJson["is_landable"] ?? false;
                Body.earthmass      = (decimal?)(double?)bodyJson["earth_masses"];
                Body.gravity        = (decimal?)(double?)bodyJson["gravity"]; // G's
                Body.radius         = (decimal?)bodyJson["radius"];           // Kilometers
                Body.pressure       = (decimal?)(double?)bodyJson["surface_pressure"] ?? 0;
                Body.terraformState = TerraformState.FromName((string)bodyJson["terraforming_state_name"]);
                // Per Themroc @ EDDB, "Major" and "Minor" volcanism descriptors are stripped from EDDB data.
                Body.volcanism       = Volcanism.FromName((string)bodyJson["volcanism_type_name"]);
                Body.atmosphereclass = AtmosphereClass.FromName((string)bodyJson["atmosphere_type_name"]);
                if (bodyJson["atmosphere_composition"] != null)
                {
                    List <AtmosphereComposition> atmosphereCompositions = new List <AtmosphereComposition>();
                    foreach (JObject atmoJson in bodyJson["atmosphere_composition"])
                    {
                        string  composition = (string)atmoJson["atmosphere_component_name"];
                        decimal?share       = (decimal?)atmoJson["share"];
                        if (composition != null && share != null)
                        {
                            atmosphereCompositions.Add(new AtmosphereComposition(composition, (decimal)share));
                        }
                    }
                    if (atmosphereCompositions.Count > 0)
                    {
                        atmosphereCompositions      = atmosphereCompositions.OrderByDescending(x => x.percent).ToList();
                        Body.atmospherecompositions = atmosphereCompositions;
                    }
                }
                if (bodyJson["solid_composition"] != null)
                {
                    List <SolidComposition> bodyCompositions = new List <SolidComposition>();
                    foreach (JObject bodyCompJson in bodyJson["solid_composition"])
                    {
                        string  composition = (string)bodyCompJson["solid_component_name"];
                        decimal?share       = (decimal?)bodyCompJson["share"];
                        if (composition != null && share != null)
                        {
                            bodyCompositions.Add(new SolidComposition(composition, (decimal)share));
                        }
                    }
                    if (bodyCompositions.Count > 0)
                    {
                        bodyCompositions       = bodyCompositions.OrderByDescending(x => x.percent).ToList();
                        Body.solidcompositions = bodyCompositions;
                    }
                }
                if (bodyJson["materials"] != null)
                {
                    List <MaterialPresence> Materials = new List <MaterialPresence>();
                    foreach (JObject materialJson in bodyJson["materials"])
                    {
                        Material material = Material.FromEDName((string)materialJson["material_name"]);
                        decimal? amount   = (decimal?)(double?)materialJson["share"];
                        if (material != null && amount != null)
                        {
                            Materials.Add(new MaterialPresence(material, (decimal)amount));
                        }
                    }
                    if (Materials.Count > 0)
                    {
                        Body.materials = Materials.OrderByDescending(o => o.percentage).ToList();
                    }
                }
            }

            // Rings may be an object or may be a singular
            List <Ring> rings = new List <Ring>();

            if (bodyJson["rings"] != null)
            {
                foreach (JObject ringJson in bodyJson["rings"])
                {
                    string          name             = (string)ringJson["name"];
                    RingComposition composition      = RingComposition.FromName((string)ringJson["ring_type_name"]);
                    decimal         ringMassMegaTons = (decimal)ringJson["ring_mass"];
                    decimal         innerRadiusKm    = (decimal)ringJson["ring_inner_radius"];
                    decimal         outerRadiusKm    = (decimal)ringJson["ring_outer_radius"];
                    Ring            ring             = new Ring(name, composition, ringMassMegaTons, innerRadiusKm, outerRadiusKm);
                    rings.Add(ring);
                }
            }
            if (bodyJson["ring_type_name"].HasValues)
            {
                string          name             = (string)bodyJson["name"];
                RingComposition composition      = RingComposition.FromName((string)bodyJson["ring_type_name"]);
                decimal         ringMassMegaTons = (decimal)bodyJson["ring_mass"];
                decimal         innerRadiusKm    = (decimal)bodyJson["ring_inner_radius"];
                decimal         outerRadiusKm    = (decimal)bodyJson["ring_outer_radius"];
                Ring            ring             = new Ring(name, composition, ringMassMegaTons, innerRadiusKm, outerRadiusKm);
                rings.Add(ring);
            }
            if (rings.Count > 0)
            {
                Body.rings = rings.OrderBy(o => o.innerradius).ToList();
            }

            return(Body);
        }
Beispiel #2
0
        private Body ParseStarMapBody(JObject body, string systemName)
        {
            // General items
            long?    bodyId            = (long?)body["bodyId"];
            long?    EDSMID            = (long?)body["id"];
            string   bodyname          = (string)body["name"];
            BodyType bodyType          = BodyType.FromName((string)body["type"]) ?? BodyType.None;
            decimal? distanceLs        = (decimal?)body["distanceToArrival"]; // Light Seconds
            decimal? temperatureKelvin = (long?)body["surfaceTemperature"];   // Kelvin

            // Orbital characteristics
            decimal?orbitalPeriodDays         = (decimal?)body["orbitalPeriod"];                           // Days
            decimal?semimajoraxisLs           = ConstantConverters.au2ls((decimal?)body["semiMajorAxis"]); // Light seconds
            decimal?eccentricity              = (decimal?)body["orbitalEccentricity"];
            decimal?orbitalInclinationDegrees = (decimal?)body["orbitalInclination"];                      // Degrees
            decimal?periapsisDegrees          = (decimal?)body["argOfPeriapsis"];                          // Degrees
            decimal?rotationPeriodDays        = (decimal?)body["rotationalPeriod"];                        // Days
            decimal?axialTiltDegrees          = (decimal?)body["axialTilt"];                               // Degrees

            List <IDictionary <string, object> > parents = new List <IDictionary <string, object> >();

            if (body["parents"] != null)
            {
                // Parent body types and IDs
                parents = body["parents"].ToObject <List <IDictionary <string, object> > >() ?? new List <IDictionary <string, object> >();
            }

            List <Ring> rings = new List <Ring>();

            if ((JArray)body["rings"] != null || (JArray)body["belts"] != null)
            {
                var ringsData = body["rings"] ?? body["belts"];
                if (ringsData != null)
                {
                    foreach (JObject ring in ringsData)
                    {
                        rings.Add(new Ring(
                                      (string)ring["name"],
                                      RingComposition.FromName((string)ring["type"]),
                                      (decimal)ring["mass"],
                                      (decimal)ring["innerRadius"],
                                      (decimal)ring["outerRadius"]
                                      ));
                    }
                }
            }

            if ((string)body["type"] == "Star")
            {
                // Star-specific items
                string stellarclass      = ((string)body["subType"]).Split(' ')[0]; // Splits "B (Blue-White) Star" to "B"
                int?   stellarsubclass   = null;
                string endOfStellarClass = stellarclass.ToCharArray().ElementAt(stellarclass.Length - 1).ToString();
                if (int.TryParse(endOfStellarClass, out int subclass))
                {
                    // If our stellarclass ends in a number, we need to separate the class from the subclass
                    stellarsubclass = subclass;
                    stellarclass    = stellarclass.Replace(endOfStellarClass, "");
                }

                long?   ageMegaYears      = (long?)body["age"]; // Age in megayears
                string  luminosityclass   = (string)body["luminosity"];
                decimal?absolutemagnitude = (decimal?)body["absoluteMagnitude"];
                decimal?stellarMass       = (decimal?)body["solarMasses"];
                decimal?solarradius       = (decimal?)body["solarRadius"];
                decimal radiusKm          = (decimal)(solarradius != null ? solarradius * Constants.solarRadiusMeters / 1000 : null);

                Body Body = new Body(bodyname, bodyId, parents, distanceLs, stellarclass, stellarsubclass, stellarMass,
                                     radiusKm, absolutemagnitude, ageMegaYears, temperatureKelvin, luminosityclass, semimajoraxisLs,
                                     eccentricity, orbitalInclinationDegrees, periapsisDegrees, orbitalPeriodDays, rotationPeriodDays,
                                     axialTiltDegrees, rings, true, false, systemName, null)
                {
                    EDSMID = EDSMID
                };
                DateTime updatedAt = JsonParsing.getDateTime("updateTime", body);
                Body.updatedat = updatedAt == null ? null : (long?)Dates.fromDateTimeToSeconds(updatedAt);

                return(Body);
            }

            if ((string)body["type"] == "Planet")
            {
                // Planet and moon specific items
                PlanetClass    planetClass    = PlanetClass.FromName((string)body["subType"]) ?? PlanetClass.None;
                bool?          tidallylocked  = (bool?)body["rotationalPeriodTidallyLocked"] ?? false;
                bool?          landable       = (bool?)body["isLandable"];
                decimal?       gravity        = (decimal?)body["gravity"]; // G's
                decimal?       earthmass      = (decimal?)body["earthMasses"];
                decimal?       radiusKm       = (decimal?)body["radius"];  // Kilometers
                TerraformState terraformState = TerraformState.FromName((string)body["terraformingState"]) ?? TerraformState.NotTerraformable;

                Volcanism volcanism = null;
                if ((string)body["volcanismType"] != null)
                {
                    volcanism = Volcanism.FromName((string)body["volcanismType"]);
                }

                List <AtmosphereComposition> atmosphereCompositions = new List <AtmosphereComposition>();
                if (body["atmosphereComposition"] is JObject)
                {
                    var compositions = body["atmosphereComposition"].ToObject <Dictionary <string, decimal?> >();

                    foreach (KeyValuePair <string, decimal?> compositionKV in compositions)
                    {
                        string  compositionName = compositionKV.Key;
                        decimal?share           = compositionKV.Value;
                        if (compositionName != null && share != null)
                        {
                            atmosphereCompositions.Add(new AtmosphereComposition(compositionName, (decimal)share));
                        }
                    }
                    if (atmosphereCompositions.Count > 0)
                    {
                        atmosphereCompositions = atmosphereCompositions.OrderByDescending(x => x.percent).ToList();
                    }
                }
                decimal?        pressureAtm     = (decimal?)body["surfacePressure"];
                AtmosphereClass atmosphereClass = null;
                if (((string)body["subType"]).Contains("gas giant") &&
                    (string)body["atmosphereType"] == "No atmosphere")
                {
                    // EDSM classifies any body with an empty string atmosphere property as "No atmosphere".
                    // However, gas giants also receive an empty string. Fix it, since gas giants have atmospheres.
                    atmosphereClass = AtmosphereClass.FromEDName("GasGiant");
                }
                else
                {
                    atmosphereClass = AtmosphereClass.FromName((string)body["atmosphereType"]);
                }

                List <SolidComposition> solidCompositions = new List <SolidComposition>();
                if (body["solidComposition"] is JObject)
                {
                    var compositions = body["solidComposition"].ToObject <Dictionary <string, decimal?> >();

                    foreach (KeyValuePair <string, decimal?> compositionKV in compositions)
                    {
                        string  composition = compositionKV.Key;
                        decimal?share       = compositionKV.Value;
                        if (composition != null && share != null)
                        {
                            solidCompositions.Add(new SolidComposition(composition, (decimal)share));
                        }
                    }
                    if (solidCompositions.Count > 0)
                    {
                        solidCompositions = solidCompositions.OrderByDescending(x => x.percent).ToList();
                    }
                }

                List <MaterialPresence> materials = new List <MaterialPresence>();
                if (body["materials"] is JObject)
                {
                    var materialsData = body["materials"].ToObject <Dictionary <string, decimal?> >();
                    foreach (KeyValuePair <string, decimal?> materialKV in materialsData)
                    {
                        Material material = Material.FromName(materialKV.Key);
                        decimal? amount   = materialKV.Value;
                        if (material != null && amount != null)
                        {
                            materials.Add(new MaterialPresence(material, (decimal)amount));
                        }
                    }
                    if (materials.Count > 0)
                    {
                        materials = materials.OrderByDescending(o => o.percentage).ToList();
                    }
                }
                ReserveLevel reserveLevel = ReserveLevel.FromName((string)body["reserveLevel"]) ?? ReserveLevel.None;

                DateTime updatedAt = JsonParsing.getDateTime("updateTime", body);
                Body     Body      = new Body(bodyname, bodyId, parents, distanceLs, tidallylocked, terraformState, planetClass, atmosphereClass, atmosphereCompositions, volcanism, earthmass, radiusKm, (decimal)gravity, temperatureKelvin, pressureAtm, landable, materials, solidCompositions, semimajoraxisLs, eccentricity, orbitalInclinationDegrees, periapsisDegrees, orbitalPeriodDays, rotationPeriodDays, axialTiltDegrees, rings, reserveLevel, true, true, systemName, null)
                {
                    EDSMID    = EDSMID,
                    updatedat = updatedAt == null ? null : (long?)Dates.fromDateTimeToSeconds(updatedAt)
                };

                return(Body);
            }


            return(null);
        }
Beispiel #3
0
        private static Body ParseStarMapBody(JObject body, string system)
        {
            Body Body = new Body
            {
                // General items
                EDSMID      = (long?)body["id"],
                name        = (string)body["name"],
                systemname  = system,
                Type        = BodyType.FromName((string)body["type"]) ?? BodyType.None,
                distance    = (decimal?)body["distanceToArrival"], // Light Seconds
                temperature = (long?)body["surfaceTemperature"],   // Kelvin

                // Orbital characteristics
                orbitalperiod    = (decimal?)body["orbitalPeriod"],                           // Days
                semimajoraxis    = ConstantConverters.au2ls((decimal?)body["semiMajorAxis"]), // Light seconds
                eccentricity     = (decimal?)body["orbitalEccentricity"],
                inclination      = (decimal?)body["orbitalInclination"],                      // Degrees
                periapsis        = (decimal?)body["argOfPeriapsis"],                          // Degrees
                rotationalperiod = (decimal?)body["rotationalPeriod"],                        // Days
                tidallylocked    = (bool?)body["rotationalPeriodTidallyLocked"] ?? false,
                tilt             = (decimal?)body["axialTilt"]                                // Degrees
            };

            if ((string)body["type"] == "Belt")
            {
                // Not interested in asteroid belts,
                // no need to add additional information at this time.
            }

            if ((string)body["type"] == "Star")
            {
                // Star-specific items
                Body.stellarclass      = ((string)body["subType"]).Split(' ')[0]; // Splits "B (Blue-White) Star" to "B"
                Body.mainstar          = (bool?)body["isMainStar"];
                Body.age               = (long?)body["age"];                      // Age in megayears
                Body.luminosityclass   = (string)body["luminosity"];
                Body.absoluteMagnitude = (decimal?)body["absoluteMagnitude"];
                Body.solarmass         = (decimal?)body["solarMasses"];
                Body.solarradius       = (decimal?)body["solarRadius"];
                Body.landable          = false;
                Body.setStellarExtras();
            }

            if ((string)body["type"] == "Planet")
            {
                // Planet-specific items
                Body.planetClass    = PlanetClass.FromName((string)body["subType"]) ?? PlanetClass.None;
                Body.landable       = (bool?)body["isLandable"];
                Body.gravity        = (decimal?)body["gravity"]; // G's
                Body.earthmass      = (decimal?)body["earthMasses"];
                Body.radius         = (decimal?)body["radius"];  // Kilometers
                Body.terraformState = TerraformState.FromName((string)body["terraformingState"]) ?? TerraformState.NotTerraformable;
                if ((string)body["volcanismType"] != null)
                {
                    Body.volcanism = Volcanism.FromName((string)body["volcanismType"]);
                }

                if (body["atmosphereComposition"] is JObject)
                {
                    List <AtmosphereComposition> atmosphereCompositions = new List <AtmosphereComposition>();
                    var compositions = body["atmosphereComposition"].ToObject <Dictionary <string, decimal?> >();

                    foreach (KeyValuePair <string, decimal?> compositionKV in compositions)
                    {
                        string  compositionName = compositionKV.Key;
                        decimal?share           = compositionKV.Value;
                        if (compositionName != null && share != null)
                        {
                            atmosphereCompositions.Add(new AtmosphereComposition(compositionName, (decimal)share));
                        }
                    }
                    if (atmosphereCompositions.Count > 0)
                    {
                        atmosphereCompositions      = atmosphereCompositions.OrderByDescending(x => x.percent).ToList();
                        Body.atmospherecompositions = atmosphereCompositions;
                    }
                }
                Body.pressure = (decimal?)body["surfacePressure"];
                if (((string)body["subType"]).Contains("gas giant") &&
                    (string)body["atmosphereType"] == "No atmosphere")
                {
                    // EDSM classifies any body with an empty string atmosphere property as "No atmosphere".
                    // However, gas giants also receive an empty string. Fix it, since gas giants have atmospheres.
                    Body.atmosphereclass = AtmosphereClass.FromEDName("GasGiant");
                }
                else
                {
                    Body.atmosphereclass = AtmosphereClass.FromName((string)body["atmosphereType"]);
                }

                if (body["solidComposition"] is JObject)
                {
                    List <SolidComposition> bodyCompositions = new List <SolidComposition>();
                    var compositions = body["solidComposition"].ToObject <Dictionary <string, decimal?> >();

                    foreach (KeyValuePair <string, decimal?> compositionKV in compositions)
                    {
                        string  composition = compositionKV.Key;
                        decimal?share       = compositionKV.Value;
                        if (composition != null && share != null)
                        {
                            bodyCompositions.Add(new SolidComposition(composition, (decimal)share));
                        }
                    }
                    if (bodyCompositions.Count > 0)
                    {
                        bodyCompositions       = bodyCompositions.OrderByDescending(x => x.percent).ToList();
                        Body.solidcompositions = bodyCompositions;
                    }
                }

                if (body["materials"] is JObject)
                {
                    List <MaterialPresence> Materials = new List <MaterialPresence>();
                    var materials = body["materials"].ToObject <Dictionary <string, decimal?> >();
                    foreach (KeyValuePair <string, decimal?> materialKV in materials)
                    {
                        Material material = Material.FromName(materialKV.Key);
                        decimal? amount   = materialKV.Value;
                        if (material != null && amount != null)
                        {
                            Materials.Add(new MaterialPresence(material, (decimal)amount));
                        }
                    }
                    if (Materials.Count > 0)
                    {
                        Body.materials = Materials.OrderByDescending(o => o.percentage).ToList();
                    }
                }
            }

            if ((JArray)body["rings"] != null || (JArray)body["belts"] != null)
            {
                var rings = body["rings"] ?? body["belts"];
                if (rings != null)
                {
                    List <Ring> Rings = new List <Ring>();
                    foreach (JObject ring in rings)
                    {
                        Rings.Add(new Ring(
                                      (string)ring["name"],
                                      RingComposition.FromName((string)ring["type"]),
                                      (decimal)ring["mass"],
                                      (decimal)ring["innerRadius"],
                                      (decimal)ring["outerRadius"]
                                      ));
                    }
                    Body.rings = Rings;
                }
            }
            Body.reserveLevel = ReserveLevel.FromName((string)body["reserveLevel"]) ?? ReserveLevel.None;

            DateTime updatedAt = DateTime.SpecifyKind(DateTime.Parse((string)body["updateTime"]), DateTimeKind.Utc);

            Body.updatedat = updatedAt == null ? null : (long?)(updatedAt.Subtract(new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;

            return(Body);
        }