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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Globalization;
using System.Text.Json;
using XFEEDToolBox.Core.Models;

namespace XFEEDToolBox.Core.Galaxy;

/// <summary>One original Activities influence pair, never a jump-range edge.
/// Null means unavailable; Eligible=false is a source-proven missing link.</summary>
public sealed record GalaxyNativePowerInfluence(bool Eligible,long FromSystemAddress=0,long ToSystemAddress=0,
    Coordinates? FromPosition=null,Coordinates? ToPosition=null,IReadOnlyList<float>? LinearRgb=null,int Kind=0,
    float Phase=0,float PhaseRate=0,ulong CapturedAtTick=0)
{
    public const string Source="original-power-influence";
    public static GalaxyNativePowerInfluence? FromWire(JsonElement value,long candidate,GalaxyNativePowerOuter outer)
    {
        if(value.ValueKind==JsonValueKind.Null)return null;
        try
        {
            if(value.ValueKind!=JsonValueKind.Object||value.GetProperty("source").GetString()!=Source||outer.Visible!=true)
                throw new InvalidDataException("原势力影响连线缺少对应活动视图。");
            var eligible=value.GetProperty("eligible");if(eligible.ValueKind is not(JsonValueKind.True or JsonValueKind.False))throw new InvalidDataException("原势力影响连线状态无效。");
            if(!eligible.GetBoolean())
            {
                if(value.EnumerateObject().Any(p=>p.Name is not("source" or "eligible")))throw new InvalidDataException("原势力影响连线的负回执包含几何资料。");
                return new(false);
            }
            long Address(string key)
            {
                var v=value.GetProperty(key);
                return v.ValueKind==JsonValueKind.String&&long.TryParse(v.GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var id)&&id>0&&id<(1L<<55)
                    ?id:throw new InvalidDataException("原势力影响连线地址无效。");
            }
            Coordinates Position(string key)
            {
                var v=value.GetProperty(key);if(v.ValueKind!=JsonValueKind.Array||v.GetArrayLength()!=3)throw new InvalidDataException("原势力影响连线坐标无效。");
                var position=new Coordinates(v[0].GetDouble(),v[1].GetDouble(),v[2].GetDouble());
                return position.IsValid?position:throw new InvalidDataException("原势力影响连线坐标无效。");
            }
            var from=Address("fromSystemAddress");var to=Address("toSystemAddress");var a=Position("fromPosition");var b=Position("toPosition");
            if(from==to||candidate==outer.CentreSystemAddress||
                !(from==outer.CentreSystemAddress&&to==candidate&&a==outer.CentrePosition||to==outer.CentreSystemAddress&&from==candidate&&b==outer.CentrePosition))
                throw new InvalidDataException("原势力影响连线没有绑定实际查询目标和中心。");
            var colours=value.GetProperty("linearRgb");if(colours.ValueKind!=JsonValueKind.Array||colours.GetArrayLength()!=3)throw new InvalidDataException("原势力影响连线颜色无效。");
            var rgb=colours.EnumerateArray().Select(v=>v.GetSingle()).ToArray();var kind=value.GetProperty("kind").GetInt32();
            var phase=value.GetProperty("phase").GetSingle();var rate=value.GetProperty("phaseRate").GetSingle();var tick=value.GetProperty("capturedAtTick");
            if(rgb.Any(v=>!float.IsFinite(v)||v<0)||kind is <0 or >1||!float.IsFinite(phase)||!float.IsFinite(rate)||tick.ValueKind!=JsonValueKind.String||
                !ulong.TryParse(tick.GetString(),NumberStyles.None,CultureInfo.InvariantCulture,out var captured)||captured==0)
                throw new InvalidDataException("原势力影响连线动画或颜色无效。");
            return new(true,from,to,a,b,rgb,kind,phase,rate,captured);
        }
        catch(Exception error)when(error is KeyNotFoundException or InvalidOperationException or FormatException or OverflowException)
        {throw new InvalidDataException("原势力影响连线回执无效。",error);}
    }
}