public void TestHaulageCopyCtor() { Haulage original = new Haulage(1, "name", "Sol", 42, null, false); Haulage copy = new Haulage(original); Assert.AreEqual(original.name, copy.name); }
private void _handleMissionAcceptedEvent(MissionAcceptedEvent @event) { Cargo cargo = new Cargo(); string type = @event.name.Split('_').ElementAt(1)?.ToLowerInvariant(); if (type != null && CHAINED.TryGetValue(type, out string value)) { type = value; } else if (type == "ds" || type == "rs" || type == "welcome") { type = @event.name.Split('_').ElementAt(2)?.ToLowerInvariant(); } bool naval = @event.name.ToLowerInvariant().Contains("rank"); switch (type) { case "altruism": case "collect": case "collectwing": case "delivery": case "deliverywing": case "mining": case "piracy": case "rescue": case "salvage": case "smuggle": { string originSystem = EDDI.Instance?.CurrentStarSystem?.name; Haulage haulage = new Haulage(@event.missionid ?? 0, @event.name, originSystem, @event.amount ?? 0, @event.expiry) { startmarketid = (type.Contains("delivery") && !naval) ? EDDI.Instance?.CurrentStation?.marketId ?? 0 : 0, endmarketid = (type.Contains("collect")) ? EDDI.Instance?.CurrentStation?.marketId ?? 0 : 0, }; if (type.Contains("delivery") || type == "smuggle") { haulage.sourcesystem = EDDI.Instance?.CurrentStarSystem?.name; haulage.sourcebody = EDDI.Instance?.CurrentStation?.name; } else if (type == "rescue" || type == "salvage") { haulage.sourcesystem = @event.destinationsystem; } cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo == null) { cargo = new Cargo(@event.commodityDefinition?.edname, 0); AddCargo(cargo); } cargo.haulageData.Add(haulage); cargo.CalculateNeed(); } break; } }
private void _handleMissionExpiredEvent(MissionExpiredEvent @event) { Haulage haulage = GetHaulageWithMissionId(@event.missionid ?? 0); if (haulage != null) { haulage.status = "Failed"; } }
private void UpdateCargoFromInfo(Cargo cargo, List <CargoInfo> infoList) { cargo.total = infoList.Sum(i => i.count); cargo.haulage = infoList.Where(i => i.missionid != null).Sum(i => i.count); cargo.stolen = infoList.Where(i => i.missionid == null).Sum(i => i.stolen); cargo.owned = cargo.total - cargo.haulage - cargo.stolen; MissionMonitor missionMonitor = (MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor"); foreach (CargoInfo info in infoList.Where(i => i.missionid != null).ToList()) { Mission mission = missionMonitor?.GetMissionWithMissionId(info.missionid ?? 0); Haulage cargoHaulage = cargo.haulageData.FirstOrDefault(h => h.missionid == info.missionid); if (cargoHaulage != null) { int need = cargoHaulage.remaining - info.count; // Check for sold haulage if (need != cargoHaulage.need) { if (checkHaulage && need > cargoHaulage.need) { // We lost haulage switch (cargoHaulage.typeEDName) { case "delivery": case "deliverywing": case "smuggle": { cargoHaulage.status = "Failed"; if (mission != null) { mission.statusDef = MissionStatus.FromEDName("Failed"); } } break; } } cargoHaulage.need = need; } } else { string name = mission?.name ?? "Mission_None"; int amount = mission?.amount ?? info.count; DateTime?expiry = mission?.expiry; cargoHaulage = new Haulage(info.missionid ?? 0, name, mission?.originsystem, amount, expiry); cargo.haulageData.Add(cargoHaulage); } } cargo.CalculateNeed(); checkHaulage = false; }
public Haulage GetHaulageWithMissionId(long missionid) { foreach (Cargo cargo in inventory.ToList()) { Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == missionid); if (haulage != null) { return(haulage); } } return(null); }
private bool _handleMissionExpiredEvent(MissionExpiredEvent @event) { bool update = false; Haulage haulage = GetHaulageWithMissionId(@event.missionid ?? 0); if (haulage != null) { haulage.status = "Failed"; update = true; } return(update); }
private void _handleMissionFailedEvent(MissionFailedEvent @event) { Haulage haulage = GetHaulageWithMissionId(@event.missionid ?? 0); if (haulage != null) { Cargo cargo = GetCargoWithMissionId(@event.missionid ?? 0); int onboard = haulage.remaining - haulage.need; cargo.haulage -= onboard; cargo.stolen += onboard; cargo.haulageData.Remove(haulage); RemoveCargo(cargo); } }
private void _handleMissionCompletedEvent(MissionCompletedEvent @event) { Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo != null) { Haulage haulage = cargo.haulageData.FirstOrDefault(ha => ha.missionid == @event.missionid); if (haulage != null) { cargo.haulageData.Remove(haulage); } RemoveCargo(cargo); } }
private bool _handleMissionFailedEvent(MissionFailedEvent @event) { bool update = false; Haulage haulage = GetHaulageWithMissionId(@event.missionid ?? 0); if (haulage != null) { Cargo cargo = GetCargoWithMissionId(@event.missionid ?? 0); int onboard = haulage.remaining - haulage.need; cargo.RemoveDetailedQty(CargoType.haulage, onboard, haulage); cargo.AddDetailedQty(CargoType.stolen, onboard, cargo.price); RemoveCargo(cargo); return(true); } return(update); }
public void TestCargoEventsScenario() { // Save original data CargoMonitorConfiguration data = CargoMonitorConfiguration.FromFile(); var privateObject = new PrivateObject(cargoMonitor); Haulage haulage = new Haulage(); // CargoEvent line = "{ \"timestamp\":\"2018-10-31T01:54:40Z\", \"event\":\"Missions\", \"Active\":[ ], \"Failed\":[ ], \"Complete\":[ ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionsEvent", new object[] { events[0] }); line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":32, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); Assert.AreEqual(3, cargoMonitor.inventory.Count); Assert.AreEqual(32, cargoMonitor.cargoCarried); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "HydrogenFuel"); Assert.AreEqual("Hydrogen Fuel", cargo.localizedName); Assert.AreEqual(1, cargo.total); Assert.AreEqual(1, cargo.owned); Assert.AreEqual(0, cargo.need + cargo.stolen + cargo.haulage); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Biowaste"); Assert.AreEqual(30, cargo.total); Assert.AreEqual(30, cargo.haulage); haulage = cargo.haulageData.FirstOrDefault(); Assert.IsNotNull(haulage); Assert.AreEqual(426282789, haulage.missionid); Assert.AreEqual("Mission_None", haulage.name); Assert.AreEqual(30, haulage.amount); Assert.AreEqual("Active", haulage.status); // CargoEjectedEvent haulage.typeEDName = "delivery"; line = @"{""timestamp"": ""2016-06-10T14:32:03Z"", ""event"": ""EjectCargo"", ""Type"":""biowaste"", ""Count"":2, ""MissionID"":426282789, ""Abandoned"":true}"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCommodityEjectedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Biowaste"); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 426282789); Assert.AreEqual("Failed", haulage.status); // Restore original data data.ToFile(); }
private bool _handleMissionFailedEvent(MissionFailedEvent @event) { bool update = false; Haulage haulage = GetHaulageWithMissionId(@event.missionid ?? 0); if (haulage != null) { Cargo cargo = GetCargoWithMissionId(@event.missionid ?? 0); int onboard = haulage.remaining - haulage.need; cargo.haulage -= onboard; cargo.stolen += onboard; cargo.haulageData.Remove(haulage); RemoveCargo(cargo); return(true); } return(update); }
private bool _handleMissionCompletedEvent(MissionCompletedEvent @event) { bool update = false; Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo != null) { Haulage haulage = cargo.haulageData.FirstOrDefault(ha => ha.missionid == @event.missionid); if (haulage != null) { cargo.haulageData.Remove(haulage); } RemoveCargo(cargo); update = true; } return(update); }
private bool _handleCommodityEjectedEvent(CommodityEjectedEvent @event) { bool update = false; Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo != null) { if (EDDI.Instance?.Vehicle != Constants.VEHICLE_SHIP) { if (@event.missionid != null) { cargo.haulage -= @event.amount; } else { cargo.owned -= @event.amount; } cargo.CalculateNeed(); update = true; } Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == @event.missionid); if (haulage != null) { MissionMonitor missionMonitor = (MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor"); switch (haulage.typeEDName) { case "delivery": case "deliverywing": case "smuggle": { haulage.status = "Failed"; Mission mission = missionMonitor?.GetMissionWithMissionId(@event.missionid ?? 0); if (mission != null) { mission.statusDef = MissionStatus.FromEDName("Failed"); } update = true; } break; } } } return(update); }
private bool _handleCommodityCollectedEvent(CommodityCollectedEvent @event) { bool update = false; Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo != null) { if (EDDI.Instance?.Vehicle != Constants.VEHICLE_SHIP) { if (@event.missionid != null) { cargo.haulage++; } else if (@event.stolen) { cargo.stolen++; } else { cargo.owned++; } cargo.CalculateNeed(); update = true; } Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == @event.missionid); if (haulage != null) { switch (haulage.typeEDName) { case "mining": case "piracy": case "rescue": case "salvage": { haulage.sourcesystem = EDDI.Instance?.CurrentStarSystem?.systemname; haulage.sourcebody = EDDI.Instance?.CurrentStellarBody?.bodyname; update = true; } break; } } } return(update); }
private bool _handleCommodityRefinedEvent(CommodityRefinedEvent @event) { bool update = false; Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname); if (cargo != null) { Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.typeEDName .ToLowerInvariant() .Contains("mining")); if (haulage != null) { haulage.sourcesystem = EDDI.Instance?.CurrentStarSystem?.systemname; haulage.sourcebody = EDDI.Instance?.CurrentStation?.name; update = true; } } return(update); }
private bool _handleCommodityPurchasedEvent(CommodityPurchasedEvent @event) { Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname) ?? new Cargo(@event.commodityDefinition?.edname); Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.typeEDName .ToLowerInvariant() .Contains("collect")); if (haulage != null) { haulage.sourcesystem = EDDI.Instance?.CurrentStarSystem?.systemname; haulage.sourcebody = EDDI.Instance?.CurrentStation?.name; cargo.AddDetailedQty(CargoType.haulage, @event.amount, @event.price, haulage); } else { cargo.AddDetailedQty(CargoType.owned, @event.amount, @event.price); } AddOrUpdateCargo(cargo); return(true); }
public void TestCargoMissionScenario() { cargoMonitor.initializeCargoMonitor(new CargoMonitorConfiguration()); var privateObject = new PrivateObject(cargoMonitor); line = @"{""timestamp"": ""2018-05-05T19:12:10Z"", ""event"": ""Cargo"", ""Inventory"": [ { ""Name"": ""damagedescapepod"", ""Name_Localised"": ""Damaged Escape Pod"", ""Count"": 4, ""Stolen"": 0 }, { ""Name"": ""usscargoblackbox"", ""Name_Localised"": ""Black Box"", ""Count"": 4, ""Stolen"": 4 }, { ""Name"": ""drones"", ""Name_Localised"": ""Limpet"", ""Count"": 21, ""Stolen"": 0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoInventoryEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Drones"); Assert.AreEqual("Limpet", cargo.invariantName); Assert.AreEqual(21, cargo.total); Assert.AreEqual(21, cargo.owned); Assert.AreEqual(0, cargo.need + cargo.stolen + cargo.haulage); // CargoMissionAcceptedEvent - Check to see if this is a cargo mission and update our inventory accordingly line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 4 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375660729 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual("Structural Regulators", cargo.invariantName); Assert.AreEqual(0, cargo.total); Assert.AreEqual(7, cargo.need); Haulage haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 375682327); Assert.AreEqual(0, cargo.haulage + cargo.stolen + cargo.owned); Assert.AreEqual(3, haulage.amount); Assert.AreEqual("Mission_Salvage_Planet", haulage.name); Assert.AreEqual(DateTime.Parse("2018-05-12T15:20:27Z").ToUniversalTime(), haulage.expiry); // CargoCollectedEvent line = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCommodityCollectedEvent", new object[] { events[0] }); line = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCommodityCollectedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(2, cargo.total); Assert.AreEqual(2, cargo.haulage); Assert.AreEqual(5, cargo.need); Assert.AreEqual(0, cargo.stolen + cargo.owned); Assert.AreEqual(3, haulage.amount); // CargoMissionAbandonedEvent - If we abandon a mission with cargo it becomes stolen line = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionAbandoned"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAbandonedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(2, cargo.total); Assert.AreEqual(2, cargo.stolen); Assert.AreEqual(4, cargo.need); Assert.AreEqual(0, cargo.haulage + cargo.owned); // CargoMissionCompletedEvent - Check to see if this is a cargo mission and update our inventory accordingly line = @"{ ""timestamp"": ""2018-05-05T22:27:58Z"", ""event"": ""MissionCompleted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet_name"", ""MissionID"": 375660729, ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Reward"": 624016, ""FactionEffects"": [ { ""Faction"": ""Merope Expeditionary Fleet"", ""Effects"": [ { ""Effect"": ""$MISSIONUTIL_Interaction_Summary_civilUnrest_down;"", ""Effect_Localised"": ""$#MinorFaction; are happy to report improved civil contentment, making a period of civil unrest unlikely."", ""Trend"": ""DownGood"" } ], ""Influence"": [ { ""SystemAddress"": 224644818084, ""Trend"": ""UpGood"" } ], ""Reputation"": ""UpGood"" } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionCompletedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.IsNull(cargo); // CargoMissionFailedEvent - If we fail a mission with cargo it becomes stolen line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); line = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCommodityCollectedEvent", new object[] { events[0] }); line = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionFailed"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionFailedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(1, cargo.total); Assert.AreEqual(1, cargo.stolen); Assert.AreEqual(0, cargo.need); Assert.AreEqual(0, cargo.haulage + cargo.owned); // CargoDepotEvent - Check response for missed 'Mission accepted' event. Verify both cargo and haulage are created line = @"{ ""timestamp"":""2018-08-26T02:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748324, ""UpdateType"":""Deliver"", ""CargoType"":""Tantalum"", ""Count"":54, ""StartMarketID"":0, ""EndMarketID"":3224777216, ""ItemsCollected"":0, ""ItemsDelivered"":54, ""TotalItemsToDeliver"":70, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Tantalum"); Assert.IsNotNull(cargo); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage + cargo.owned); Assert.AreEqual(16, cargo.need); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 413748324); Assert.IsNotNull(haulage); Assert.AreEqual(16, haulage.remaining); Assert.IsTrue(haulage.shared); // Cargo Delivery 'Mission accepted' Event with 'Cargo Depot' events line = @"{ ""timestamp"":""2018-08-26T00:50:48Z"", ""event"":""MissionAccepted"", ""Faction"":""Calennero State Industries"", ""Name"":""Mission_Delivery_Boom"", ""LocalisedName"":""Boom time delivery of 60 units of Silver"", ""Commodity"":""$Silver_Name;"", ""Commodity_Localised"":""Silver"", ""Count"":60, ""DestinationSystem"":""HIP 20277"", ""DestinationStation"":""Fabian City"", ""Expiry"":""2018-08-27T00:48:38Z"", ""Wing"":false, ""Influence"":""Med"", ""Reputation"":""Med"", ""Reward"":25000000, ""MissionID"":413748339 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Silver"); Assert.IsNotNull(cargo); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage + cargo.owned); Assert.AreEqual(60, cargo.need); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 413748339); Assert.IsNotNull(haulage); Assert.AreEqual(60, haulage.remaining); Assert.IsFalse(haulage.shared); line = @"{ ""timestamp"":""2018-08-26T02:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748339, ""UpdateType"":""Collect"", ""CargoType"":""Silver"", ""Count"":60, ""StartMarketID"":3225297216, ""EndMarketID"":3224777216, ""ItemsCollected"":60, ""ItemsDelivered"":0, ""TotalItemsToDeliver"":60, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); Assert.AreEqual(60, cargo.total); Assert.AreEqual(60, cargo.haulage); Assert.AreEqual(0, cargo.need); Assert.AreEqual(60, haulage.remaining); Assert.AreEqual(3225297216, haulage.startmarketid); Assert.AreEqual(3224777216, haulage.endmarketid); line = @"{ ""timestamp"":""2018-08-26T03:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748339, ""UpdateType"":""Deliver"", ""CargoType"":""Silver"", ""Count"":60, ""StartMarketID"":3225297216, ""EndMarketID"":3224777216, ""ItemsCollected"":60, ""ItemsDelivered"":60, ""TotalItemsToDeliver"":60, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage); Assert.AreEqual(0, cargo.need); Assert.AreEqual(0, haulage.remaining); }
private void _handleCargoDepotEvent(CargoDepotEvent @event) { MissionMonitor missionMonitor = (MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor"); Mission mission = missionMonitor?.GetMissionWithMissionId(@event.missionid ?? 0); Cargo cargo = new Cargo(); Haulage haulage = new Haulage(); int amountRemaining = @event.totaltodeliver - @event.delivered; switch (@event.updatetype) { case "Collect": { cargo = GetCargoWithMissionId(@event.missionid ?? 0); if (cargo != null) { // Cargo instantiated by either 'Mission accepted' event or previous 'WingUpdate' update haulage = cargo.haulageData.FirstOrDefault(ha => ha.missionid == @event.missionid); haulage.remaining = amountRemaining; // Update commodity definition if instantiated other than 'Mission accepted' cargo.commodityDef = @event.commodityDefinition; haulage.originsystem = EDDI.Instance?.CurrentStarSystem?.systemname; } else { // First exposure to new cargo. cargo = new Cargo(@event.commodityDefinition.edname, 0); // Total will be updated by following 'Cargo' event AddCargo(cargo); string originSystem = EDDI.Instance?.CurrentStarSystem?.systemname; string name = mission?.name ?? "MISSION_DeliveryWing"; haulage = new Haulage(@event.missionid ?? 0, name, originSystem, amountRemaining, null, true); cargo.haulageData.Add(haulage); } haulage.collected = @event.collected; haulage.delivered = @event.delivered; haulage.startmarketid = @event.startmarketid; haulage.endmarketid = @event.endmarketid; } break; case "Deliver": { cargo = GetCargoWithMissionId(@event.missionid ?? 0); if (cargo != null) { // Cargo instantiated by either 'Mission accepted' event, previous 'WingUpdate' or 'Collect' updates haulage = cargo.haulageData.FirstOrDefault(ha => ha.missionid == @event.missionid); if (haulage != null) { haulage.remaining = amountRemaining; haulage.need = amountRemaining; //Update commodity definition haulage.amount = @event.totaltodeliver; cargo.commodityDef = @event.commodityDefinition; haulage.originsystem = (@event.startmarketid == 0) ? EDDI.Instance?.CurrentStarSystem?.systemname : null; } else { string originSystem = (@event.startmarketid == 0) ? EDDI.Instance?.CurrentStarSystem?.systemname : null; string name = mission?.name ?? (@event.startmarketid == 0 ? "MISSION_CollectWing" : "MISSION_DeliveryWing"); haulage = new Haulage(@event.missionid ?? 0, name, originSystem, amountRemaining, null); cargo.haulageData.Add(haulage); } } else { // Check if cargo instantiated by previous 'Market buy' event cargo = GetCargoWithEDName(@event.commodityDefinition.edname); if (cargo == null) { cargo = new Cargo(@event.commodityDefinition.edname, 0); // Total will be updated by following 'Cargo' event AddCargo(cargo); } string originSystem = (@event.startmarketid == 0) ? EDDI.Instance?.CurrentStarSystem?.systemname : null; string name = mission?.name ?? (@event.startmarketid == 0 ? "MISSION_CollectWing" : "MISSION_DeliveryWing"); haulage = new Haulage(@event.missionid ?? 0, name, originSystem, amountRemaining, null, true); cargo.haulageData.Add(haulage); } // Update 'Need' when cargo is delivered, as the 'Cargo' event handler does not update 'Collect' mission types cargo.CalculateNeed(); haulage.collected = @event.collected; haulage.delivered = @event.delivered; haulage.endmarketid = (haulage.endmarketid == 0) ? @event.endmarketid : haulage.endmarketid; // Check for mission completion if (amountRemaining == 0) { if (haulage.shared) { cargo.haulageData.Remove(haulage); RemoveCargo(cargo); } else { haulage.status = "Complete"; } } } break; case "WingUpdate": { cargo = GetCargoWithMissionId(@event.missionid ?? 0); if (cargo != null) { // Cargo instantiated by either 'Mission accepted' event, previous 'WingUpdate' or 'Collect' updates haulage = cargo.haulageData.FirstOrDefault(ha => ha.missionid == @event.missionid); haulage.remaining = amountRemaining; haulage.need = amountRemaining; } else { // First exposure to new cargo, use 'Unknown' as placeholder cargo = new Cargo("Unknown", 0); AddCargo(cargo); string name = mission?.name ?? (@event.startmarketid == 0 ? "MISSION_CollectWing" : "MISSION_DeliveryWing"); haulage = new Haulage(@event.missionid ?? 0, name, null, amountRemaining, null, true); cargo.haulageData.Add(haulage); } // Generate a derived event when a wing-mate collects or delivers cargo for a wing mission int amount = Math.Max(@event.collected - haulage.collected, @event.delivered - haulage.delivered); if (amount > 0) { string updatetype = @event.collected > haulage.collected ? "Collect" : "Deliver"; EDDI.Instance.enqueueEvent(new CargoWingUpdateEvent(DateTime.UtcNow, haulage.missionid, updatetype, cargo.commodityDef, amount, @event.collected, @event.delivered, @event.totaltodeliver)); haulage.collected = @event.collected; haulage.delivered = @event.delivered; haulage.startmarketid = @event.startmarketid; haulage.endmarketid = @event.endmarketid; // Update 'Need' when a wing-mate delivers cargo for a wing mission if (updatetype == "Deliver") { cargo.CalculateNeed(); } } // Check for mission completion if (amountRemaining == 0) { if (haulage.shared) { cargo.haulageData.Remove(haulage); RemoveCargo(cargo); } else { haulage.status = "Complete"; } } } break; } }
/// <summary> /// Build a store from a list of variables /// </summary> private BuiltinStore buildStore(Dictionary <string, Cottle.Value> vars) { BuiltinStore store = new BuiltinStore(); // TODO fetch this from configuration bool useICAO = SpeechServiceConfiguration.FromFile().EnableIcao; // Function to call another script store["F"] = new NativeFunction((values) => { return(new ScriptResolver(scripts).resolve(values[0].AsString, store, false)); }, 1); // Translation functions store["P"] = new NativeFunction((values) => { string val = values[0].AsString; string translation = val; if (translation == val) { translation = Translations.Body(val, useICAO); } if (translation == val) { translation = Translations.StarSystem(val, useICAO); } if (translation == val) { translation = Translations.Station(val); } if (translation == val) { translation = Translations.Faction(val); } if (translation == val) { translation = Translations.Power(val); } if (translation == val) { Ship ship = ShipDefinitions.FromModel(val); if (ship != null && ship.EDID > 0) { translation = ship.SpokenModel(); } } if (translation == val) { Ship ship = ShipDefinitions.FromEDModel(val); if (ship != null && ship.EDID > 0) { translation = ship.SpokenModel(); } } if (translation == val) { translation = Translations.StellarClass(val); } return(translation); }, 1); // Boolean constants store["true"] = true; store["false"] = false; // Helper functions store["OneOf"] = new NativeFunction((values) => { return(new ScriptResolver(scripts).resolveScript(values[random.Next(values.Count)].AsString, store, false)); }); store["Occasionally"] = new NativeFunction((values) => { if (random.Next((int)values[0].AsNumber) == 0) { return(new ScriptResolver(scripts).resolveScript(values[1].AsString, store, false)); } else { return(""); } }, 2); store["Humanise"] = new NativeFunction((values) => { return(Translations.Humanize(values[0].AsNumber)); }, 1); store["List"] = new NativeFunction((values) => { string output = String.Empty; const string localisedAnd = "and"; if (values.Count == 1) { foreach (KeyValuePair <Cottle.Value, Cottle.Value> value in values[0].Fields) { string valueString = value.Value.ToString(); if (value.Key == 0) { output = valueString; } else if (value.Key < (values[0].Fields.Count - 1)) { output = $"{output}, {valueString}"; } else { output = $"{output}{(values.Count() > 2 ? "," : "")} {localisedAnd} {valueString}"; } } } return(output); }, 1); store["Pause"] = new NativeFunction((values) => { return(@"<break time=""" + values[0].AsNumber + @"ms"" />"); }, 1); store["Play"] = new NativeFunction((values) => { return(@"<audio src=""" + values[0].AsString + @""" />"); }, 1); store["Spacialise"] = new NativeFunction((values) => { string Entree = values[0].AsString; if (Entree == "") { return(""); } string Sortie = ""; string UpperSortie = ""; foreach (char c in Entree) { Sortie = Sortie + c + " "; } UpperSortie = Sortie.ToUpper(); return(UpperSortie); }, 1); store["Emphasize"] = new NativeFunction((values) => { if (values.Count == 1) { return(@"<emphasis level =""strong"">" + values[0].AsString + @"</emphasis>"); } else if (values.Count == 2) { return(@"<emphasis level =""" + values[1].AsString + @""">" + values[0].AsString + @"</emphasis>"); } else { return("The Emphasize function is used improperly. Please review the documentation for correct usage."); } }, 1, 2); store["SpeechPitch"] = new NativeFunction((values) => { string text = values[0].AsString; string pitch = "default"; if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString)) { return(text); } else if (values.Count == 2) { pitch = values[1].AsString; return(@"<prosody pitch=""" + pitch + @""">" + text + "</prosody>"); } else { return("The SpeechPitch function is used improperly. Please review the documentation for correct usage."); } }, 1, 2); store["SpeechRate"] = new NativeFunction((values) => { string text = values[0].AsString; string rate = "default"; if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString)) { return(text); } else if (values.Count == 2) { rate = values[1].AsString; return(@"<prosody rate=""" + rate + @""">" + text + "</prosody>"); } else { return("The SpeechRate function is used improperly. Please review the documentation for correct usage."); } }, 1, 2); store["SpeechVolume"] = new NativeFunction((values) => { string text = values[0].AsString; string volume = "default"; if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString)) { return(text); } else if (values.Count == 2) { volume = values[1].AsString; return(@"<prosody volume=""" + volume + @""">" + text + "</prosody>"); } else { return("The SpeechVolume function is used improperly. Please review the documentation for correct usage."); } }, 1, 2); store["Transmit"] = new NativeFunction((values) => { string text = values[0].AsString; if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString)) { return(@"<transmit>" + values[0].AsString + "</transmit>"); // This is a synthetic tag used to signal to the speech service that radio effects should be enabled. } else { return("The Transmit function is used improperly. Please review the documentation for correct usage."); } }, 1); store["StartsWithVowel"] = new NativeFunction((values) => { string Entree = values[0].AsString; if (Entree == "") { return(""); } char[] vowels = { 'a', 'à', 'â', 'ä', 'e', 'ê', 'é', 'è', 'ë', 'i', 'î', 'ï', 'o', 'ô', 'ö', 'u', 'ù', 'û', 'ü', 'œ', 'y' }; char firstCharacter = Entree.ToLower().ToCharArray().ElementAt(0); Boolean result = vowels.Contains(firstCharacter); return(result); }, 1); store["Voice"] = new NativeFunction((values) => { string text = values[0].AsString; string voice = values[1].AsString; foreach (System.Speech.Synthesis.InstalledVoice vc in SpeechService.synth?.GetInstalledVoices()) { if (vc.VoiceInfo.Name.ToLowerInvariant().Contains(voice?.ToLowerInvariant()) && !vc.VoiceInfo.Name.Contains("Microsoft Server Speech Text to Speech Voice")) { voice = vc.VoiceInfo.Name; continue; } } if (values.Count == 2) { return(@"<voice name=""" + voice + @""">" + text + "</voice>"); } else { return("The Voice function is used improperly. Please review the documentation for correct usage."); } }, 1, 2); store["VoiceDetails"] = new NativeFunction((values) => { var result = new object(); if (values.Count == 0) { List <VoiceDetail> voices = new List <VoiceDetail>(); foreach (System.Speech.Synthesis.InstalledVoice vc in SpeechService.synth?.GetInstalledVoices()) { if (!vc.VoiceInfo.Name.Contains("Microsoft Server Speech Text to Speech Voice")) { voices.Add(new VoiceDetail( vc.VoiceInfo.Name, vc.VoiceInfo.Culture.Parent?.EnglishName ?? vc.VoiceInfo.Culture.EnglishName, vc.VoiceInfo.Culture.Parent?.NativeName ?? vc.VoiceInfo.Culture.NativeName, vc.VoiceInfo.Culture.Name, vc.VoiceInfo.Gender.ToString(), vc.Enabled )); } } result = voices; return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); } else if (values.Count == 1) { foreach (System.Speech.Synthesis.InstalledVoice vc in SpeechService.synth?.GetInstalledVoices()) { if (vc.VoiceInfo.Name.ToLowerInvariant().Contains(values[0].AsString?.ToLowerInvariant()) && !vc.VoiceInfo.Name.Contains("Microsoft Server Speech Text to Speech Voice")) { result = new VoiceDetail( vc.VoiceInfo.Name, vc.VoiceInfo.Culture.Parent?.EnglishName ?? vc.VoiceInfo.Culture.EnglishName, vc.VoiceInfo.Culture.Parent?.NativeName ?? vc.VoiceInfo.Culture.NativeName, vc.VoiceInfo.Culture.Name, vc.VoiceInfo.Gender.ToString(), vc.Enabled ); continue; } } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); } return("The VoiceDetails function is used improperly. Please review the documentation for correct usage."); }, 0, 1); // // Commander-specific functions // store["ShipName"] = new NativeFunction((values) => { int?localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber); string model = (values.Count == 2 ? values[1].AsString : null); Ship ship = findShip(localId, model); string result = (ship == null ? "your ship" : ship.SpokenName()); return(result); }, 0, 2); store["ShipCallsign"] = new NativeFunction((values) => { int?localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber); Ship ship = findShip(localId, null); string result; if (ship != null) { if (EDDI.Instance.Cmdr != null && EDDI.Instance.Cmdr.name != null) { // Obtain the first three characters string chars = new Regex("[^a-zA-Z0-9]").Replace(EDDI.Instance.Cmdr.name, "").ToUpperInvariant().Substring(0, 3); result = ship.SpokenManufacturer() + " " + Translations.ICAO(chars); } else { if (ship.SpokenManufacturer() == null) { result = "unidentified ship"; } else { result = "unidentified " + ship.SpokenManufacturer() + " " + ship.SpokenModel(); } } } else { result = "unidentified ship"; } return(result); }, 0, 1); // // Obtain definition objects for various items // store["SecondsSince"] = new NativeFunction((values) => { long?date = (long?)values[0].AsNumber; if (date == null) { return(null); } long?now = (long?)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalSeconds; return(now - date); }, 1); store["ICAO"] = new NativeFunction((values) => { // Turn a string in to an ICAO definition string value = values[0].AsString; if (value == null || value == "") { return(""); } // Remove anything that isn't alphanumeric Logging.Warn("value is " + value); value = value.ToUpperInvariant().Replace("[^A-Z0-9]", ""); Logging.Warn("value is " + value); // Translate to ICAO return(Translations.ICAO(value)); }, 1); store["ShipDetails"] = new NativeFunction((values) => { Ship result = ShipDefinitions.FromModel(values[0].AsString); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["CombatRatingDetails"] = new NativeFunction((values) => { CombatRating result = CombatRating.FromName(values[0].AsString); if (result == null) { result = CombatRating.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["TradeRatingDetails"] = new NativeFunction((values) => { TradeRating result = TradeRating.FromName(values[0].AsString); if (result == null) { result = TradeRating.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["ExplorationRatingDetails"] = new NativeFunction((values) => { ExplorationRating result = ExplorationRating.FromName(values[0].AsString); if (result == null) { result = ExplorationRating.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["EmpireRatingDetails"] = new NativeFunction((values) => { EmpireRating result = EmpireRating.FromName(values[0].AsString); if (result == null) { result = EmpireRating.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["FederationRatingDetails"] = new NativeFunction((values) => { FederationRating result = FederationRating.FromName(values[0].AsString); if (result == null) { result = FederationRating.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["SystemDetails"] = new NativeFunction((values) => { StarSystem result; if (values.Count == 0) { result = EDDI.Instance.CurrentStarSystem; } else if (values[0]?.AsString?.ToLowerInvariant() == EDDI.Instance.CurrentStarSystem?.name?.ToLowerInvariant()) { result = EDDI.Instance.CurrentStarSystem; } else { result = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[0].AsString, true); } setSystemDistanceFromHome(result); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["BodyDetails"] = new NativeFunction((values) => { StarSystem system; if (values.Count == 0) { system = EDDI.Instance.CurrentStarSystem; } else if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString) || values[1]?.AsString?.ToLowerInvariant() == EDDI.Instance.CurrentStarSystem?.name?.ToLowerInvariant()) { system = EDDI.Instance.CurrentStarSystem; } else { // Named system system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true); } Body result = system != null && system.bodies != null ? system.bodies.FirstOrDefault(v => v.name.ToLowerInvariant() == values[0].AsString.ToLowerInvariant()) : null; if (result != null && result.Type.invariantName == "Star" && result.chromaticity == null) { // Need to set our internal extras for the star result.setStellarExtras(); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1, 2); store["MissionDetails"] = new NativeFunction((values) => { List <Mission> missions = new List <Mission>(); missions = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).missions.ToList(); Mission result = missions?.FirstOrDefault(v => v.missionid == values[0].AsNumber); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["RouteDetails"] = new NativeFunction((values) => { string result = null; string value = values[0].AsString; if (value == null || value == "") { return(null); } switch (value) { case "expiring": { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetExpiringRoute(); } break; case "farthest": { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetFarthestRoute(); } break; case "most": { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetMostRoute(); } break; case "nearest": { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetNearestRoute(); } break; case "route": { if (values.Count == 2) { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetMissionsRoute(values[1].AsString); } else { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).GetMissionsRoute(); } } break; case "source": { if (values.Count == 2) { result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetSourceRoute(values[1].AsString); } else { result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetSourceRoute(); } } break; case "update": { if (values.Count == 2) { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).UpdateMissionsRoute(values[1].AsString); } else { result = ((MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor")).UpdateMissionsRoute(); } } break; } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1, 2); store["StationDetails"] = new NativeFunction((values) => { Station result; if (values.Count == 0) { result = EDDI.Instance.CurrentStation; } if (values[0]?.AsString?.ToLowerInvariant() == EDDI.Instance.CurrentStation?.name?.ToLowerInvariant()) { result = EDDI.Instance.CurrentStation; } else { StarSystem system; if (values.Count == 1 || values[1]?.AsString?.ToLowerInvariant() == EDDI.Instance.CurrentStarSystem?.name?.ToLowerInvariant()) { // Current system system = EDDI.Instance.CurrentStarSystem; } else { // Named system system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true); } result = system != null && system.stations != null ? system.stations.FirstOrDefault(v => v.name.ToLowerInvariant() == values[0].AsString.ToLowerInvariant()) : null; } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1, 2); store["SuperpowerDetails"] = new NativeFunction((values) => { Superpower result = Superpower.FromName(values[0].AsString); if (result == null) { result = Superpower.FromNameOrEdName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["StateDetails"] = new NativeFunction((values) => { FactionState result = FactionState.FromName(values[0].AsString); if (result == null) { result = FactionState.FromName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["EconomyDetails"] = new NativeFunction((values) => { Economy result = Economy.FromName(values[0].AsString); if (result == null) { result = Economy.FromName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["EngineerDetails"] = new NativeFunction((values) => { Engineer result = Engineer.FromName(values[0].AsString); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["GovernmentDetails"] = new NativeFunction((values) => { Government result = Government.FromName(values[0].AsString); if (result == null) { result = Government.FromName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["SecurityLevelDetails"] = new NativeFunction((values) => { SecurityLevel result = SecurityLevel.FromName(values[0].AsString); if (result == null) { result = SecurityLevel.FromName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["MaterialDetails"] = new NativeFunction((values) => { if (string.IsNullOrEmpty(values[0].AsString)) { return(new ReflectionValue(new object())); } Material result = Material.FromName(values[0].AsString); if (result == null) { result = Material.FromName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["CommodityMarketDetails"] = new NativeFunction((values) => { CommodityMarketQuote result = null; CommodityMarketQuote CommodityDetails(string commodityLocalizedName, Station station) { return(station?.commodities?.FirstOrDefault(c => c.localizedName == commodityLocalizedName)); } if (values.Count == 1) { // Named commodity, current station Station station = EDDI.Instance.CurrentStation; result = CommodityDetails(values[0].AsString, station); } else if (values.Count == 2) { // Named commodity, named station, current system StarSystem system = EDDI.Instance.CurrentStarSystem; string stationName = values[1].AsString; Station station = system?.stations?.FirstOrDefault(v => v.name == stationName); result = CommodityDetails(values[0].AsString, station); } else if (values.Count == 3) { // Named commodity, named station, named system StarSystem system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[2].AsString); string stationName = values[1].AsString; Station station = system?.stations?.FirstOrDefault(v => v.name == stationName); result = CommodityDetails(values[0].AsString, station); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 0, 3); store["CargoDetails"] = new NativeFunction((values) => { Cottle.Value value = values[0]; Cargo result = null; string edname = string.Empty; if (value.Type == Cottle.ValueContent.String) { edname = CommodityDefinition.FromNameOrEDName(value.AsString).edname; result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetCargoWithEDName(edname); } else if (value.Type == Cottle.ValueContent.Number) { result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetCargoWithMissionId((long)value.AsNumber); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["HaulageDetails"] = new NativeFunction((values) => { Haulage result = null; result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetHaulageWithMissionId((long)values[0].AsNumber); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["BlueprintDetails"] = new NativeFunction((values) => { BlueprintMaterials result = BlueprintMaterials.FromName(values[0].AsString); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["GalnetNewsArticle"] = new NativeFunction((values) => { News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["GalnetNewsArticles"] = new NativeFunction((values) => { List <News> results = null; if (values.Count == 0) { // Obtain all unread articles results = GalnetSqLiteRepository.Instance.GetArticles(); } else if (values.Count == 1) { // Obtain all unread news of a given category results = GalnetSqLiteRepository.Instance.GetArticles(values[0].AsString); } else if (values.Count == 2) { // Obtain all news of a given category results = GalnetSqLiteRepository.Instance.GetArticles(values[0].AsString, values[1].AsBoolean); } return(results == null ? new ReflectionValue(new List <News>()) : new ReflectionValue(results)); }, 0, 2); store["GalnetNewsMarkRead"] = new NativeFunction((values) => { News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString); if (result != null) { GalnetSqLiteRepository.Instance.MarkRead(result); } return(""); }, 1); store["GalnetNewsMarkUnread"] = new NativeFunction((values) => { News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString); if (result != null) { GalnetSqLiteRepository.Instance.MarkUnread(result); } return(""); }, 1); store["GalnetNewsDelete"] = new NativeFunction((values) => { News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString); if (result != null) { GalnetSqLiteRepository.Instance.DeleteNews(result); } return(""); }, 1); store["Distance"] = new NativeFunction((values) => { decimal result = 0; Cottle.Value value = values[0]; if (values.Count == 2 && value.Type == Cottle.ValueContent.String) { StarSystem curr = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[0].AsString, true); StarSystem dest = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true); if (curr != null & dest != null) { result = (decimal)Math.Round(Math.Sqrt(Math.Pow((double)(curr.x - dest.x), 2) + Math.Pow((double)(curr.y - dest.y), 2) + Math.Pow((double)(curr.z - dest.z), 2)), 2); } } else if (values.Count == 6 && value.Type == Cottle.ValueContent.Number) { result = (decimal)Math.Round(Math.Sqrt(Math.Pow((double)(values[0].AsNumber - values[3].AsNumber), 2) + Math.Pow((double)(values[1].AsNumber - values[4].AsNumber), 2) + Math.Pow((double)(values[2].AsNumber - values[5].AsNumber), 2)), 2); } return(new ReflectionValue(result)); }, 2, 6); store["Log"] = new NativeFunction((values) => { Logging.Info(values[0].AsString); return(""); }, 1); store["SetState"] = new NativeFunction((values) => { string name = values[0].AsString.ToLowerInvariant().Replace(" ", "_"); Cottle.Value value = values[1]; if (value.Type == Cottle.ValueContent.Boolean) { EDDI.Instance.State[name] = value.AsBoolean; store["state"] = buildState(); } else if (value.Type == Cottle.ValueContent.Number) { EDDI.Instance.State[name] = value.AsNumber; store["state"] = buildState(); } else if (value.Type == Cottle.ValueContent.String) { EDDI.Instance.State[name] = value.AsString; store["state"] = buildState(); } // Ignore other possibilities return(""); }, 2); // Variables foreach (KeyValuePair <string, Cottle.Value> entry in vars) { store[entry.Key] = entry.Value; } return(store); }
public void TestCargoMissionScenario() { var privateObject = new PrivateObject(cargoMonitor); Haulage haulage = new Haulage(); // CargoEvent line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":32, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); // CargoMissionAcceptedEvent - Check to see if this is a cargo mission and update our inventory accordingly line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 4 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375660729 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); // Verify cargo populated properly cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual("Structural Regulators", cargo.invariantName); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage + cargo.stolen + cargo.owned); Assert.AreEqual(7, cargo.need); Assert.AreEqual(2, cargo.haulageData.Count); // Verify haulage populated properly haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 375682327); Assert.AreEqual(3, haulage.amount); Assert.AreEqual("Mission_Salvage_Planet", haulage.name); Assert.AreEqual(DateTime.Parse("2018-05-12T15:20:27Z").ToUniversalTime(), haulage.expiry); // Verify duplication protection events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(7, cargo.need); Assert.AreEqual(2, cargo.haulageData.Count); // CargoEvent - Collected 2 Structural Regulators for mission ID 375682327. Verify haulage changed but not need line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":34, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"structuralregulators\", \"MissionID\":375682327, \"Count\":2, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(2, cargo.total); Assert.AreEqual(2, cargo.haulage); Assert.AreEqual(7, cargo.need); Assert.AreEqual(0, cargo.stolen + cargo.owned); // Cargo MissionAbandonedEvent - Verify haulage data for for mission ID 375682327 has been removed line = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionAbandoned"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAbandonedEvent", new object[] { events[0] }); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 375682327); Assert.IsNull(haulage); // CargoEvent - Verify 2 stolen Structural Regulators and 4 still needed for mission ID 37566072 line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":34, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"structuralregulators\", \"Count\":2, \"Stolen\":2 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.AreEqual(2, cargo.total); Assert.AreEqual(2, cargo.stolen); Assert.AreEqual(4, cargo.need); Assert.AreEqual(0, cargo.haulage + cargo.owned); // CargoMissionCompletedEvent - Verify haulage data & cargo has been removed line = @"{ ""timestamp"": ""2018-05-05T22:27:58Z"", ""event"": ""MissionCompleted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet_name"", ""MissionID"": 375660729, ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Reward"": 624016, ""FactionEffects"": [ { ""Faction"": ""Merope Expeditionary Fleet"", ""Effects"": [ { ""Effect"": ""$MISSIONUTIL_Interaction_Summary_civilUnrest_down;"", ""Effect_Localised"": ""$#MinorFaction; are happy to report improved civil contentment, making a period of civil unrest unlikely."", ""Trend"": ""DownGood"" } ], ""Influence"": [ { ""SystemAddress"": 224644818084, ""Trend"": ""UpGood"" } ], ""Reputation"": ""UpGood"" } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionCompletedEvent", new object[] { events[0] }); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 375660729); Assert.IsNull(haulage); line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":32, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.IsNull(cargo); // CargoMissionFailedEvent line = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.IsNotNull(cargo); line = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionFailed"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionFailedEvent", new object[] { events[0] }); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 375682327); Assert.IsNull(haulage); line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":32, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators"); Assert.IsNull(cargo); // CargoDepotEvent - Check response for missed 'Mission accepted' event. Verify both cargo and haulage are created line = @"{ ""timestamp"":""2018-08-26T02:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748324, ""UpdateType"":""Deliver"", ""CargoType"":""Tantalum"", ""Count"":54, ""StartMarketID"":0, ""EndMarketID"":3224777216, ""ItemsCollected"":0, ""ItemsDelivered"":54, ""TotalItemsToDeliver"":70, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Tantalum"); Assert.IsNotNull(cargo); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage + cargo.owned); Assert.AreEqual(16, cargo.need); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 413748324); Assert.IsNotNull(haulage); Assert.AreEqual(16, haulage.remaining); Assert.IsTrue(haulage.shared); // Cargo Delivery 'Mission accepted' Event with 'Cargo Depot' events line = @"{ ""timestamp"":""2018-08-26T00:50:48Z"", ""event"":""MissionAccepted"", ""Faction"":""Calennero State Industries"", ""Name"":""Mission_Delivery_Boom"", ""LocalisedName"":""Boom time delivery of 60 units of Silver"", ""Commodity"":""$Silver_Name;"", ""Commodity_Localised"":""Silver"", ""Count"":60, ""DestinationSystem"":""HIP 20277"", ""DestinationStation"":""Fabian City"", ""Expiry"":""2018-08-27T00:48:38Z"", ""Wing"":false, ""Influence"":""Med"", ""Reputation"":""Med"", ""Reward"":25000000, ""MissionID"":413748339 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleMissionAcceptedEvent", new object[] { events[0] }); cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Silver"); Assert.IsNotNull(cargo); Assert.AreEqual(0, cargo.total); Assert.AreEqual(0, cargo.haulage + cargo.owned); Assert.AreEqual(60, cargo.need); haulage = cargo.haulageData.FirstOrDefault(h => h.missionid == 413748339); Assert.IsNotNull(haulage); Assert.AreEqual(60, haulage.remaining); Assert.IsFalse(haulage.shared); line = @"{ ""timestamp"":""2018-08-26T02:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748339, ""UpdateType"":""Collect"", ""CargoType"":""Silver"", ""Count"":60, ""StartMarketID"":3225297216, ""EndMarketID"":3224777216, ""ItemsCollected"":60, ""ItemsDelivered"":0, ""TotalItemsToDeliver"":60, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); line = "{ \"timestamp\":\"2018-10-31T03:39:10Z\", \"event\":\"Cargo\", \"Count\":92, \"Inventory\":[ { \"Name\":\"hydrogenfuel\", \"Name_Localised\":\"Hydrogen Fuel\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"biowaste\", \"MissionID\":426282789, \"Count\":30, \"Stolen\":0 }, { \"Name\":\"animalmeat\", \"Name_Localised\":\"Animal Meat\", \"Count\":1, \"Stolen\":0 }, { \"Name\":\"silver\", \"MissionID\":413748339, \"Count\":60, \"Stolen\":0 } ] }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoEvent", new object[] { events[0] }); Assert.AreEqual(60, cargo.total); Assert.AreEqual(60, cargo.haulage); Assert.AreEqual(60, cargo.need); Assert.AreEqual(60, haulage.remaining); Assert.AreEqual(3225297216, haulage.startmarketid); Assert.AreEqual(3224777216, haulage.endmarketid); line = @"{ ""timestamp"":""2018-08-26T03:55:10Z"", ""event"":""CargoDepot"", ""MissionID"":413748339, ""UpdateType"":""Deliver"", ""CargoType"":""Silver"", ""Count"":60, ""StartMarketID"":3225297216, ""EndMarketID"":3224777216, ""ItemsCollected"":60, ""ItemsDelivered"":60, ""TotalItemsToDeliver"":60, ""Progress"":0.000000 }"; events = JournalMonitor.ParseJournalEntry(line); privateObject.Invoke("_handleCargoDepotEvent", new object[] { events[0] }); Assert.AreEqual(0, haulage.remaining); Assert.AreEqual(0, haulage.need); Assert.AreEqual(0, cargo.need); }
public void TestCargoConfig() { string cargoConfigJson = @"{ ""cargo"": [{ ""edname"": ""DamagedEscapePod"", ""stolen"": 0, ""haulage"": 0, ""owned"": 4, ""need"": 0, ""total"": 4, ""ejected"": 0, ""price"": 11912, ""haulageData"": [{ ""missionid"": 413563829, ""name"": ""Mission_Salvage_Expansion"", ""typeEDName"": ""Salvage"", ""status"": ""Active"", ""originsystem"": ""HIP 20277"", ""sourcesystem"": ""Bunuson"", ""sourcebody"": null, ""amount"": 4, ""remaining"": 4, ""startmarketid"": 0, ""endmarketid"": 0, ""collected"": 0, ""delivered"": 0, ""expiry"": null, ""shared"": false }] }, { ""edname"": ""USSCargoBlackBox"", ""stolen"": 4, ""haulage"": 0, ""owned"": 0, ""need"": 0, ""total"": 4, ""ejected"": 0, ""price"": 6995, ""haulageData"": [] }, { ""edname"": ""Drones"", ""stolen"": 0, ""haulage"": 0, ""owned"": 21, ""need"": 0, ""total"": 21, ""ejected"": 0, ""price"": 101, ""haulageData"": [] }], ""cargocarried"": 29 }"; CargoMonitorConfiguration config = CargoMonitorConfiguration.FromJsonString(cargoConfigJson); Assert.AreEqual(3, config.cargo.Count); cargo = config.cargo.ToList().FirstOrDefault(c => c.edname == "DamagedEscapePod"); Assert.AreEqual("Damaged Escape Pod", cargo.commodityDef.invariantName); Assert.AreEqual(4, cargo.total); Assert.AreEqual(4, cargo.owned); Assert.AreEqual(0, cargo.need); Assert.AreEqual(0, cargo.stolen); Assert.AreEqual(0, cargo.haulage); Assert.AreEqual(11912, cargo.price); // Verify haulage object Assert.AreEqual(1, cargo.haulageData.Count()); Haulage haulage = cargo.haulageData[0]; Assert.AreEqual(413563829, haulage.missionid); Assert.AreEqual("Mission_Salvage_Expansion", haulage.name); Assert.AreEqual("Salvage", haulage.typeEDName); Assert.AreEqual(4, haulage.amount); Assert.AreEqual(4, haulage.remaining); Assert.IsFalse(haulage.shared); }