using System; using System.Collections.Generic; using System.Linq; using Aetheria.State.Documents; namespace Aetheria.State; public sealed class AetheriaCatalogSnapshot { private readonly Dictionary _itemsByLegacyId; private readonly Dictionary _corporationsByLegacyId; private readonly Dictionary _nameFilesByLegacyId; public AetheriaCatalogSnapshot( IEnumerable items, IEnumerable corporations, IEnumerable nameFiles) { Items = items.OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase).ToArray(); Corporations = corporations.OrderBy(corporation => corporation.Name, StringComparer.OrdinalIgnoreCase).ToArray(); NameFiles = nameFiles.OrderBy(nameFile => nameFile.Name, StringComparer.OrdinalIgnoreCase).ToArray(); _itemsByLegacyId = Items .Where(item => !string.IsNullOrWhiteSpace(item.LegacyId)) .ToDictionary(item => item.LegacyId, StringComparer.OrdinalIgnoreCase); _corporationsByLegacyId = Corporations .Where(corporation => !string.IsNullOrWhiteSpace(corporation.LegacyId)) .ToDictionary(corporation => corporation.LegacyId, StringComparer.OrdinalIgnoreCase); _nameFilesByLegacyId = NameFiles .Where(nameFile => !string.IsNullOrWhiteSpace(nameFile.LegacyId)) .ToDictionary(nameFile => nameFile.LegacyId, StringComparer.OrdinalIgnoreCase); } public IReadOnlyList Items { get; } public IReadOnlyList Corporations { get; } public IReadOnlyList NameFiles { get; } public IEnumerable TradeItems => Items.Where(item => item.Price > 0).OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase); public IEnumerable EquipmentItems => Items.Where(item => !string.IsNullOrWhiteSpace(item.HardpointType)) .OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase); public IEnumerable FindItemsByBehavior(string behaviorKind) { return string.IsNullOrWhiteSpace(behaviorKind) ? [] : Items .Where(item => item.BehaviorKinds.Contains(behaviorKind, StringComparer.OrdinalIgnoreCase)) .OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase); } public IEnumerable FindItemsByHardpoint(string hardpointType) { return string.IsNullOrWhiteSpace(hardpointType) ? [] : Items .Where(item => string.Equals(item.HardpointType, hardpointType, StringComparison.OrdinalIgnoreCase)) .OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase); } public AetheriaItemDefinition? FindItemByLegacyId(string legacyId) { return TryGet(_itemsByLegacyId, legacyId); } public AetheriaCorporation? FindCorporationByLegacyId(string legacyId) { return TryGet(_corporationsByLegacyId, legacyId); } public AetheriaNameFile? FindNameFileByLegacyId(string legacyId) { return TryGet(_nameFilesByLegacyId, legacyId); } public AetheriaCorporation? FindCorporationByNamePrefix(string namePrefix) { return string.IsNullOrWhiteSpace(namePrefix) ? null : Corporations.FirstOrDefault(corporation => corporation.Name.StartsWith(namePrefix, StringComparison.InvariantCultureIgnoreCase)); } public AetheriaCorporation? GetManufacturer(AetheriaItemDefinition item) { return FindCorporationByLegacyId(item.ManufacturerLegacyId); } public AetheriaNameFile? GetNameFile(AetheriaCorporation corporation) { return FindNameFileByLegacyId(corporation.GeonameFileLegacyId); } private static T? TryGet(IReadOnlyDictionary dictionary, string key) where T : class { return string.IsNullOrWhiteSpace(key) ? null : dictionary.TryGetValue(key, out var value) ? value : null; } }