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); /// The original TradeRoute tree is independent of the larger market commodity taxonomy. public static class GalaxyOriginalTradeCategories { private sealed record Resource(string FrozenSha256,IReadOnlyList Categories,IReadOnlyList Commodities); private static readonly Lazy resource=new(()=> { using var stream=typeof(GalaxyOriginalTradeCategories).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Galaxy.original-trade-categories.json")!; return JsonSerializer.Deserialize(stream,new JsonSerializerOptions(JsonSerializerDefaults.Web))??throw new InvalidDataException("原贸易路线商品分类目录无效。"); }); public static IReadOnlyList Categories=>resource.Value.Categories; public static IReadOnlyList 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 Build(IEnumerable 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(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(); } }