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; }
private bool handleMissionFine(DateTime timestamp, long missionid, long fine) { bool update = false; MissionMonitor missionMonitor = (MissionMonitor)EDDI.Instance.ObtainMonitor("Mission monitor"); Mission mission = missionMonitor?.GetMissionWithMissionId(missionid); if (mission != null) { update = _handleMissionFine(timestamp, mission, fine); } 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 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; } }