using System.Security.Cryptography; using System.Text; using System.Text.Json; using XFEEDToolBox.Core.Models; namespace XFEEDToolBox.Core.Execution; /// One durable round per confirmed outbound sale, completed at home for return routes. public static class AutomaticTradeRounds { private sealed record Completion(int Schema,long SourceMarketId,long DestinationMarketId,bool ReturnTrade,string Commander,long ShipId, string OutboundPurchaseKey,string OutboundSaleKey,string OutboundJourney,DateTimeOffset OutboundSoldAt, DateTimeOffset CompletedAt,string Evidence,string? ReturnOperationKey=null); private sealed record Journey(string Directory,TradeLeg Leg,TradeJourneyCheckpoint Checkpoint); private sealed record Arrival(DateTimeOffset At,string Evidence,string? OperationKey); private static bool SameReceipt(ConfirmedTradeReceipt? first,ConfirmedTradeReceipt? second) =>first is null?second is null:second is not null&&first.OperationKey==second.OperationKey&&first.MarketId==second.MarketId &&first.Commodity==second.Commodity&&first.QuantityDelta==second.QuantityDelta&&first.UnitPrice==second.UnitPrice &&first.CreditsBefore==second.CreditsBefore&&first.CreditsAfter==second.CreditsAfter&&first.CargoBefore==second.CargoBefore&&first.CargoAfter==second.CargoAfter &&first.CommanderId==second.CommanderId&&first.ShipId==second.ShipId&&first.SessionStartedAt==second.SessionStartedAt&&first.ConfirmedAt==second.ConfirmedAt &&first.ServerConfirmed==second.ServerConfirmed&&first.ClientApplied==second.ClientApplied; private static bool SameCheckpoint(TradeJourneyCheckpoint first,TradeJourneyCheckpoint second) =>first.Commander==second.Commander&&first.ShipId==second.ShipId&&first.StartedAt==second.StartedAt &&SameReceipt(first.Bought,second.Bought)&&SameReceipt(first.Sold,second.Sold); public static int Refresh(AutomaticTradeConfiguration configuration,PlayerState current) { var root=configuration.WorkingDirectory; bool Outbound(TradeLeg leg)=>leg.To.MarketId==configuration.Destination.MarketId &&(configuration.UsesDynamicSources?configuration.Commodities.Length>0&&leg.Orders.Count>0 &&leg.From.MarketId!=configuration.Destination.MarketId&&leg.Orders.All(o=>configuration.Commodities.Contains(o.Commodity,StringComparer.Ordinal)) :leg.From.MarketId==configuration.Source.MarketId); bool SameGoal(Journey journey) { if(configuration.Mode!=AutomaticTradeMode.CommunityGoal||configuration.CommunityGoalId is null)return true; var file=Path.Combine(journey.Directory,"membership-before.json");if(!File.Exists(file))return false; using var proof=JsonDocument.Parse(File.ReadAllText(file)); return proof.RootElement.GetProperty("goalId").GetUInt64()==configuration.CommunityGoalId; } if(!Directory.Exists(root)||string.IsNullOrWhiteSpace(current.CommanderId)||current.CurrentShipId is not {} ship)return 0; var completed=new List();var purchases=new List(); foreach(var directory in Directory.EnumerateDirectories(root,"journey-*")) { var checkpointPath=Path.Combine(directory,"journey.json"); if(!File.Exists(checkpointPath))continue; var checkpoint=TradeJourneyCheckpoints.Load(checkpointPath); if(checkpoint.Commander!=current.CommanderId||checkpoint.ShipId!=ship)continue; if(checkpoint.Bought is {} purchase)purchases.Add(purchase); var resultPath=Path.Combine(directory,"result.json");var legPath=Path.Combine(directory,"leg.json"); if(!File.Exists(resultPath)||!File.Exists(legPath))continue; var result=JsonSerializer.Deserialize(File.ReadAllText(resultPath),ToolboxSettings.JsonOptions); if(result is null||result.Checkpoint is null||!SameCheckpoint(result.Checkpoint,checkpoint)||checkpoint.Bought is not {} bought||checkpoint.Sold is not {} sold)continue; var leg=JsonSerializer.Deserialize(File.ReadAllText(legPath),ToolboxSettings.JsonOptions); if(leg is null||!result.Passed&&!HistoricalTradeCompletion.IsRecovered(directory,leg,current))continue; if(leg is null||leg.Orders.Count!=1||bought.MarketId!=leg.From.MarketId||sold.MarketId!=leg.To.MarketId ||bought.Commodity!=leg.Orders[0].Commodity||bought.QuantityDelta!=leg.Orders[0].Quantity||sold.ConfirmedAt>result.Finished)continue; completed.Add(new(directory,leg,checkpoint)); } var candidates=completed.Where(journey=>Outbound(journey.Leg)&&SameGoal(journey)) .DistinctBy(journey=>journey.Checkpoint.Sold!.OperationKey).OrderBy(journey=>journey.Checkpoint.Bought!.ConfirmedAt).ToArray(); // Two distinct batches cannot both occupy this ship's cargo hold over // overlapping intervals. Ambiguous records must not inflate rounds. var outbound=candidates.Where(journey=>!candidates.Any(other=>other!=journey &&other.Checkpoint.Bought!.ConfirmedAt(File.ReadAllText(path),ToolboxSettings.JsonOptions); if(existing is null||existing.Schema!=1||existing.SourceMarketId!=journey.Leg.From.MarketId||existing.DestinationMarketId!=configuration.Destination.MarketId ||existing.ReturnTrade!=configuration.ReturnTrade||existing.Commander!=current.CommanderId||existing.ShipId!=ship ||existing.OutboundPurchaseKey!=bought.OperationKey||existing.OutboundSaleKey!=sold.OperationKey||existing.OutboundSoldAt!=sold.ConfirmedAt ||existing.CompletedAtreceipt.MarketId==home&&receipt.ConfirmedAt>=sold.ConfirmedAt&&receipt.OperationKey!=bought.OperationKey) .Select(receipt=>new Arrival(receipt.ConfirmedAt,"confirmed-purchase-at-source",receipt.OperationKey)) .Concat(completed.Where(other=>other.Leg.From.MarketId==configuration.Destination.MarketId&&other.Leg.To.MarketId==home &&other.Checkpoint.Sold!.ConfirmedAt>=sold.ConfirmedAt) .Select(other=>new Arrival(other.Checkpoint.Sold!.ConfirmedAt,"confirmed-return-sale",other.Checkpoint.Sold.OperationKey))).ToList(); if(current.InGame&¤t.Docked&¤t.MarketId==home&¤t.LocationAt is {} dockedAt &&dockedAt>=sold.ConfirmedAt&&dockedAt<=DateTimeOffset.UtcNow.AddSeconds(1)) witnesses.Add(new(dockedAt,"journal-docked-at-source",null)); // Home must have been reached before the next outbound buy; // one later docking cannot close multiple prior rounds. var nextPurchase=index+1nextPurchase is null||witness.At<=nextPurchase).OrderBy(witness=>witness.At).FirstOrDefault(); } if(arrival is null)continue; var value=new Completion(1,journey.Leg.From.MarketId,configuration.Destination.MarketId,configuration.ReturnTrade,current.CommanderId,ship, bought.OperationKey,sold.OperationKey,Path.GetFileName(journey.Directory),sold.ConfirmedAt,arrival.At,arrival.Evidence,arrival.OperationKey); Directory.CreateDirectory(records);var temporary=path+"."+Guid.NewGuid().ToString("N")+".tmp"; try { using(var file=new FileStream(temporary,FileMode.CreateNew,FileAccess.Write,FileShare.Read,4096,FileOptions.WriteThrough)) {JsonSerializer.Serialize(file,value,ToolboxSettings.JsonOptions);file.Flush(true);} File.Move(temporary,path,false); } finally{if(File.Exists(temporary))File.Delete(temporary);} count++; } return count; } }