using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public sealed class Dialogues(ItemCatalog catalog, TimeProvider clock) { public static readonly HashSet 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().FirstOrDefault(x => x.Value("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("ClearPersist") }; if (body.Value("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().FirstOrDefault(x => x.Value("Pack") == pack); if (reset is null) { reset = new JObject { ["Pack"] = pack, ["Resets"] = 0 }; history.Array("ResetDates").Add(reset); } if (!inv.Value("noKimCooldowns") && Wire.DateMs(reset["Date"]) > now) throw new ApiException(409, "Dialogue reset is on cooldown"); reset["Date"] = Wire.Date(now + (inv.Value("noKimCooldowns") ? 0 : 28 * WorldData.Day)); reset["Resets"] = reset.Value("Resets") + 1; result["ResetDate"] = reset.DeepClone(); } history["Resets"] = history.Value("Resets") + 1; foreach (var name in names) { var old = history.Array("Dialogues").OfType().FirstOrDefault(x => x.Value("DialogueName") == name.ToString()); if (old is null) continue; if (body.Value("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("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("noKimCooldowns") ? now : (now / WorldData.Day + 1) * WorldData.Day; if (body["Data"] is JObject completed && dialogue.Array("Completed").Any(x => x.Value("Id") == completed.Value("Id"))) return ApiResponse.Json(new JObject { ["InventoryChanges"] = changes, ["AvailableDate"] = dialogue["AvailableDate"]!.DeepClone() }); if (body["Gift"] is JObject && !inv.Value("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("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()) { 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()) { var target = Get(inv, other.RequiredString("Dialogue")); if (other.Value("Tag") is { Length: > 0 } tag) target.Array("QueuedDialogues").UniqueAdd(tag); target["Chemistry"] = checked(target.Value("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().FirstOrDefault(x => x.Value("Item") == item); if (entry is null) dialogue.Array("Gifts").Add(new JObject { ["Item"] = item, ["GiftedQuantity"] = 1 }); else entry["GiftedQuantity"] = entry.Value("GiftedQuantity") + 1; dialogue["AvailableGiftDate"] = Wire.Date(nextDay); resultObject["AvailableGiftDate"] = Wire.Date(nextDay); } else return ApiResponse.Text(); return ApiResponse.Json(resultObject); } }