using Newtonsoft.Json; using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public sealed class EnhancementApi(ItemCatalog catalog, TimeProvider clock) { public static readonly HashSet Controllers = ["arcaneCommonController", "playerSkillsController"]; private static readonly int[] ArcaneCounts = [0, 3, 6, 10, 15, 21], LegacyCounts = [1, 3, 6, 10], DrifterCosts = [20, 25, 30, 45, 65, 90, 125, 160, 205, 255]; public ApiResponse Handle(string controller, JObject account, ApiRequest request) { var body = request.ObjectBody(); var inv = account.Obj("Inventory"); if (controller == "playerSkillsController") { var pool = body.RequiredString("Pool"); var skill = body.RequiredString("Skill"); if (GameData.Find("Intrinsics", skill) is not JObject meta || pool != (skill.StartsWith("LPS_DRIFT_", StringComparison.Ordinal) ? "LPP_DRIFTER" : "LPP_SPACE")) throw new ApiException(400, "Skill does not belong to this intrinsic pool"); var skills = inv.Obj("PlayerSkills"); var rank = skills.Value(skill); if (rank < 0 || rank >= Math.Min(10, (meta["ranks"] as JArray)?.Count ?? 0)) throw new ApiException(409, "Intrinsic rank limit reached"); var cost = (pool == "LPP_DRIFTER" ? DrifterCosts[rank] : 1 << rank) * 1000; if (skills.Value(pool) < cost) throw new ApiException(400, "Insufficient intrinsics"); skills[pool] = skills.Value(pool) - cost; skills[skill] = rank + 1; var changes = new JObject(); if (rank == 8 && skill is "LPS_COMMAND" or "LPS_DRIFT_RIDING") changes = new ItemAcquisition(catalog, clock, account).Add(inv, skill == "LPS_COMMAND" ? "/Lotus/Types/Restoratives/Consumable/CrewmateBall" : "/Lotus/Types/Restoratives/ErsatzSummon"); return ApiResponse.Json(new JObject { ["Pool"] = pool, ["PoolInc"] = -cost, ["Skill"] = skill, ["Rank"] = rank + 1, ["InventoryChanges"] = changes }); } if (body["newRank"] is null && body["skinId"] is not null) return Legacy(account, body); var requested = body["arcane"] as JObject ?? throw new ApiException(400, "Missing arcane"); var type = requested.RequiredString("ItemType"); var arcane = GameData.Find("Arcanes", type) ?? throw new ApiException(400, "Unknown arcane"); var id = Wire.Id(requested["ItemId"]); var owned = inv.Array("Upgrades").OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id); if (owned is not null && owned.Value("ItemType") != type) throw new ApiException(400, "Arcane type does not match owned item"); var level = owned is null ? 0 : Rank(owned); if (level is < 0 or > 5) throw new ApiException(400, "Invalid owned arcane rank"); var rankTarget = (int)body.Integer("newRank", -1, 5); if (rankTarget == -1) { if (owned is null) throw new ApiException(404, "Owned arcane not found"); Inventory.AddCount(inv, "RawUpgrades", type, ArcaneCounts[level]); owned.Remove(); return ApiResponse.Json(new JObject { ["upgradeId"] = id, ["numConsumed"] = ArcaneCounts[level] }); } var maxRank = (arcane["levelStats"] as JArray)?.Count - 1 ?? 5; if (rankTarget <= level || rankTarget > maxRank) throw new ApiException(400, "Arcane rank must increase within its rank limit"); var consumed = ArcaneCounts[rankTarget] - ArcaneCounts[level]; Inventory.AddCount(inv, "RawUpgrades", type, -consumed); if (owned is null) { id = Wire.NewId(); owned = new JObject { ["ItemId"] = Wire.Oid(id), ["ItemType"] = type }; inv.Array("Upgrades").Add(owned); } owned["UpgradeFingerprint"] = new JObject { ["lvl"] = rankTarget }.ToString(Formatting.None); return ApiResponse.Json(new JObject { ["newLevel"] = rankTarget, ["numConsumed"] = consumed, ["upgradeId"] = id }); } private ApiResponse Legacy(JObject account, JObject body) { var inv = account.Obj("Inventory"); var id = body.RequiredString("skinId"); if (body.Value("giveItem") && body.Value("modType") == 6) { var type = body.RequiredString("itemTypeName"); new ItemAcquisition(catalog, clock, account).Add(inv, type); id = Wire.Id(inv.Array("WeaponSkins").First(x => x.Value("ItemType") == type)["ItemId"]); } var item = inv.Array(body.Value("modType") == 5 ? "OperatorAmps" : "WeaponSkins").OfType().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id) ?? throw new ApiException(404, "Arcane attachment target is not owned"); var level = item["UpgradeFingerprint"] is null ? -1 : Rank(item); if (level is < -1 or > 3) throw new ApiException(400, "Invalid installed arcane rank"); if (body.Value("operationType") == "Distill") { if (level < 0) throw new ApiException(409, "No installed arcane"); var type = item.RequiredString("UpgradeType"); if (GameVersion.For(account) <= GameVersion.Of("19.4.1")) Inventory.AddCount(inv, "MiscItems", "/Lotus/Types/Recipes/CosmeticUnenhancerItem", -1); Inventory.AddCount(inv, "RawUpgrades", type, LegacyCounts[level]); item.Remove("UpgradeFingerprint"); item.Remove("UpgradeType"); return ApiResponse.Json(new JObject { ["arcaneType"] = type, ["numToRefund"] = LegacyCounts[level] }); } if (body.Value("operationType") != "Install") throw new ApiException(400, "Invalid arcane operation"); var selected = body.RequiredString("arcaneType"); if (GameData.Find("Arcanes", selected) is null) throw new ApiException(400, "Unknown arcane"); var result = new JObject(); if (level >= 0 && item.Value("UpgradeType") is string previous && previous != selected) { Inventory.AddCount(inv, "RawUpgrades", previous, LegacyCounts[level]); result["numToRefund"] = LegacyCounts[level]; result["typeToRefund"] = previous; level = -1; } if (level >= 3) throw new ApiException(409, "Arcane is already at maximum rank"); Inventory.AddCount(inv, "RawUpgrades", selected, -(LegacyCounts[level + 1] - (level < 0 ? 0 : LegacyCounts[level]))); item["UpgradeType"] = selected; item["UpgradeFingerprint"] = new JObject { ["lvl"] = level + 1 }.ToString(Formatting.None); result["newLevel"] = level + 1; if (body.Value("giveItem")) result["AddedSkinId"] = id; return ApiResponse.Json(result); } private static int Rank(JObject item) => JObject.Parse(item.Value("UpgradeFingerprint") ?? "{}").Value("lvl"); }