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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Core.Galaxy;

public sealed record GalaxyOriginalTradeCategory(int Id,string Name,string Label);
public sealed record GalaxyOriginalTradeCommodity(string Name,int CategoryId);

/// <summary>The original TradeRoute tree is independent of the larger market commodity taxonomy.</summary>
public static class GalaxyOriginalTradeCategories
{
    private sealed record Resource(string FrozenSha256,IReadOnlyList<GalaxyOriginalTradeCategory> Categories,IReadOnlyList<GalaxyOriginalTradeCommodity> Commodities);
    private static readonly Lazy<Resource> resource=new(()=>
    {
        using var stream=typeof(GalaxyOriginalTradeCategories).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Galaxy.original-trade-categories.json")!;
        return JsonSerializer.Deserialize<Resource>(stream,new JsonSerializerOptions(JsonSerializerDefaults.Web))??throw new InvalidDataException("原贸易路线商品分类目录无效。");
    });
    public static IReadOnlyList<GalaxyOriginalTradeCategory> Categories=>resource.Value.Categories;
    public static IReadOnlyList<GalaxyOriginalTradeCommodity> Commodities=>resource.Value.Commodities;
    public static string FrozenSha256=>resource.Value.FrozenSha256;
    // These are the original GMTradeCategory radio values (NPC/Players/All),
    // not the 0..12 commodity-parent IDs above.
    public static GalaxyTradeFlowSource? FromSavedTraderCategory(int value)=>value switch{1=>GalaxyTradeFlowSource.Npc,2=>GalaxyTradeFlowSource.Players,3=>GalaxyTradeFlowSource.All,_=>null};
    public static IReadOnlyList<GalaxyCommodityGroup> Build(IEnumerable<GalaxyFilterChoice> actualChoices)
    {
        var leaves=Commodities.ToDictionary(c=>GalaxyMapFilter.Normalize(c.Name),StringComparer.Ordinal);
        // Observed Market identities may translate a display alias to a symbol;
        // they never provide this tree's parent category or add a missing leaf.
        var aliases=new Dictionary<string,string>(StringComparer.Ordinal);
        foreach(var definition in GalaxyCommodityTree.Definitions)
        {var symbol=GalaxyMapFilter.Normalize(definition.Symbol);aliases[GalaxyMapFilter.Normalize(definition.Name)]=symbol;aliases[symbol]=symbol;}
        var groups=actualChoices.DistinctBy(c=>c.Key).Select(c=>
        {
            var key=GalaxyMapFilter.Normalize(c.Key);var original=leaves.GetValueOrDefault(key)??leaves.GetValueOrDefault(aliases.GetValueOrDefault(key)??key);
            return(Choice:c,Category:original?.CategoryId);
        }).GroupBy(c=>c.Category).OrderBy(g=>g.Key??int.MaxValue);
        return groups.Select(g=>
        {
            var category=g.Key is {} id?Categories.Single(c=>c.Id==id):null;
            return new GalaxyCommodityGroup(category is null?"__unknownoriginaltradecategory":"originaltrade:"+category.Id,category?.Label??"原贸易分类尚未确认",g.Select(c=>c.Choice).OrderBy(c=>c.Label,StringComparer.OrdinalIgnoreCase).ToArray(),category is not null);
        }).ToArray();
    }
}