XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFEEDToolBox.Core.Galaxy;

// Domain values only. Original 1/2/3 radio IDs use the explicit mapping in
// GalaxyOriginalTradeCategories; they are not commodity parent indices.
public enum GalaxyTradeFlowSource { All,Players,Npc }
public sealed record GalaxyHistoricalTradeEdge(long FromSystemAddress,long ToSystemAddress,string Commodity,double NpcAmount,double PlayerAmount,DateTimeOffset CachedAt,long CacheSystemAddress)
{
    public double Amount(GalaxyTradeFlowSource source)=>source switch{GalaxyTradeFlowSource.Players=>PlayerAmount,GalaxyTradeFlowSource.Npc=>NpcAmount,_=>PlayerAmount+NpcAmount};
}
public sealed record GalaxyHistoricalTradeFlow(GalaxyHistoricalTradeEdge Edge,GalacticSystem From,GalacticSystem To);
public sealed record GalaxyHistoricalTradeProjection(IReadOnlyList<GalaxyHistoricalTradeEdge> Edges,IReadOnlyList<GalaxyHistoricalTradeFlow> Flows,IReadOnlyList<long> MissingSystemAddresses)
{
    public DateTimeOffset? OldestCache=>Edges.Count>0?Edges.Min(e=>e.CachedAt):null;
    public DateTimeOffset? NewestCache=>Edges.Count>0?Edges.Max(e=>e.CachedAt):null;
}

/// <summary>Projects timestamped original account TRD records. It makes no live availability or TTL claim.</summary>
public static class GalaxyHistoricalTrade
{
    public static IReadOnlyList<GalaxyHistoricalTradeEdge> Select(IEnumerable<OriginalTradeDataSnapshot> snapshots,ulong commanderId,GalaxyTradeFlowSource source,IReadOnlySet<string>? commodities)
    {
        if(commanderId==0)return [];
        // Both endpoints may cache the same directed commodity flow. A newer
        // zero amount must supersede an older positive amount before selection.
        return snapshots.Where(s=>s.CommanderId==commanderId)
            .SelectMany(s=>s.Routes.Select(r=>new GalaxyHistoricalTradeEdge(r.FromSystemAddress,r.ToSystemAddress,r.Commodity,r.NpcAmount,r.PlayerAmount,s.Timestamp,s.SystemAddress)))
            .Where(e=>e.FromSystemAddress>0&&e.ToSystemAddress>0&&e.FromSystemAddress!=e.ToSystemAddress&&double.IsFinite(e.NpcAmount)&&double.IsFinite(e.PlayerAmount)&&e.NpcAmount>=0&&e.PlayerAmount>=0)
            .GroupBy(e=>(e.FromSystemAddress,e.ToSystemAddress,GalaxyMapFilter.Normalize(e.Commodity)))
            .Select(g=>g.OrderByDescending(e=>e.CachedAt).ThenBy(e=>e.CacheSystemAddress).First())
            .Where(e=>double.IsFinite(e.Amount(source))&&e.Amount(source)>0&&(commodities is null||commodities.Contains(GalaxyMapFilter.Normalize(e.Commodity))))
            .OrderBy(e=>e.FromSystemAddress).ThenBy(e=>e.ToSystemAddress).ThenBy(e=>e.Commodity,StringComparer.OrdinalIgnoreCase).ToArray();
    }
    public static GalaxyHistoricalTradeProjection Project(IReadOnlyList<GalaxyHistoricalTradeEdge> edges,IReadOnlyDictionary<long,GalacticSystem> systems)
    {
        var resolved=new List<GalaxyHistoricalTradeFlow>();var missing=new HashSet<long>();
        foreach(var edge in edges)
        {
            bool Try(long address,out GalacticSystem? system)
            {system=null;if(systems.TryGetValue(address,out var found)&&found.SystemAddress==address&&found.Position.IsValid&&!string.IsNullOrWhiteSpace(found.Name)){system=found;return true;}missing.Add(address);return false;}
            var fromKnown=Try(edge.FromSystemAddress,out var from);var toKnown=Try(edge.ToSystemAddress,out var to);
            if(fromKnown&&toKnown)resolved.Add(new(edge,from!,to!));
        }
        return new(edges,resolved,missing.Order().ToArray());
    }
}