Skip to content

Commit

Permalink
Update ship locations from carrier jumps.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkael committed Jan 4, 2025
1 parent f11fda2 commit 366fe94
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
12 changes: 6 additions & 6 deletions DataDefinitions/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public bool hot

private bool _hot = false;

public ShipLocation StoredLocation
public Location StoredLocation
{
get => _storedLocation;
set
Expand All @@ -309,7 +309,7 @@ public ShipLocation StoredLocation
}
}
}
private ShipLocation _storedLocation;
private Location _storedLocation;

/// <summary>the name of the system in which this ship is stored; null if the commander is in this ship</summary>
[ Utilities.PublicAPI, JsonIgnore ]
Expand Down Expand Up @@ -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; }
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion JournalMonitor/JournalMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ public static List<Event> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion ShipMonitor/FrontierApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static List<Ship> 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 );
}
Expand Down
34 changes: 29 additions & 5 deletions ShipMonitor/ShipMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -604,15 +628,15 @@ 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
{
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 );
Expand Down
2 changes: 1 addition & 1 deletion Tests/ShipMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void TestShipJumpedEvent()
var ship = new Ship
{
LocalId = 9999,
StoredLocation = new Ship.ShipLocation( DeserializeJsonResource<StarSystem>( Resources.sqlStarSystem6 ), "Furukawa Enterprise", 3534391808 )
StoredLocation = new Ship.Location( DeserializeJsonResource<StarSystem>( Resources.sqlStarSystem6 ), "Furukawa Enterprise", 3534391808 )
};
shipMonitor.RemoveShip(9999);
shipMonitor.AddShip(ship);
Expand Down

0 comments on commit 366fe94

Please sign in to comment.