Skip to content

Commit

Permalink
Remove null items from data provided from Spansh (for example if a ti…
Browse files Browse the repository at this point in the history
…meout occurs while debugging).
  • Loading branch information
Tkael committed Jan 18, 2025
1 parent 2b45450 commit 8df29e9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
12 changes: 8 additions & 4 deletions EddiSpanshService/SpanshDumpSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EddiDataDefinitions;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
Expand All @@ -11,6 +12,7 @@ namespace EddiSpanshService
public partial class SpanshService
{
// Uses the Spansh star system dump API (full star system data), e.g. https://www.spansh.co.uk/api/dump/10477373803
[CanBeNull]
public StarSystem GetStarSystem ( ulong systemAddress, bool showMarketDetails = false )
{
if ( systemAddress == 0 ) { return null; }
Expand All @@ -24,6 +26,7 @@ public StarSystem GetStarSystem ( ulong systemAddress, bool showMarketDetails =
return null;
}

[CanBeNull]
public StarSystem GetStarSystem ( string systemName, bool showMarketDetails = false )
{
if ( systemName == null || string.IsNullOrEmpty( systemName ) ) { return null; }
Expand All @@ -36,6 +39,7 @@ public IList<StarSystem> GetStarSystems ( ulong[] systemAddresses, bool showMark
{
return systemAddresses.AsParallel()
.Select( systemAddress => GetStarSystem( systemAddress, showMarketDetails ) )
.RemoveNulls()
.ToList();
}

Expand Down Expand Up @@ -67,7 +71,7 @@ private bool TryGetStarSystemDump ( IRestRequest request, out StarSystem fullSta
}
else
{
Logging.Debug( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
Logging.Warn( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
}

return fullStarSystem != null;
Expand Down Expand Up @@ -112,7 +116,7 @@ starSystem.y is null ||
starSystem.securityLevel = SecurityLevel.FromName( (string)data[ "security" ] ) ??
SecurityLevel.None;

starSystem.stations.AddRange( data[ "stations" ]?.AsParallel().Select( stationToken => ParseStation( starSystem, stationToken, null, showMarketDetails ) ).ToList() ?? new List<Station>() );
starSystem.stations.AddRange( data[ "stations" ]?.AsParallel().Select( stationToken => ParseStation( starSystem, stationToken, null, showMarketDetails ) ).RemoveNulls().ToList() ?? new List<Station>() );

starSystem.Power = Power.FromName( data[ "controllingPower" ]?.ToString() );
starSystem.powerState = PowerplayState.FromName( data[ "powerState" ]?.ToString() );
Expand All @@ -126,7 +130,7 @@ starSystem.y is null ||
// Get bodies
starSystem.totalbodies = data[ "bodyCount" ]?.ToObject<int>() ?? 0;
starSystem.AddOrUpdateBodies( data[ "bodies" ]?.AsParallel()
.Select( b => ParseBody( starSystem, b, showMarketDetails ) ).ToList() ?? new List<Body>() );
.Select( b => ParseBody( starSystem, b, showMarketDetails ) ).RemoveNulls().ToList() ?? new List<Body>() );

starSystem.lastupdated = DateTime.UtcNow;
return starSystem;
Expand Down Expand Up @@ -296,7 +300,7 @@ private static Body GetPlanetData( JToken planetData, StarSystem starSystem, str
} ).OrderByDescending( x => x.percent ).ToList() ?? new List<SolidComposition>();

var surfaceStations = planetData[ "stations" ]?.AsParallel().Select( s =>
ParseStation( starSystem, s, planetData, showMarketDetails ) ).ToList() ?? new List<Station>();
ParseStation( starSystem, s, planetData, showMarketDetails ) ).RemoveNulls().ToList() ?? new List<Station>();
starSystem.stations.AddRange( surfaceStations );

var terraformState = TerraformState.FromName( planetData[ "terraformingState" ]?.ToString() ) ??
Expand Down
2 changes: 1 addition & 1 deletion EddiSpanshService/SpanshQuerySystemName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<NavWaypoint> GetWaypointsBySystemName (string partialSystemName)
}
else
{
Logging.Debug("Spansh responded with " + clientResponse.ErrorMessage, clientResponse.ErrorException);
Logging.Warn("Spansh responded with " + clientResponse.ErrorMessage, clientResponse.ErrorException);
}
return new List<NavWaypoint>();
}
Expand Down
6 changes: 2 additions & 4 deletions EddiSpanshService/SpanshQuickStation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public NavWaypoint GetQuickStation (long marketId)

public IList<NavWaypoint> GetQuickStations ( long[] marketIds )
{
return marketIds.AsParallel()
.Select( GetQuickStation )
.ToList();
return marketIds.AsParallel().Select( GetQuickStation ).RemoveNulls().ToList();
}

private bool TryGetQuickStation ( IRestRequest request, out NavWaypoint quickStation )
Expand Down Expand Up @@ -56,7 +54,7 @@ private bool TryGetQuickStation ( IRestRequest request, out NavWaypoint quickSta
}
else
{
Logging.Debug( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
Logging.Warn( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
}

return quickStation != null;
Expand Down
6 changes: 2 additions & 4 deletions EddiSpanshService/SpanshQuickSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public StarSystem GetQuickStarSystem(ulong systemAddress)

public IList<StarSystem> GetQuickStarSystems ( ulong[] systemAddresses )
{
return systemAddresses.AsParallel()
.Select( GetQuickStarSystem )
.ToList();
return systemAddresses.AsParallel().Select( GetQuickStarSystem ).RemoveNulls().ToList();
}

private bool TryGetQuickSystem ( IRestRequest request, out StarSystem quickStarSystem )
Expand Down Expand Up @@ -56,7 +54,7 @@ private bool TryGetQuickSystem ( IRestRequest request, out StarSystem quickStarS
}
else
{
Logging.Debug( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
Logging.Warn( "Spansh responded with: " + clientResponse.ErrorMessage, clientResponse.ErrorException );
}

return quickStarSystem != null;
Expand Down
1 change: 1 addition & 0 deletions Tests/SpanshServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public void TestSpanshSystemDumpSol ()
fakeSpanshRestClient.Expect( "dump/10477373803", DeserializeJsonResource<string>( Resources.SpanshStarSystemDumpSol ) );
var result = fakeSpanshService.GetStarSystem(10477373803U, true);

Assert.IsNotNull( result );
Assert.AreEqual( 10477373803U, result.systemAddress);
Assert.AreEqual( 0.0M, result.x );
Assert.AreEqual( 0.0M, result.y );
Expand Down

0 comments on commit 8df29e9

Please sign in to comment.