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

XFEWarframe

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

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

namespace XFEWarframe.Core.Services;

public static partial class RivenCalculator
{
    private static JObject Challenge(string type, string key) => (RivenApi.Metadata(Prefix + Family(type))["availableChallenges"] as JArray ?? [])
        .OfType<JObject>().FirstOrDefault(x => x.Value<string>("fullName") == key) ?? throw new ApiException(400, "该类紫卡不支持此揭示挑战。");

    private static JArray ChallengeCatalog(JObject meta, JObject dictionary) => new((meta["availableChallenges"] as JArray ?? []).OfType<JObject>().Select(x => new JObject
    {
        ["key"] = x["fullName"]!.DeepClone(), ["label"] = dictionary.Value<string>(x.Value<string>("description") ?? "") ?? x.Value<string>("fullName")!.Split('/').Last(),
        ["required"] = x["countRange"]![0]!.DeepClone(),
        ["complications"] = new JArray(new[] { new JObject { ["key"] = "", ["label"] = "无附加条件" } }.Concat((x["complications"] as JArray ?? []).OfType<JObject>().Select(c => new JObject
        { ["key"] = c["fullName"]!.DeepClone(), ["label"] = dictionary.Value<string>(c.Value<string>("description") ?? "") ?? c.Value<string>("fullName")!.Split('/').Last() })))
    }));

    private static JObject ParseVeiled(string type, JObject fingerprint)
    {
        var challenge = fingerprint["challenge"] as JObject ?? throw new ApiException(400, "揭示挑战必须为对象。");
        var meta = Challenge(type, challenge.RequiredString("Type"));
        if (challenge["Complication"] is JToken complication && complication.Type != JTokenType.Null &&
            !(meta["complications"] as JArray ?? []).Any(x => x.Value<string>("fullName") == complication.Value<string>())) throw new ApiException(400, "此挑战不支持所选附加条件。");
        double Number(string field)
        {
            if (challenge[field]?.Type is not (JTokenType.Integer or JTokenType.Float)) throw new ApiException(400, "缺少挑战数值 " + field);
            var value = challenge.Value<double>(field);
            if (!double.IsFinite(value) || value < 0 || value > 1_000_000_000) throw new ApiException(400, "挑战数值超出范围。");
            return value;
        }
        var required = Number("Required"); var progress = Number("Progress");
        if (required <= 0 || progress > required) throw new ApiException(400, "挑战目标必须大于零,当前进度不能超过目标。");
        if (fingerprint["IsSentinel"] is JToken sentinel && sentinel.Type != JTokenType.Boolean) throw new ApiException(400, "守护武器标记必须为布尔值。");
        if (fingerprint["buffs"] is not null || fingerprint["curses"] is not null) throw new ApiException(400, "未揭示紫卡不能同时包含已揭示词条。");
        return new JObject { ["name"] = $"未揭示紫卡 · {progress:0.##}/{required:0.##}", ["veiled"] = true, ["stats"] = new JArray(), ["fingerprint"] = fingerprint.DeepClone() };
    }

    private static JObject BuildVeiled(string type, JObject request)
    {
        var fp = request["baseFingerprint"] is { Type: not JTokenType.Null } original ? (JObject)Decode(original).DeepClone() : new JObject();
        foreach (var field in new[] { "compat", "pol", "buffs", "curses", "lvl", "lvlReq", "rerolls", "lim" }) fp.Remove(field);
        var challenge = fp["challenge"] is JObject existing ? (JObject)existing.DeepClone() : new JObject();
        challenge["Type"] = request.RequiredString("challengeType"); challenge["Required"] = request["required"]?.DeepClone(); challenge["Progress"] = request["progress"]?.DeepClone();
        if (request.Value<string>("complication") is { Length: > 0 } complication) challenge["Complication"] = complication; else challenge.Remove("Complication");
        fp["challenge"] = challenge;
        if (request.Value<bool>("sentinel")) fp["IsSentinel"] = true; else fp.Remove("IsSentinel");
        return ParseVeiled(type, fp);
    }
}