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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

/// <summary>Retains the game's route when its Journal later supplies only a suffix or clears it on arrival.</summary>
public sealed class NavigationItinerary
{
    private readonly List<NavigationLegPreview> legs=[];
    public void Reset()=>legs.Clear();
    public void UpdateRoute(IReadOnlyList<NavigationLegPreview> incoming)
    {
        if(incoming.Count==0)return;
        var offset=legs.FindIndex(l=>l.SystemAddress==incoming[0].SystemAddress);
        if(offset>=0&&offset+incoming.Count==legs.Count&&incoming.Select(l=>l.SystemAddress).SequenceEqual(legs.Skip(offset).Select(l=>l.SystemAddress)))
            for(var i=0;i<incoming.Count;i++)legs[offset+i]=incoming[i] with{Number=legs[offset+i].Number,Completed=legs[offset+i].Completed||incoming[i].Completed};
        else{legs.Clear();legs.AddRange(incoming.Select((l,i)=>l with{Number=i+1}));}
    }
    public void ConfirmPosition(long address)
    {
        if(address<=0)return;
        var index=legs.FindIndex(l=>l.SystemAddress==address);
        if(index<0)return;
        for(var i=0;i<=index;i++)legs[i]=legs[i] with{Completed=true,Next=false};
    }
    public IReadOnlyList<NavigationLegPreview> Present(NavigationRunState state,string message="",string nextSystem="")
    {
        var next=legs.FindIndex(l=>!l.Completed&&l.Name.Equals(nextSystem,StringComparison.OrdinalIgnoreCase));
        if(next<0)next=legs.FindIndex(l=>!l.Completed);
        return legs.Select((leg,index)=>
        {
            var status=leg.Completed?NavigationLegState.Completed:index!=next?NavigationLegState.Waiting:state switch
            {NavigationRunState.Running=>NavigationLegState.Running,NavigationRunState.Failed=>NavigationLegState.Error,NavigationRunState.Stopped=>NavigationLegState.Warning,_=>NavigationLegState.Waiting};
            return leg with{Next=index==next,ExecutionState=status,Message=index==next&&status is NavigationLegState.Running or NavigationLegState.Error or NavigationLegState.Warning?message:""};
        }).ToArray();
    }
}