using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public static partial class CustomizationApi { public static ApiResponse SaveLoadout(JObject account, JObject body) { var inv = account.Obj("Inventory"); string? createdId = null; foreach (var property in body.Properties()) { if (property.Name is "UpgradeVer" or "ValidNewLoadoutId" or "CurrentLoadout") continue; if (property.Value is JObject empty && !empty.HasValues) continue; if (property.Name is "OperatorLoadOuts" or "AdultOperatorLoadOuts" or "KahlLoadOuts") { if (property.Value is not JObject entries) throw new ApiException(400, "Invalid operator loadouts"); foreach (var entry in entries.Properties()) { if (entry.Value is not JObject value) throw new ApiException(400, "Invalid operator customization"); var id = Wire.Id(value["ItemId"]); if (id.Length == 0) id = entry.Name; var existing = inv.Array(property.Name).OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id); var saved = (JObject)value.DeepClone(); saved["ItemId"] = Wire.Oid(id); if (existing is null) inv.Array(property.Name).Add(saved); else existing.Merge(saved, new() { MergeArrayHandling = MergeArrayHandling.Replace }); } } else if (property.Name == "ActiveCrewShip") { Inventory.Equipment(inv, "CrewShips", Wire.Id(property.Value)); inv["ActiveCrewShip"] = property.Value.DeepClone(); } else if (property.Name is "LoadOut" or "Presets") LegacyLoadout(account, property.Value); else if (property.Name == "LoadOuts") { if (property.Value is not JObject loadouts) throw new ApiException(400, "Invalid LoadOuts"); foreach (var category in loadouts.Properties()) { if (category.Value is JObject legacy && legacy["Presets"] is JArray) { LegacyLoadout(account, category.Value); continue; } if (!Inventory.LoadoutKeys.Contains(category.Name) || category.Value is not JObject configs) throw new ApiException(400, "Invalid loadout category"); var existing = account.Obj("Loadouts").Array(category.Name); foreach (var entry in configs.Properties()) { if (entry.Value is not JObject input) throw new ApiException(400, "Invalid loadout configuration"); var config = (JObject)input.DeepClone(); var id = Wire.Id(config["ItemId"]); if (id.Length == 0) id = entry.Name; var target = existing.OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id); if (config.Value("Remove") == true) { target?.Remove(); continue; } if (id == "ffffffffffffffffffffffff") id = createdId ??= Wire.NewId(); config["ItemId"] = Wire.Oid(id); if (target is null) existing.Add(config); else target.Replace(config); } } } else if (Inventory.EquipmentKeys.Contains(property.Name) || property.Name == "WeaponSkins") { if (property.Value is not JObject equipment) throw new ApiException(400, "Invalid equipment configs"); foreach (var entry in equipment.Properties()) { var item = property.Name == "WeaponSkins" ? inv.Array("WeaponSkins").OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == entry.Name) ?? throw new ApiException(404, "Skin not owned") : Inventory.Equipment(inv, property.Name, entry.Name); if (entry.Value is not JObject config) throw new ApiException(400, "Invalid equipment config"); var previousAbilities = Abilities(item); var previous = (JArray?)item["Configs"]?.DeepClone() ?? []; foreach (var c in config.Properties()) { if (int.TryParse(c.Name, out var index) && index is >= 0 and <= 5) { if (property.Name == "WeaponSkins" || c.Value is not JObject incoming) throw new ApiException(400, "Invalid equipment customization"); var configs = item.Array("Configs"); while (configs.Count <= index) configs.Add(new JObject()); configs[index] = ImportConfig(inv, incoming, GameVersion.For(account), previous.ElementAtOrDefault(index), previousAbilities); } else if (c.Name is "Favorite" or "IsNew") item[c.Name] = c.Value.DeepClone(); else if (property.Name == "CrewShips" && c.Name is "ItemName" or "RailjackImage" or "Customization" or "Weapon" or "CrewMembers") item[c.Name] = c.Value.DeepClone(); else throw new ApiException(400, $"Unknown loadout property: {c.Name}"); } if (!previousAbilities.SequenceEqual(Abilities(item))) throw new ApiException(400, "Use Helminth to add or remove an ability override"); } } else if (property.Name is "CurrentLoadOutIds" or "EquippedGear" or "EquippedEmotes" or "UseAdultOperatorLoadout" or "LotusCustomization" or "FocusLoadouts" or "Sketches") inv[property.Name] = property.Value.DeepClone(); else throw new ApiException(400, $"Unknown loadout section: {property.Name}"); } return ApiResponse.Text(createdId ?? ""); } public static ApiResponse Ship(string controller, JObject account, ApiRequest request) { var rooms = account.Obj("PersonalRooms"); var body = request.ObjectBody(); if (controller == "getShipController") return ApiResponse.Json(rooms); if (controller == "setActiveShipController") { var id = request.Q("shipId"); if (!account.Obj("Inventory").Array("Ships").Any(x => Wire.Id(x["ItemId"]) == id)) throw new ApiException(404, "Ship is not owned"); rooms["activeShipId"] = id; return ApiResponse.Text(); } if (controller == "setShipFavouriteLoadoutController") { var location = body.Value("BootLocation"); if (location == "LISET") rooms.Obj("Ship")["FavouriteLoadoutId"] = body["FavouriteLoadoutId"]; else if (location is "APARTMENT" or "SHOP") Inventory.Upsert(rooms.Obj(location == "SHOP" ? "TailorShop" : "Apartment").Array("FavouriteLoadouts"), "Tag", new JObject { ["Tag"] = body["TagName"], ["LoadoutId"] = body["FavouriteLoadoutId"] }); else throw new ApiException(400, "Invalid BootLocation"); return ApiResponse.Json(body); } if (body["Customization"] is not JObject customization) throw new ApiException(400, "Missing Customization"); if (body.Value("IsExterior") == true) { var ship = account.Obj("Inventory").Array("Ships").OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == body.Value("ShipId")) ?? throw new ApiException(404, "Ship not found"); ship["ShipExterior"] = customization.DeepClone(); ship["AirSupportPower"] = body["AirSupportPower"]; } else if (body.Value("IsShop") == true) foreach (var key in new[] { "Colors", "LevelDecosVisible", "CustomJson" }) rooms.Obj("TailorShop")[key] = customization[key]; else rooms.Obj("Ship")["ShipInterior"] = customization.DeepClone(); return ApiResponse.Text(); } }