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

XFEWarframe

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

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

namespace XFEWarframe.Core.Services;

public sealed class Dialogues(ItemCatalog catalog, TimeProvider clock)
{
    public static readonly HashSet<string> Controllers = ["saveDialogueController", "clearDialogueHistoryController"];
    public static JObject New(string name) => new() { ["DialogueName"] = name, ["Rank"] = 1, ["Chemistry"] = 0, ["AvailableDate"] = Wire.Date(0), ["AvailableGiftDate"] = Wire.Date(0), ["RankUpExpiry"] = Wire.Date(0), ["BountyChemExpiry"] = Wire.Date(0), ["QueuedDialogues"] = new JArray(), ["Gifts"] = new JArray(), ["Booleans"] = new JArray(), ["Completed"] = new JArray(), ["Counters"] = new JArray() };
    public static JObject Get(JObject inv, string name)
    {
        if (!name.StartsWith("/Lotus/", StringComparison.Ordinal) || name.Length > 512) throw new ApiException(400, "Invalid dialogue name");
        var dialogues = inv.Obj("DialogueHistory").Array("Dialogues"); var dialogue = dialogues.OfType<JObject>().FirstOrDefault(x => x.Value<string>("DialogueName") == name);
        if (dialogue is null) { dialogue = New(name); dialogues.Add(dialogue); } return dialogue;
    }
    public ApiResponse Handle(string controller, JObject account, ApiRequest request)
    {
        var body = request.ObjectBody(); var inv = account.Obj("Inventory"); var history = inv.Obj("DialogueHistory"); var now = clock.GetUtcNow().ToUnixTimeMilliseconds();
        if (controller == "clearDialogueHistoryController")
        {
            var names = body["Dialogues"] as JArray ?? throw new ApiException(400, "Missing dialogues"); var result = new JObject { ["Dialogues"] = names.DeepClone(), ["ClearPersist"] = body.Value<bool>("ClearPersist") };
            if (body.Value<string>("Pack") is string pack)
            {
                if (pack is not ("Hex" or "Roundtable" or "Triad")) throw new ApiException(400, "Invalid dialogue pack");
                var reset = history.Array("ResetDates").OfType<JObject>().FirstOrDefault(x => x.Value<string>("Pack") == pack);
                if (reset is null) { reset = new JObject { ["Pack"] = pack, ["Resets"] = 0 }; history.Array("ResetDates").Add(reset); }
                if (!inv.Value<bool>("noKimCooldowns") && Wire.DateMs(reset["Date"]) > now) throw new ApiException(409, "Dialogue reset is on cooldown");
                reset["Date"] = Wire.Date(now + (inv.Value<bool>("noKimCooldowns") ? 0 : 28 * WorldData.Day)); reset["Resets"] = reset.Value<int>("Resets") + 1; result["ResetDate"] = reset.DeepClone();
            }
            history["Resets"] = history.Value<int>("Resets") + 1;
            foreach (var name in names)
            {
                var old = history.Array("Dialogues").OfType<JObject>().FirstOrDefault(x => x.Value<string>("DialogueName") == name.ToString()); if (old is null) continue;
                if (body.Value<bool>("ClearPersist")) old.Remove();
                else { var next = New(name.ToString()); foreach (var key in new[] { "Completed", "Counters" }) next[key] = new JArray(old.Array(key).Where(x => x.Value<bool>("Persist")).Select(x => x.DeepClone())); old.Replace(next); }
            }
            return ApiResponse.Json(result);
        }
        if (body["YearIteration"] is not null) { history["YearIteration"] = body.Integer("YearIteration", 0, 100_000); return ApiResponse.Text(); }
        var dialogue = Get(inv, body.RequiredString("DialogueName")); var changes = new JObject(); var resultObject = new JObject { ["InventoryChanges"] = changes };
        var nextDay = inv.Value<bool>("noKimCooldowns") ? now : (now / WorldData.Day + 1) * WorldData.Day;
        if (body["Data"] is JObject completed && dialogue.Array("Completed").Any(x => x.Value<string>("Id") == completed.Value<string>("Id"))) return ApiResponse.Json(new JObject { ["InventoryChanges"] = changes, ["AvailableDate"] = dialogue["AvailableDate"]!.DeepClone() });
        if (body["Gift"] is JObject && !inv.Value<bool>("noKimCooldowns") && Wire.DateMs(dialogue["AvailableGiftDate"]) > now) throw new ApiException(409, "Already sent today's gift");
        dialogue["Rank"] = body.Integer("Rank", 0, 100); dialogue["Chemistry"] = checked(dialogue.Value<long>("Chemistry") + body.Integer("Chemistry", -999_999_999));
        dialogue["QueuedDialogues"] = body["QueuedDialogues"]?.DeepClone() ?? new JArray();
        foreach (var flag in body["Booleans"] as JArray ?? [])
        {
            if (dialogue.Array("Booleans").Any(x => x.ToString() == flag.ToString())) continue;
            dialogue.Array("Booleans").Add(flag.DeepClone());
            if (flag.ToString() == "LizzieShawzin") Inventory.Combine(changes, new ItemAcquisition(catalog, clock, account).Add(inv, "/Lotus/Types/Items/EmailItems/LizzieShawzinSkinEmailItem"));
        }
        foreach (var flag in body["ResetBooleans"] as JArray ?? []) foreach (var existing in dialogue.Array("Booleans").Where(x => x.ToString() == flag.ToString()).ToArray()) existing.Remove();
        foreach (var counter in (body["Counters"] as JArray ?? []).OfType<JObject>()) { counter.RequiredString("Name"); counter.Integer("Count", -999_999_999); Inventory.Upsert(dialogue.Array("Counters"), "Name", counter); }
        foreach (var other in (body["OtherDialogueInfos"] as JArray ?? []).OfType<JObject>())
        {
            var target = Get(inv, other.RequiredString("Dialogue")); if (other.Value<string>("Tag") is { Length: > 0 } tag) target.Array("QueuedDialogues").UniqueAdd(tag); target["Chemistry"] = checked(target.Value<long>("Chemistry") + other.Integer("Value", -999_999_999));
        }
        if (body["Data"] is JObject data) { data.RequiredString("Id"); dialogue.Array("Completed").Add(data.DeepClone()); dialogue["AvailableDate"] = Wire.Date(nextDay); resultObject["AvailableDate"] = Wire.Date(nextDay); }
        else if (body["Gift"] is JObject gift)
        {
            Inventory.Combine(changes, Inventory.ChangeCurrency(inv, "RegularCredits", -gift.Integer("Cost"))); var item = gift.RequiredString("Item"); var entry = dialogue.Array("Gifts").OfType<JObject>().FirstOrDefault(x => x.Value<string>("Item") == item);
            if (entry is null) dialogue.Array("Gifts").Add(new JObject { ["Item"] = item, ["GiftedQuantity"] = 1 }); else entry["GiftedQuantity"] = entry.Value<int>("GiftedQuantity") + 1;
            dialogue["AvailableGiftDate"] = Wire.Date(nextDay); resultObject["AvailableGiftDate"] = Wire.Date(nextDay);
        }
        else return ApiResponse.Text();
        return ApiResponse.Json(resultObject);
    }
}