using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public sealed partial class QuestApi { public bool Manage(JObject account, string operation, string? selected) { var inv = account.Obj("Inventory"); var before = inv.ToString(); var featuresBefore = account["PersonalRooms"]?["Ship"]?["Features"]?.DeepClone(); if (operation == "setInactive") { inv["ActiveQuest"] = ""; return inv.ToString() != before; } var keys = operation is "completeAll" or "resetAll" ? inv.Array("QuestKeys").OfType().ToArray() : [Find(inv, selected ?? throw new ApiException(400, "Missing quest key"))]; foreach (var quest in keys) { var type = quest.RequiredString("ItemType"); var meta = Meta(type); var progress = quest.Array("Progress"); var run = Math.Max(0, progress.FirstOrDefault()?.Value("c") ?? 0); if (operation == "deleteKey") { quest.Remove(); if (inv.Value("ActiveQuest") == type) inv["ActiveQuest"] = ""; continue; } if (operation is "resetKey" or "resetAll") { quest["Completed"] = false; quest["Progress"] = new JArray(); quest.Remove("CompletionDate"); if (operation == "resetAll") inv["ActiveQuest"] = ""; continue; } if (operation is "completeKey" or "completeAll") { if (quest.Value("Completed")) { ShipFeatureProgress.RestoreCompletedQuests(account); continue; } for (var i = 0; i < meta.Array("chainStages").Count; i++) { EnsureStage(quest, i, run); ManageStage(account, type, i, true); } var upload = (JObject)quest.DeepClone(); upload["Completed"] = true; Update(account, new JArray(upload)); if (type == "/Lotus/Types/Keys/ModQuest/ModQuestKeyChain") inv.Array("NodeIntrosCompleted").UniqueAdd("ModQuestTeshinAccess"); if (type == "/Lotus/Types/Keys/JadeShadows/JadeShadowQuestKeyChain" && !inv.Array("NodeIntrosCompleted").Any(x => x.ToString().StartsWith("BabyName_", StringComparison.Ordinal))) inv.Array("NodeIntrosCompleted").Add("BabyName_A"); continue; } var current = progress.Select(x => x.Value("c")).ToList().LastIndexOf(run); if (operation == "prevStage") { quest["Completed"] = false; quest.Remove("CompletionDate"); if (current >= 0) { if (run > 0) progress[current]["c"] = run - 1; else progress.RemoveAt(progress.Count - 1); } if (current > 0) ManageStage(account, type, current - 1, false); } else if (operation == "nextStage") { if (current + 1 >= meta.Array("chainStages").Count) { var upload = (JObject)quest.DeepClone(); upload["Completed"] = true; Update(account, new JArray(upload)); } else { EnsureStage(quest, current + 1, run); if (current >= 0) ManageStage(account, type, current, true); ManageStage(account, type, current + 1, false); } } else throw new ApiException(400, "Unknown quest management operation"); } return inv.ToString() != before || !JToken.DeepEquals(featuresBefore, account["PersonalRooms"]?["Ship"]?["Features"]); } private static void EnsureStage(JObject quest, int index, int run) { var progress = quest.Array("Progress"); while (progress.Count <= index) progress.Add(new JObject { ["c"] = run, ["i"] = false, ["m"] = false, ["b"] = new JArray() }); progress[index]["c"] = run; } private void ManageStage(JObject account, string type, int index, bool completed) { var inv = account.Obj("Inventory"); var body = new JObject { ["KeyChain"] = type, ["ChainStage"] = index }; var meta = Meta(type); var stage = meta["chainStages"]![index]!; var acquire = new ItemAcquisition(catalog, clock, account); if (type == "/Lotus/Types/Keys/PriestFrameQuest/PriestQuestKeyChain" && index == 0 && !inv.Array("OperatorAmps").Any(x => x.Value("ItemType") == FocusApi.TrainingAmpType)) acquire.Add(inv, FocusApi.TrainingAmpType, premium: true); TriggeredItems(account, body); foreach (var feature in (stage["itemsToGiveWhenTriggered"] as JArray ?? []).Select(x => GameData.ItemType(x.ToString())).Where(x => x.StartsWith("/Lotus/Types/Items/ShipFeatureItems/", StringComparison.Ordinal))) InventoryProgress.UnlockShip(account, feature); if (!completed) { if (stage["messageToSendWhenTriggered"] is JObject) TriggeredMessage(account, body); return; } var stageInfo = Stage(account, body, "message"); if (stage["messageToSendWhenTriggered"] is JObject message && !stageInfo.Stage.Value("m") && !account.Array("QuestClaims").Any(x => x.ToString() == stageInfo.Claim)) { if (stageInfo.Quest["Progress"]?[0]?.Value("c") <= 0) { foreach (var attachment in message["attachments"] as JArray ?? []) acquire.Add(inv, attachment.ToString(), premium: true); foreach (var attachment in (message["countedAttachments"] as JArray ?? []).OfType()) acquire.Add(inv, attachment.RequiredString("ItemType"), (int)attachment.Integer("ItemCount", 1, 100000), premium: true); } stageInfo.Stage["m"] = true; account.Array("QuestClaims").Add(stageInfo.Claim); } var claim = "managedStage:" + type + ":" + index + ":" + (Find(inv, type)["Progress"]?[0]?.Value("c") ?? 0); if (account.Array("QuestClaims").Any(x => x.ToString() == claim)) return; var mission = GameData.Find("Keys", stage.Value("key")); var rewards = new JArray(); long credits = 0; if (mission?["missionReward"] is JObject fixedReward) credits += MissionApi.FixedRewards(fixedReward, rewards); foreach (var reward in mission?["rewards"] as JArray ?? []) { if (reward.Value("rewardType") == "RT_CREDITS") credits += reward.Value("amount"); else rewards.Add(new JObject { ["StoreItem"] = reward["itemType"]!.DeepClone(), ["ItemCount"] = reward.Value("rewardType") == "RT_RESOURCE" ? reward.Value("amount") : 1 }); } Inventory.ChangeCurrency(inv, "RegularCredits", credits); foreach (var reward in rewards.OfType()) { var item = GameData.ItemType(reward.RequiredString("StoreItem")); acquire.Add(inv, item, (int)reward.Integer("ItemCount", 1, 100000), premium: true); if (item.StartsWith("/Lotus/Types/Items/ShipFeatureItems/", StringComparison.Ordinal)) InventoryProgress.UnlockShip(account, item); } account.Array("QuestClaims").Add(claim); } }