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

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace XFEWarframe.Core.Services;

/// <summary>A separate owned MOD; the native card is only a game resource carrier.</summary>
public static class InfestationKing
{
    public const string Type = "/Lotus/Upgrades/XFEWarframe/InfestationKing";
    public const string Carrier = "/Lotus/Powersuits/Infestation/InfestPassiveAugmentCard";
    public const string EquippedField = "XFEInfestationKing";
    public static bool Compatible(string? suit) => suit is "/Lotus/Powersuits/Infestation/Infestation" or "/Lotus/Powersuits/Infestation/InfestationPrime";

    public static JObject Add(JObject inventory, int count)
    {
        if (count is < 1 or > 100) throw new ApiException(400, "每次最多添加 100 张感染之王。");
        var cards = new JArray();
        for (var i = 0; i < count; i++)
        {
            var card = new JObject { ["ItemId"] = Wire.Oid(Wire.NewId()), ["ItemType"] = Type, ["UpgradeFingerprint"] = "{\"lvl\":0}" };
            inventory.Array("Upgrades").Add(card); cards.Add(card.DeepClone());
        }
        return new JObject { ["Upgrades"] = cards };
    }

    public static void Project(JObject inventory)
    {
        var ids = inventory.Array("Upgrades").OfType<JObject>().Where(x => x.Value<string>("ItemType") == Type)
            .Select(x => Wire.Id(x["ItemId"])).ToHashSet(StringComparer.Ordinal);
        // Ignore imported/client-supplied flags: only an owned, installed card enables it.
        foreach (var category in Inventory.EquipmentKeys)
            foreach (var item in inventory.Array(category).OfType<JObject>())
                foreach (var config in (item["Configs"] as JArray ?? []).OfType<JObject>())
                {
                    config.Remove(EquippedField);
                    if (category == "Suits" && Compatible(item.Value<string>("ItemType")) &&
                        (config["Upgrades"] as JArray ?? []).Any(x => ids.Contains(Wire.Id(x)))) config[EquippedField] = true;
                }
        foreach (var card in inventory.Array("Upgrades").OfType<JObject>().Where(x => x.Value<string>("ItemType") == Type))
        {
            card["ItemType"] = Carrier;
            var fingerprint = JObject.Parse(card.Value<string>("UpgradeFingerprint") ?? "{}");
            fingerprint["xfe"] = "InfestationKing";
            card["UpgradeFingerprint"] = fingerprint.ToString(Formatting.None);
        }
    }

    // Requests use the resource carrier but identity is always resolved by owned ID.
    public static void ResolveRequest(JObject inventory, JObject request)
    {
        var ids = inventory.Array("Upgrades").OfType<JObject>().Where(x => x.Value<string>("ItemType") == Type)
            .Select(x => Wire.Id(x["ItemId"])).ToHashSet(StringComparer.Ordinal);
        foreach (var item in request.DescendantsAndSelf().OfType<JObject>())
            if (item.Value<string>("ItemType") == Carrier && ids.Contains(Wire.Id(item["ItemId"]))) item["ItemType"] = Type;
    }
}