public void TestDataDefinitionUnknownName()
        {
            Commodity commodity = CommodityDefinitions.FromName("$MagicStuff_Name;");

            Assert.AreEqual("Magic Stuff", commodity.name);
            Assert.AreEqual(0, commodity.EDDBID);
        }
        public void TestDataDefinitionReactiveArmour()
        {
            Commodity commodity = CommodityDefinitions.FromName("$ReactiveArmour_Name;");

            Assert.AreEqual(commodity.name, "Reactive Armour");
            Assert.IsNotNull(commodity.EDDBID);
        }
Beispiel #3
0
        public void TestMalformedCommodityName()
        {
            string malformedCommodityName = "I gotta quote\" and a backslash\\, I'm really bad.";
            var    badCommoditity         = CommodityDefinitions.FromName(malformedCommodityName);

            Assert.AreEqual(badCommoditity.name, malformedCommodityName);
        }
Beispiel #4
0
        // Obtain the list of commodities from the profile
        public static List <Commodity> CommoditiesFromProfile(dynamic json)
        {
            List <Commodity> Commodities = new List <Commodity>();

            if (json["lastStarport"] != null && json["lastStarport"]["commodities"] != null)
            {
                List <Commodity> commodityErrors = new List <Commodity>();
                foreach (dynamic commodity in json["lastStarport"]["commodities"])
                {
                    dynamic   commodityJson = commodity.Value;
                    Commodity Commodity     = new Commodity();
                    Commodity eddiCommodity = CommodityDefinitions.CommodityFromEliteID((long)commodity["id"]);
                    if (eddiCommodity == null)
                    {
                        // If we fail to identify the commodity by EDID, try using the EDName.
                        eddiCommodity = CommodityDefinitions.FromName((string)commodity["name"]);
                    }

                    Commodity.EDName        = (string)commodity["name"];
                    Commodity.name          = (string)commodity["locName"];
                    Commodity.category      = ((string)commodity["categoryname"]).Trim();
                    Commodity.avgprice      = (int)commodity["meanPrice"];
                    Commodity.buyprice      = (int)commodity["buyPrice"];
                    Commodity.stock         = (int)commodity["stock"];
                    Commodity.stockbracket  = (dynamic)commodity["stockBracket"];
                    Commodity.sellprice     = (int)commodity["sellPrice"];
                    Commodity.demand        = (int)commodity["demand"];
                    Commodity.demandbracket = (dynamic)commodity["demandBracket"];

                    List <string> StatusFlags = new List <string>();
                    foreach (dynamic statusFlag in commodity["statusFlags"])
                    {
                        StatusFlags.Add((string)statusFlag);
                    }
                    Commodity.StatusFlags = StatusFlags;
                    Commodities.Add(Commodity);

                    if (eddiCommodity == null || eddiCommodity.EDName != Commodity.EDName || eddiCommodity.name != Commodity.name ||
                        eddiCommodity.category != Commodity.category)
                    {
                        if (eddiCommodity.name != "Limpet")
                        {
                            // Unknown commodity; batch and report so that we can update the definitions
                            commodityErrors.Add(Commodity);
                        }
                    }
                }

                if (commodityErrors.Count() > 0)
                {
                    Logging.Report("Commodity definition errors", JsonConvert.SerializeObject(commodityErrors));
                }
            }

            return(Commodities);
        }
        // Obtain the list of commodities from the profile
        public static List <Commodity> CommoditiesFromProfile(dynamic json)
        {
            List <Commodity> Commodities = new List <Commodity>();

            if (json["lastStarport"] != null && json["lastStarport"]["commodities"] != null)
            {
                List <Commodity> commodityErrors = new List <Commodity>();
                foreach (dynamic commodity in json["lastStarport"]["commodities"])
                {
                    dynamic   commodityJson = commodity.Value;
                    Commodity Commodity     = new Commodity();
                    Commodity eddiCommodity = CommodityDefinitions.CommodityFromEliteID((long)commodity["id"]);

                    Commodity.EDName        = (string)commodity["name"];
                    Commodity.name          = (string)commodity["locName"];
                    Commodity.category      = ((string)commodity["categoryname"]).Trim();
                    Commodity.avgprice      = (int)commodity["meanPrice"];
                    Commodity.buyprice      = (int)commodity["buyPrice"];
                    Commodity.stock         = (int)commodity["stock"];
                    Commodity.stockbracket  = (dynamic)commodity["stockBracket"];
                    Commodity.sellprice     = (int)commodity["sellPrice"];
                    Commodity.demand        = (int)commodity["demand"];
                    Commodity.demandbracket = (dynamic)commodity["demandBracket"];

                    List <string> StatusFlags = new List <string>();
                    foreach (dynamic statusFlag in commodity["statusFlags"])
                    {
                        StatusFlags.Add((string)statusFlag);
                    }
                    Commodity.StatusFlags = StatusFlags;
                    Commodities.Add(Commodity);

                    if (eddiCommodity == null || eddiCommodity.EDName != Commodity.EDName || eddiCommodity.name != Commodity.name ||
                        eddiCommodity.category != Commodity.category)
                    {
                        if (eddiCommodity.name != "Limpet")
                        {
                            commodityErrors.Add(Commodity);
                        }
                    }
                }

                if (commodityErrors.Count() > 0)
                {
                    Logging.Warn("Commodity definition errors: " + JsonConvert.SerializeObject(commodityErrors));
                    SpeechService.Instance.Say(null, "E-D-D-I commodity definition errors found.  Please forward your log to developers.", false);
                }
            }

            return(Commodities);
        }
Beispiel #6
0
        private void handleLimpetPurchasedEvent(LimpetPurchasedEvent @event)
        {
            Cargo limpets = GetCurrentShip().cargo.Find(c => c.commodity.name == "Limpet");

            if (limpets == null)
            {
                // No limpets so create an entry
                limpets           = new Cargo();
                limpets.commodity = CommodityDefinitions.FromName("Limpet");
                limpets.price     = @event.price;
                limpets.amount    = 0;
                GetCurrentShip().cargo.Add(limpets);
            }
            limpets.amount += @event.amount;
        }
        // Obtain the list of commodities from the profile
        public static List <Commodity> CommoditiesFromProfile(dynamic json)
        {
            List <Commodity> Commodities = new List <Commodity>();

            if (json["lastStarport"] != null && json["lastStarport"]["commodities"] != null)
            {
                foreach (dynamic commodity in json["lastStarport"]["commodities"])
                {
                    dynamic   commodityJson = commodity.Value;
                    Commodity Commodity     = CommodityDefinitions.CommodityFromEliteID((long)commodity["id"]);
                    if (Commodity == null || Commodity.name == null)
                    {
                        Commodity          = new Commodity();
                        Commodity.EDName   = (string)commodity["name"];
                        Commodity.category = (string)commodity["categoryName"];
                    }
                    Commodity.avgprice      = (int)commodity["meanPrice"];
                    Commodity.buyprice      = (int)commodity["buyPrice"];
                    Commodity.stock         = (int)commodity["stock"];
                    Commodity.stockbracket  = (dynamic)commodity["stockBracket"];
                    Commodity.sellprice     = (int)commodity["sellPrice"];
                    Commodity.demand        = (int)commodity["demand"];
                    Commodity.demandbracket = (dynamic)commodity["demandBracket"];

                    List <string> StatusFlags = new List <string>();
                    foreach (dynamic statusFlag in commodity["statusFlags"])
                    {
                        StatusFlags.Add((string)statusFlag);
                    }
                    Commodity.StatusFlags = StatusFlags;
                    Commodities.Add(Commodity);
                }
            }

            return(Commodities);
        }
Beispiel #8
0
        public static Ship ShipFromJson(JObject json)
        {
            if (json == null)
            {
                return(null);
            }

            Ship Ship = ShipDefinitions.FromEDModel((string)json.GetValue("name"));

            // We want to return a basic ship if the parsing fails so wrap this
            try
            {
                Ship.raw = json.ToString(Formatting.None);
                /// As of 2.3.0 Frontier no longer supplies module information for ships other than the active ship.
                /// 'Health' is only given in the complete un-summarized json.
                /// Get the raw only if it is complete.
                if (!(Ship.raw).Contains("health"))
                {
                    Ship.raw = null;
                }

                Ship.LocalId = json.GetValue("id").Value <int>();
                Ship.name    = (string)json.GetValue("shipName");
                Ship.ident   = (string)json.GetValue("shipID");

                Ship.value         = (long)(json["value"]?["hull"] ?? 0) + (long)(json["value"]?["modules"] ?? 0);
                Ship.cargocapacity = 0;

                // Be sensible with health - round it unless it's very low
                decimal Health = (decimal)(json["health"]?["hull"] ?? 1000000M) / 10000M;
                if (Health < 5)
                {
                    Ship.health = Math.Round(Health, 1);
                }
                else
                {
                    Ship.health = Math.Round(Health);
                }

                if (json["modules"] != null)
                {
                    // Obtain the internals
                    Ship.bulkheads        = ModuleFromJson("Armour", (JObject)json["modules"]["Armour"]);
                    Ship.powerplant       = ModuleFromJson("PowerPlant", (JObject)json["modules"]["PowerPlant"]);
                    Ship.thrusters        = ModuleFromJson("MainEngines", (JObject)json["modules"]["MainEngines"]);
                    Ship.frameshiftdrive  = ModuleFromJson("FrameShiftDrive", (JObject)json["modules"]["FrameShiftDrive"]);
                    Ship.lifesupport      = ModuleFromJson("LifeSupport", (JObject)json["modules"]["LifeSupport"]);
                    Ship.powerdistributor = ModuleFromJson("PowerDistributor", (JObject)json["modules"]["PowerDistributor"]);
                    Ship.sensors          = ModuleFromJson("Radar", (JObject)json["modules"]["Radar"]);
                    Ship.fueltank         = ModuleFromJson("FuelTank", (JObject)json["modules"]["FuelTank"]);
                    if (Ship.fueltank != null)
                    {
                        Ship.fueltankcapacity = (decimal)Math.Pow(2, Ship.fueltank.@class);
                    }
                    Ship.fueltanktotalcapacity = Ship.fueltankcapacity;
                    Ship.paintjob = (string)(json["modules"]?["PaintJob"]?["name"] ?? null);

                    // Obtain the hardpoints.  Hardpoints can come in any order so first parse them then second put them in the correct order
                    Dictionary <string, Hardpoint> hardpoints = new Dictionary <string, Hardpoint>();
                    foreach (JProperty module in json["modules"])
                    {
                        if (module.Name.Contains("Hardpoint"))
                        {
                            hardpoints.Add(module.Name, HardpointFromJson(module));
                        }
                    }
                    foreach (string size in HARDPOINT_SIZES)
                    {
                        for (int i = 1; i < 12; i++)
                        {
                            Hardpoint hardpoint;
                            hardpoints.TryGetValue(size + "Hardpoint" + i, out hardpoint);
                            if (hardpoint != null)
                            {
                                Ship.hardpoints.Add(hardpoint);
                            }
                        }
                    }

                    // Obtain the compartments
                    foreach (dynamic module in json["modules"])
                    {
                        if (module.Name.Contains("Slot"))
                        {
                            Compartment compartment = CompartmentFromJson(module);
                            string      moduleName  = compartment.module?.name ?? "";
                            if (moduleName == "Fuel Tank")
                            {
                                Ship.fueltanktotalcapacity += (decimal)Math.Pow(2, compartment.module.@class);
                            }
                            if (moduleName == "Cargo Rack")
                            {
                                Ship.cargocapacity += (int)Math.Pow(2, compartment.module.@class);
                            }
                            if (moduleName == "Armour")
                            {
                                Ship.health = compartment.module?.health ?? 100M;
                            }

                            Ship.compartments.Add(compartment);
                        }
                    }
                }

                // Obtain the launchbays
                if (json["launchBays"] != null)
                {
                    foreach (dynamic launchbay in json["launchBays"])
                    {
                        if (launchbay.Name.Contains("Slot"))
                        {
                            Ship.launchbays.Add(LaunchBayFromJson(launchbay, Ship));
                        }
                    }
                }

                // Obtain the cargo
                if (json["cargo"] != null && json["cargo"]["items"] != null)
                {
                    foreach (dynamic cargoJson in json["cargo"]["items"])
                    {
                        if (cargoJson != null && cargoJson["commodity"] != null)
                        {
                            string name  = (string)cargoJson["commodity"];
                            Cargo  cargo = new Cargo();
                            cargo.commodity = CommodityDefinitions.FromName(name);
                            if (cargo.commodity.name == null)
                            {
                                // Unknown commodity; log an error so that we can update the definitions
                                Logging.Error("No commodity definition for cargo", cargoJson.ToString(Formatting.None));
                                cargo.commodity.name = name;
                            }
                            cargo.amount    = (int)cargoJson["qty"];
                            cargo.price     = (long)cargoJson["value"] / cargo.amount;
                            cargo.missionid = (long?)cargoJson["mission"];
                            cargo.stolen    = ((int?)(long?)cargoJson["marked"]) == 1;

                            Ship.cargo.Add(cargo);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Warn("Failed to parse ship", ex);
            }

            return(Ship);
        }
Beispiel #9
0
        public static Ship ShipFromProfile(dynamic json)
        {
            if (json["ship"] == null)
            {
                return(null);
            }

            string Model = json["ship"]["name"];

            if (shipTranslations.ContainsKey(Model))
            {
                Model = shipTranslations[Model];
            }

            Ship Ship = ShipDefinitions.ShipFromModel(Model);

            Ship.LocalId = json["ship"]["id"];

            Ship.Value = (long)json["ship"]["value"]["hull"] + (long)json["ship"]["value"]["modules"];

            Ship.CargoCapacity = (int)json["ship"]["cargo"]["capacity"];
            Ship.CargoCarried  = (int)json["ship"]["cargo"]["qty"];

            // Be sensible with health - round it unless it's very low
            decimal Health = (decimal)json["ship"]["health"]["hull"] / 10000;

            if (Health < 5)
            {
                Ship.Health = Math.Round(Health, 1);
            }
            else
            {
                Ship.Health = Math.Round(Health);
            }

            // Obtain the internals
            Ship.Bulkheads        = ModuleFromProfile("Armour", json["ship"]["modules"]["Armour"]);
            Ship.PowerPlant       = ModuleFromProfile("PowerPlant", json["ship"]["modules"]["PowerPlant"]);
            Ship.Thrusters        = ModuleFromProfile("MainEngines", json["ship"]["modules"]["MainEngines"]);
            Ship.FrameShiftDrive  = ModuleFromProfile("FrameShiftDrive", json["ship"]["modules"]["FrameShiftDrive"]);
            Ship.LifeSupport      = ModuleFromProfile("LifeSupport", json["ship"]["modules"]["LifeSupport"]);
            Ship.PowerDistributor = ModuleFromProfile("PowerDistributor", json["ship"]["modules"]["PowerDistributor"]);
            Ship.Sensors          = ModuleFromProfile("Radar", json["ship"]["modules"]["Radar"]);
            Ship.FuelTank         = ModuleFromProfile("FuelTank", json["ship"]["modules"]["FuelTank"]);
            Ship.FuelTankCapacity = (decimal)json["ship"]["fuel"]["main"]["capacity"];

            // Obtain the hardpoints.  Hardpoints can come in any order so first parse them then second put them in the correct order
            Dictionary <string, Hardpoint> hardpoints = new Dictionary <string, Hardpoint>();

            foreach (dynamic module in json["ship"]["modules"])
            {
                if (module.Name.Contains("Hardpoint"))
                {
                    hardpoints.Add(module.Name, HardpointFromProfile(module));
                }
            }

            foreach (string size in HARDPOINT_SIZES)
            {
                for (int i = 1; i < 8; i++)
                {
                    Hardpoint hardpoint;
                    hardpoints.TryGetValue(size + "Hardpoint" + i, out hardpoint);
                    if (hardpoint != null)
                    {
                        Ship.Hardpoints.Add(hardpoint);
                    }
                }
            }

            // Obtain the compartments
            foreach (dynamic module in json["ship"]["modules"])
            {
                if (module.Name.Contains("Slot"))
                {
                    Ship.Compartments.Add(CompartmentFromProfile(module));
                }
            }

            // Obtain the cargo
            Ship.Cargo = new List <Cargo>();
            if (json["ship"]["cargo"] != null && json["ship"]["cargo"]["items"] != null)
            {
                foreach (dynamic cargoJson in json["ship"]["cargo"]["items"])
                {
                    string name  = (string)cargoJson["commodity"];
                    Cargo  cargo = new Cargo();
                    cargo.Commodity = CommodityDefinitions.CommodityFromCargoName(name);
                    if (cargo.Commodity.Name == null)
                    {
                        // Unknown commodity; log an error so that we can update the definitions
                        DataProviderService.LogError("No commodity definition for cargo " + cargoJson.ToString());
                        cargo.Commodity.Name = name;
                    }
                    cargo.Quantity = (int)cargoJson["qty"];
                    cargo.Cost     = (long)cargoJson["value"];
                    Ship.Cargo.Add(cargo);
                }
            }

            return(Ship);
        }
        public static Ship ShipFromProfile(dynamic json)
        {
            Logging.Debug("Entered");
            if (json == null)
            {
                Logging.Debug("Leaving");
                return(null);
            }

            Ship Ship = ShipDefinitions.FromEDModel((string)json["name"]);

            Ship.json = json.ToString(Formatting.None);

            Ship.LocalId = json["id"];

            // Some ship information is just skeleton data of the ship's ID.  Use value as our canary to see if there is more data
            if (json["value"] != null)
            {
                Ship.value = (long)json["value"]["hull"] + (long)json["value"]["modules"];

                Ship.cargocapacity = (int)json["cargo"]["capacity"];
                Ship.cargocarried  = (int)json["cargo"]["qty"];

                // Be sensible with health - round it unless it's very low
                decimal Health = (decimal)json["health"]["hull"] / 10000;
                if (Health < 5)
                {
                    Ship.health = Math.Round(Health, 1);
                }
                else
                {
                    Ship.health = Math.Round(Health);
                }

                // Obtain the internals
                Ship.bulkheads             = ModuleFromProfile("Armour", json["modules"]["Armour"]);
                Ship.powerplant            = ModuleFromProfile("PowerPlant", json["modules"]["PowerPlant"]);
                Ship.thrusters             = ModuleFromProfile("MainEngines", json["modules"]["MainEngines"]);
                Ship.frameshiftdrive       = ModuleFromProfile("FrameShiftDrive", json["modules"]["FrameShiftDrive"]);
                Ship.lifesupport           = ModuleFromProfile("LifeSupport", json["modules"]["LifeSupport"]);
                Ship.powerdistributor      = ModuleFromProfile("PowerDistributor", json["modules"]["PowerDistributor"]);
                Ship.sensors               = ModuleFromProfile("Radar", json["modules"]["Radar"]);
                Ship.fueltank              = ModuleFromProfile("FuelTank", json["modules"]["FuelTank"]);
                Ship.fueltankcapacity      = (decimal)Math.Pow(2, Ship.fueltank.@class);
                Ship.fueltanktotalcapacity = (decimal)json["fuel"]["main"]["capacity"];

                // Obtain the hardpoints.  Hardpoints can come in any order so first parse them then second put them in the correct order
                Dictionary <string, Hardpoint> hardpoints = new Dictionary <string, Hardpoint>();
                foreach (JProperty module in json["modules"])
                {
                    if (module.Name.Contains("Hardpoint"))
                    {
                        hardpoints.Add(module.Name, HardpointFromProfile(module));
                    }
                }

                foreach (string size in HARDPOINT_SIZES)
                {
                    for (int i = 1; i < 12; i++)
                    {
                        Hardpoint hardpoint;
                        hardpoints.TryGetValue(size + "Hardpoint" + i, out hardpoint);
                        if (hardpoint != null)
                        {
                            Ship.hardpoints.Add(hardpoint);
                        }
                    }
                }

                // Obtain the compartments
                foreach (dynamic module in json["modules"])
                {
                    if (module.Name.Contains("Slot"))
                    {
                        Ship.compartments.Add(CompartmentFromProfile(module));
                    }
                }

                // Obtain the cargo
                Ship.cargo = new List <Cargo>();
                if (json["cargo"] != null && json["cargo"]["items"] != null)
                {
                    foreach (dynamic cargoJson in json["cargo"]["items"])
                    {
                        if (cargoJson != null && cargoJson["commodity"] != null)
                        {
                            string name  = (string)cargoJson["commodity"];
                            Cargo  cargo = new Cargo();
                            cargo.commodity = CommodityDefinitions.FromName(name);
                            if (cargo.commodity.name == null)
                            {
                                // Unknown commodity; log an error so that we can update the definitions
                                Logging.Error("No commodity definition for cargo", cargoJson.ToString(Formatting.None));
                                cargo.commodity.name = name;
                            }
                            cargo.amount    = (int)cargoJson["qty"];
                            cargo.price     = (long)cargoJson["value"] / cargo.amount;
                            cargo.missionid = (long?)cargoJson["mission"];
                            cargo.stolen    = ((int?)(long?)cargoJson["marked"]) == 1;

                            Ship.cargo.Add(cargo);
                        }
                    }
                }
            }

            Logging.Debug("Leaving");
            return(Ship);
        }