namespace XFEWarframe.Loadouts; public enum ModSlotKind { Normal, Aura, Stance, Exilus, Arcane } public sealed record ModSlot(int Index, ModSlotKind Kind, string Polarity, bool Locked = false); public sealed record ModCard(string Key, string Type, string Name, string Family, string Group, int Rank, int MaximumRank, int Drain, string Polarity, string Rarity, string Icon, string Fingerprint, int Count, int[] CompatibleSlots, string SearchText = "", string[]? Elements = null); public sealed record ModLoadout(int Capacity, bool CapacityKnown, ModSlot[] Slots, ModCard[] Cards); public sealed record ModCapacity(int Maximum, int Used, int Remaining, bool Known); /// Shared, UI-independent drag/drop and capacity rules. The server repeats validation before writing. public static class ModLoadoutRules { public static int EffectiveDrain(ModSlot slot, ModCard card) { if (slot.Kind == ModSlotKind.Arcane) return 0; var matching = slot.Polarity == "AP_ANY" || slot.Polarity == card.Polarity && slot.Polarity != "AP_UNIVERSAL"; var mismatch = !matching && slot.Polarity is not ("" or "AP_UNIVERSAL"); var drain = Math.Abs(card.Drain); if (slot.Kind is ModSlotKind.Aura or ModSlotKind.Stance) return -(matching ? drain * 2 : mismatch ? (int)Math.Round(drain * .8, MidpointRounding.AwayFromZero) : drain); return matching ? (drain + 1) / 2 : mismatch ? (int)Math.Round(drain * 1.25, MidpointRounding.AwayFromZero) : drain; } public static ModCapacity Capacity(ModLoadout loadout, IReadOnlyDictionary selection) { var maximum = loadout.Capacity; var used = 0; foreach (var slot in loadout.Slots) { if (!selection.TryGetValue(slot.Index, out var key) || key.Length == 0) continue; var card = loadout.Cards.FirstOrDefault(x => x.Key == key); if (card is null) continue; var cost = EffectiveDrain(slot, card); if (cost < 0) maximum -= cost; else used += cost; } return new(maximum, used, maximum - used, loadout.CapacityKnown); } public static string? Validate(ModLoadout loadout, IReadOnlyDictionary selection, IReadOnlyDictionary? original = null) { var groups = new HashSet(StringComparer.Ordinal); foreach (var (index, key) in selection) { var slot = loadout.Slots.FirstOrDefault(x => x.Index == index); if (slot is null) return "未知 MOD 槽位。"; if (key.Length == 0) continue; var unchanged = original is not null && original.GetValueOrDefault(index) == key; var card = loadout.Cards.FirstOrDefault(x => x.Key == key); if (card is null) { if (unchanged) continue; return "此 MOD 不在当前库存中。"; } if (slot.Locked && !unchanged) return "此槽位尚未解锁。"; if (!card.CompatibleSlots.Contains(index) && !unchanged) return "此 MOD 与槽位或装备不兼容。"; if (!groups.Add(card.Group) && (original is null || !Same(selection, original))) return "同一配装不能重复安装互斥的 MOD。"; } var capacity = Capacity(loadout, selection); if (capacity.Known && capacity.Remaining < 0 && (original is null || !Same(selection, original))) return "MOD 消耗超过装备容量。"; return null; } public static IReadOnlyDictionary Install(ModLoadout loadout, IReadOnlyDictionary selection, string key, int target, int? source = null) { if (source == target) return new Dictionary(selection); if (source is { } from && selection.GetValueOrDefault(from) != key) throw new InvalidOperationException("拖动来源已变化。请重新拖动。"); var next = new Dictionary(selection); if (source is { } old) next[old] = next.GetValueOrDefault(target) ?? ""; else { // Moving an installed UID from the collection follows the game's single-instance rule. foreach (var slot in next.Where(x => x.Value == key).Select(x => x.Key).ToArray()) next[slot] = ""; } next[target] = key; if (Validate(loadout, next, selection) is { } error) throw new InvalidOperationException(error); return next; } public static bool Same(IReadOnlyDictionary a, IReadOnlyDictionary b) => a.Count == b.Count && a.All(x => b.GetValueOrDefault(x.Key) == x.Value); }