From 366fe941b9f512b64987bd9475c005515f0ab2bb Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Fri, 3 Jan 2025 21:59:48 -0800 Subject: [PATCH] Update ship locations from carrier jumps. --- DataDefinitions/Ship.cs | 12 +++++------ JournalMonitor/JournalMonitor.cs | 2 +- ShipMonitor/FrontierApi.cs | 2 +- ShipMonitor/ShipMonitor.cs | 34 +++++++++++++++++++++++++++----- Tests/ShipMonitorTests.cs | 2 +- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/DataDefinitions/Ship.cs b/DataDefinitions/Ship.cs index 9d36767bac..9a7b5f998d 100644 --- a/DataDefinitions/Ship.cs +++ b/DataDefinitions/Ship.cs @@ -293,7 +293,7 @@ public bool hot private bool _hot = false; - public ShipLocation StoredLocation + public Location StoredLocation { get => _storedLocation; set @@ -309,7 +309,7 @@ public ShipLocation StoredLocation } } } - private ShipLocation _storedLocation; + private Location _storedLocation; /// the name of the system in which this ship is stored; null if the commander is in this ship [ Utilities.PublicAPI, JsonIgnore ] @@ -814,7 +814,7 @@ private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); } - public class ShipLocation + public class Location { public string systemName { get; set; } public ulong systemAddress { get; set; } @@ -826,7 +826,7 @@ public class ShipLocation // Default constructor [JsonConstructor] - public ShipLocation ( string systemName, ulong systemAddress, decimal? x, decimal? y, decimal? z, string stationName, long? marketId ) + public Location ( string systemName, ulong systemAddress, decimal? x, decimal? y, decimal? z, string stationName, long? marketId ) { this.systemName = systemName; this.systemAddress = systemAddress; @@ -837,7 +837,7 @@ public ShipLocation ( string systemName, ulong systemAddress, decimal? x, decima this.marketId = marketId; } - public ShipLocation ( [NotNull] StarSystem starSystem, string stationName, long? marketId ) + public Location ( [NotNull] StarSystem starSystem, string stationName, long? marketId ) { this.systemName = starSystem.systemname; this.systemAddress = starSystem.systemAddress; @@ -848,7 +848,7 @@ public ShipLocation ( [NotNull] StarSystem starSystem, string stationName, long? this.marketId = marketId; } - public ShipLocation ( [NotNull] NavWaypoint waypoint ) + public Location ( [NotNull] NavWaypoint waypoint ) { this.systemName = waypoint.systemName; this.systemAddress = waypoint.systemAddress; diff --git a/JournalMonitor/JournalMonitor.cs b/JournalMonitor/JournalMonitor.cs index e34d1ab784..f4380f5953 100644 --- a/JournalMonitor/JournalMonitor.cs +++ b/JournalMonitor/JournalMonitor.cs @@ -1317,7 +1317,7 @@ public static List ParseJournalEntry(string line, bool fromLogLoad = fals shipSystemName ?? system, shipMarketID ?? marketId ); ship.StoredLocation = stationWaypoint is null ? null - : new Ship.ShipLocation( stationWaypoint ); + : new Ship.Location( stationWaypoint ); ship.distance = ship.DistanceLY( EDDI.Instance.CurrentStarSystem ); shipyard.Add(ship); } diff --git a/ShipMonitor/FrontierApi.cs b/ShipMonitor/FrontierApi.cs index dc090ec0fe..721dc17f02 100644 --- a/ShipMonitor/FrontierApi.cs +++ b/ShipMonitor/FrontierApi.cs @@ -42,7 +42,7 @@ public static List ShipyardFromJson(Ship activeShip, dynamic json) ? null : EDDI.Instance.DataProvider.GetOrFetchStationWaypoint( (ulong)shipSystemAddress, (long)shipMarketId ); ship.StoredLocation = - stationWaypoint is null ? null : new Ship.ShipLocation( stationWaypoint ); + stationWaypoint is null ? null : new Ship.Location( stationWaypoint ); ship.distance = ship.DistanceLY( EDDI.Instance.CurrentStarSystem ); shipyard.Add( ship ); } diff --git a/ShipMonitor/ShipMonitor.cs b/ShipMonitor/ShipMonitor.cs index 0b602c2c45..e5164f6e96 100644 --- a/ShipMonitor/ShipMonitor.cs +++ b/ShipMonitor/ShipMonitor.cs @@ -123,7 +123,11 @@ public void Save() public void PreHandle(Event @event) { // Handle the events that we care about - if (@event is CommanderContinuedEvent commanderContinuedEvent) + if ( @event is CarrierJumpedEvent carrierJumpedEvent ) + { + handleCarrierJumpedEvent( carrierJumpedEvent ); + } + else if (@event is CommanderContinuedEvent commanderContinuedEvent) { handleCommanderContinuedEvent(commanderContinuedEvent); } @@ -241,6 +245,26 @@ public void PreHandle(Event @event) } } + private void handleCarrierJumpedEvent ( CarrierJumpedEvent @event ) + { + if ( @event.timestamp > updatedAt ) + { + lock ( shipyardLock ) + { + foreach ( var ship in shipyard ) + { + if ( ship.StoredLocation.marketId == @event.carrierId ) + { + ship.StoredLocation = new Ship.Location( @event.systemname, @event.systemAddress, @event.x, + @event.y, @event.z, @event.carriername, @event.carrierId ); + ship.distance = ship.DistanceLY( EDDI.Instance.CurrentStarSystem ); + } + } + writeShips(); + } + } + } + // Set the ship name conditionally, avoiding filtered names private void setShipName(Ship ship, string name) { @@ -338,7 +362,7 @@ private void handleShipPurchasedEvent(ShipPurchasedEvent @event) if (storedShip != null) { // Set location of stored ship to the current system - storedShip.StoredLocation = new Ship.ShipLocation( EDDI.Instance.CurrentStarSystem, EDDI.Instance?.CurrentStation?.name, EDDI.Instance?.CurrentStation?.marketId ); + storedShip.StoredLocation = new Ship.Location( EDDI.Instance.CurrentStarSystem, EDDI.Instance?.CurrentStation?.name, EDDI.Instance?.CurrentStation?.marketId ); storedShip.distance = 0; } } @@ -383,7 +407,7 @@ private void handleShipSwappedEvent(ShipSwappedEvent @event) // Set location of stored ship to the current system if ( EDDI.Instance?.CurrentStarSystem != null ) { - storedShip.StoredLocation = new Ship.ShipLocation( EDDI.Instance.CurrentStarSystem, + storedShip.StoredLocation = new Ship.Location( EDDI.Instance.CurrentStarSystem, EDDI.Instance?.CurrentStation?.name, EDDI.Instance?.CurrentStation?.marketId ); storedShip.distance = 0; } @@ -604,7 +628,7 @@ private void handleStoredShipsEvent(StoredShipsEvent @event) var stationData = systemData?.stations?.FirstOrDefault( s => s.marketId == ship.marketid ); ship.StoredLocation = systemData is null || stationData is null ? null - : new Ship.ShipLocation( systemData, stationData?.name, stationData?.marketId ); + : new Ship.Location( systemData, stationData?.name, stationData?.marketId ); ship.distance = ship.DistanceLY( EDDI.Instance?.CurrentStarSystem ); } else @@ -612,7 +636,7 @@ private void handleStoredShipsEvent(StoredShipsEvent @event) ship.StoredLocation = EDDI.Instance?.CurrentStarSystem is null || EDDI.Instance.CurrentStation is null ? null - : new Ship.ShipLocation( + : new Ship.Location( EDDI.Instance.CurrentStarSystem, EDDI.Instance.CurrentStation.name, EDDI.Instance.CurrentStation.marketId ); diff --git a/Tests/ShipMonitorTests.cs b/Tests/ShipMonitorTests.cs index 69ec42473b..c0b88da4eb 100644 --- a/Tests/ShipMonitorTests.cs +++ b/Tests/ShipMonitorTests.cs @@ -534,7 +534,7 @@ public void TestShipJumpedEvent() var ship = new Ship { LocalId = 9999, - StoredLocation = new Ship.ShipLocation( DeserializeJsonResource( Resources.sqlStarSystem6 ), "Furukawa Enterprise", 3534391808 ) + StoredLocation = new Ship.Location( DeserializeJsonResource( Resources.sqlStarSystem6 ), "Furukawa Enterprise", 3534391808 ) }; shipMonitor.RemoveShip(9999); shipMonitor.AddShip(ship);