using System.Text.Json; namespace XFEEDToolBox.Core.Navigation; /// Recognizes a completed optional SCO refusal without weakening primary flight checks. public static class ScoStartRefusal { public static bool CanContinueOrdinaryFlight(JsonElement status,JsonElement receipt,string owner,string requestId,ulong generation,ulong selection, SystemFlightFrame flight,DateTimeOffset now) { static bool Is(JsonElement e,string key,JsonValueKind kind)=>e.ValueKind==JsonValueKind.Object&&e.TryGetProperty(key,out var v)&&v.ValueKind==kind; static string? Text(JsonElement e,string key)=>Is(e,key,JsonValueKind.String)?e.GetProperty(key).GetString():null; static ulong Id(JsonElement e,string key)=>ulong.TryParse(Text(e,key),out var value)?value:0; static bool Number(JsonElement e,string key,ulong expected)=>Is(e,key,JsonValueKind.Number)&&e.GetProperty(key).TryGetUInt64(out var value)&&value==expected; if(!flight.InGame||!flight.GeometryVerified||flight.PlayerOverride||flight.ExternalThreat||flight.Mode!=LocalFlightMode.Supercruise||flight.ShipEntity==0||flight.ReferenceWorld==0 ||flight.ObservedAt>now||now-flight.ObservedAt>TimeSpan.FromMilliseconds(300)||string.IsNullOrEmpty(owner)||string.IsNullOrEmpty(requestId) ||!Is(status,"owned",JsonValueKind.False)||!Is(receipt,"failed",JsonValueKind.True)||!Is(receipt,"applied",JsonValueKind.False) ||!Is(receipt,"nativeMethodCalled",JsonValueKind.False)||Text(receipt,"owner")!=owner||Text(receipt,"requestId")!=requestId ||generation==0||selection==0||!Number(receipt,"generation",generation)||!Number(receipt,"selectionGeneration",selection) ||Text(receipt,"reason") is not("geometry-or-assist" or "approach-or-alignment" or "native-refusal" or "fuel" or "heat" or "thermal-model") ||!status.TryGetProperty("sample",out var sample)||sample.ValueKind!=JsonValueKind.Object ||Id(sample,"entityId")!=flight.ShipEntity||Id(sample,"referenceWorldAddress")!=flight.ReferenceWorld ||!Is(sample,"verified",JsonValueKind.True)||!Is(sample,"inSupercruise",JsonValueKind.True)||!Is(sample,"stableFsd",JsonValueKind.True) ||!Number(sample,"rawFsdState",2)||!Is(sample,"playerOverride",JsonValueKind.False)||!Is(sample,"state",JsonValueKind.Number) ||!sample.GetProperty("state").TryGetInt32(out var stateId)||stateId is not(0 or 3) ||!Is(sample,"observedAtUnixMilliseconds",JsonValueKind.Number)||!sample.GetProperty("observedAtUnixMilliseconds").TryGetInt64(out var milliseconds) ||milliseconds>now.ToUnixTimeMilliseconds()||milliseconds1)return false; return true; } }