using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class ModApi
{
public static readonly HashSet<string> Controllers = ["upgradesController", "artifactsController", "artifactTransmutationController"];
public ApiResponse Handle(string controller, JObject account, ApiRequest request)
{
var inv = account.Obj("Inventory"); var body = request.ObjectBody();
InfestationKing.ResolveRequest(inv, body);
FetchOverdrive.ResolveRequest(inv, body);
return controller switch { "upgradesController" => EquipmentUpgrades(inv, body), "artifactsController" => Fuse(inv, body, GameVersion.For(account)), "artifactTransmutationController" => Transmute(inv, body, GameVersion.For(account)), _ => throw new ApiException(404, "Unknown mod operation") };
}
private static ApiResponse EquipmentUpgrades(JObject inv, JObject body)
{
var legacy = body["Operations"] is not JArray; var category = body.Value<string>(legacy ? "Category" : "ItemCategory") ?? throw new ApiException(400, "Missing equipment category");
var gear = Inventory.Equipment(inv, category, Wire.Id(legacy ? body["Weapon"]?["ItemId"] : body["ItemId"]));
var inventoryChanges = new JObject();
var operations = body["Operations"] as JArray ?? new JArray();
if (legacy)
{
if (body.Value<bool>("IsSwappingOperation")) operations.Add(new JObject { ["OperationType"] = "UOT_SWAP_POLARITY", ["UpgradeRequirement"] = "", ["PolarityRemap"] = body["PolarityRemap"]?.DeepClone() ?? new JArray() });
else foreach (var key in new[] { "PolarizeReq", "UtilityReq", "UpgradeReq" }) if (body.Value<string>(key) is { Length: > 0 } requirement) operations.Add(new JObject { ["OperationType"] = "LEGACY", ["UpgradeRequirement"] = requirement, ["PolarizeSlot"] = body["PolarizeSlot"]?.DeepClone(), ["PolarizeValue"] = body["PolarizeValue"]?.DeepClone() });
}
foreach (var operation in operations.OfType<JObject>())
{
var op = operation.RequiredString("OperationType"); var requirement = operation.Value<string>("UpgradeRequirement") ?? "";
if (op == "UOT_ABILITY_OVERRIDE") { if (category != "Suits") throw new ApiException(400, "Abilities can only be installed on warframes"); var change = HelminthApi.Override(inv, gear, operation); Inventory.Combine(inventoryChanges, new JObject { ["Recipes"] = change["Recipes"]!.DeepClone() }); inventoryChanges["InfestedFoundry"] = change["InfestedFoundry"]!.DeepClone(); continue; }
if (op == "UOT_SWAP_POLARITY")
{
var remap = operation["PolarityRemap"] as JArray ?? throw new ApiException(400, "Missing polarity remap");
for (var i = 0; i < remap.Count; i++) if (legacy || remap[i].Value<int>("Slot") != i) SetPolarity(gear, i, remap[i].Value<string>("Value") ?? "AP_UNIVERSAL");
continue;
}
if (requirement is "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" or "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker")
{
Inventory.ChangeCurrency(inv, "PremiumCredits", -10); var key = requirement.EndsWith("/ModSlotUnlocker", StringComparison.Ordinal) ? "ModSlotPurchases" : "CustomizationSlotPurchases";
gear[key] = checked(gear.Value<int>(key) + 1); continue;
}
var feature = requirement switch
{
"/Lotus/Types/Items/MiscItems/OrokinReactor" or "/Lotus/Types/Items/MiscItems/OrokinCatalyst" => 1,
"/Lotus/Types/Items/MiscItems/UtilityUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponUtilityUnlocker" => 2,
"/Lotus/Types/Items/MiscItems/HeavyWeaponCatalyst" => 4,
"/Lotus/Types/Items/MiscItems/WeaponPrimaryArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponSecondaryArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponMeleeArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponAmpArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponArchGunArcaneUnlocker" => op == "UOT_ARCANE_UNLOCK_1" ? 64 : 32,
"/Lotus/Types/Items/MiscItems/ValenceAdapter" => 1024, _ => 0
};
var forma = requirement is "/Lotus/Types/Items/MiscItems/Forma" or "/Lotus/Types/Items/MiscItems/FormaUmbra" or "/Lotus/Types/Items/MiscItems/FormaAura" or "/Lotus/Types/Items/MiscItems/FormaStance";
if (feature == 0 && !forma) throw new ApiException(400, "Unknown equipment upgrade requirement");
if (feature != 0 && (gear.Value<int>("Features") & feature) != 0) throw new ApiException(409, "Equipment feature already unlocked");
Inventory.AddCount(inv, "MiscItems", requirement, -1);
var affected = new List<JObject> { gear };
if (gear["AltWeaponModeId"] is JToken alternate && (forma || feature is 1 or 2)) affected.Add(Inventory.Equipment(inv, category == "Melee" ? "LongGuns" : "Melee", Wire.Id(alternate)));
foreach (var item in affected)
{
if (forma) { item["XP"] = 0; item["Polarized"] = checked(item.Value<int>("Polarized") + 1); SetPolarity(item, (int)operation.Integer("PolarizeSlot", 0, 63), operation.RequiredString("PolarizeValue")); }
else item["Features"] = item.Value<int>("Features") | feature;
}
}
if (legacy)
{
var configs = gear.Array("Configs"); if (configs.Count == 0) configs.Add(new JObject()); var mods = ((JObject)configs[0]).Array("Upgrades"); while (mods.Count < 11) mods.Add("");
foreach (var attach in (body["UpgradesToAttach"] as JArray ?? []).OfType<JObject>())
{
var id = Wire.Id(attach["ItemId"]); var type = attach.RequiredString("ItemType"); var slot = (int)attach.Integer("Slot", 1, 11) - 1;
var owned = inv.Array("Upgrades").FirstOrDefault(x => Wire.Id(x["ItemId"]) == id);
if (owned is null) { Inventory.AddCount(inv, "RawUpgrades", type, -1); id = Wire.NewId(); inv.Array("Upgrades").Add(new JObject { ["ItemId"] = Wire.Oid(id), ["ItemType"] = type, ["UpgradeFingerprint"] = "{\"lvl\":0}" }); }
else if (owned.Value<string>("ItemType") != type) throw new ApiException(400, "Attached mod type does not match owned mod");
for (var i = 0; i < mods.Count; i++) if (mods[i].ToString() == id) mods[i] = ""; mods[slot] = id;
}
foreach (var detached in body["UpgradesToDetach"] as JArray ?? []) for (var i = 0; i < mods.Count; i++) if (mods[i].ToString() == Wire.Id(detached["ItemId"])) mods[i] = "";
if (body["Weapon"]?["UpgradeNodes"] is JToken nodes) gear["UpgradeNodes"] = nodes.DeepClone();
if (body["Cost"] is not null) Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost"));
}
return ApiResponse.Json(new JObject { ["InventoryChanges"] = inventoryChanges });
}
private static void SetPolarity(JObject item, int slot, string value)
{
if (slot is < 0 or > 63 || !value.StartsWith("AP_", StringComparison.Ordinal)) throw new ApiException(400, "Invalid polarity slot");
var entry = item.Array("Polarity").OfType<JObject>().FirstOrDefault(x => x.Value<int>("Slot") == slot);
if (entry is null) item.Array("Polarity").Add(new JObject { ["Slot"] = slot, ["Value"] = value }); else entry["Value"] = value;
}
public static JObject Metadata(string type) => GameData.Find("Upgrades", type) ?? GameData.Find("Arcanes", type) ?? WorldData.Get("supplementalUpgrades")[type] as JObject ?? LegacyMods.Find(type) ?? throw new ApiException(400, "Unknown mod");
public static void Consume(JObject inv, JObject mod, int? countOverride = null)
{
var type = mod.RequiredString("ItemType"); var id = Wire.Id(mod["ItemId"]); var count = countOverride ?? mod.Value<int?>("ItemCount") ?? 1;
if (count is < 1 or > 100_000) throw new ApiException(400, "Invalid mod count");
if (id is "" or "000000000000000000000000") Inventory.AddCount(inv, "RawUpgrades", type, -count);
else
{
var owned = inv.Array("Upgrades").FirstOrDefault(x => Wire.Id(x["ItemId"]) == id) ?? throw new ApiException(404, "Mod is not owned");
if (count != 1 || owned.Value<string>("ItemType") != type) throw new ApiException(400, "Owned mod does not match request"); owned.Remove();
}
}
private static ApiResponse Fuse(JObject inv, JObject body, long build)
{
var upgrade = body["Upgrade"] as JObject ?? throw new ApiException(400, "Missing upgrade"); var type = upgrade.RequiredString("ItemType"); var meta = Metadata(type);
var id = Wire.Id(upgrade["ItemId"]); var owned = inv.Array("Upgrades").OfType<JObject>().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id);
if (owned is not null && owned.Value<string>("ItemType") != type) throw new ApiException(400, "Mod type does not match owned mod");
var fingerprint = JObject.Parse(owned?.Value<string>("UpgradeFingerprint") ?? "{\"lvl\":0}"); var increment = (int)body.Integer("LevelDiff", 0, 20); var rank = fingerprint.Value<int>("lvl") + increment;
if (rank > meta.Value<int>("fusionLimit") || increment == 0 && body["VariantFusion"] is null) throw new ApiException(400, "Invalid fusion level");
Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost"));
if (build >= GameVersion.Of("18.18.0")) Inventory.ChangeCurrency(inv, build < GameVersion.Of("29.10.0") && type.StartsWith("/Lotus/Upgrades/Mods/Railjack/", StringComparison.Ordinal) ? "CrewShipFusionPoints" : "FusionPoints", -body.Integer("FusionPointCost"));
else foreach (var mod in (body["Consumed"] as JArray ?? []).OfType<JObject>()) { if (Wire.Id(mod["ItemId"]) == id && id.Length != 0) throw new ApiException(400, "Cannot consume the mod being fused"); Consume(inv, mod); }
if (body.Value<bool>("LegendaryFusion")) Inventory.AddCount(inv, "RawUpgrades", "/Lotus/Upgrades/Mods/Fusers/LegendaryModFuser", -1);
if (body["VariantFusion"] is not null) { fingerprint["variant"] = body.Integer("VariantFusion", 0, 1000); Inventory.AddCount(inv, "MiscItems", "/Lotus/Types/Items/MiscItems/FoilModUnlocker", -1); }
fingerprint["lvl"] = rank;
if (owned is null) { Inventory.AddCount(inv, "RawUpgrades", type, -1); id = Wire.NewId(); owned = new JObject { ["ItemId"] = Wire.Oid(id), ["ItemType"] = type }; inv.Array("Upgrades").Add(owned); }
owned["UpgradeFingerprint"] = fingerprint.ToString(Formatting.None); return ApiResponse.Text(id);
}
private static ApiResponse Transmute(JObject inv, JObject body, long build)
{
if (body.Value<bool>("RivenTransmute")) return RivenApi.Transmute(inv, body, build);
var consumed = body["Consumed"] as JArray ?? throw new ApiException(400, "Missing transmutation ingredients");
if (consumed.Sum(x => x.Value<int?>("ItemCount") ?? 1) != 4) throw new ApiException(400, "Transmutation requires four mods");
var counts = new Dictionary<string, int> { ["COMMON"] = 0, ["UNCOMMON"] = 0, ["RARE"] = 0, ["LEGENDARY"] = 0 }; string? polarity = null;
foreach (var mod in consumed.OfType<JObject>())
{
var type = mod.RequiredString("ItemType"); var meta = Metadata(type); var count = mod.Value<int?>("ItemCount") ?? 1; Consume(inv, mod); counts[meta.Value<string>("rarity")!] += count;
if (type.StartsWith("/Lotus/Upgrades/Mods/TransmuteCores/", StringComparison.Ordinal)) polarity = type.EndsWith("AttackTransmuteCore", StringComparison.Ordinal) ? "AP_ATTACK" : type.EndsWith("DefenseTransmuteCore", StringComparison.Ordinal) ? "AP_DEFENSE" : "AP_TACTIC";
}
Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost")); if (body["FusionPointCost"] is not null) Inventory.ChangeCurrency(inv, "FusionPoints", -body.Integer("FusionPointCost"));
var special = WorldData.Get("specialModSets").OfType<JArray>().FirstOrDefault(x => x.Any(t => t.ToString() == consumed[0]!.Value<string>("ItemType")));
if (special is not null) { var type = special[Random.Shared.Next(special.Count)].ToString(); Inventory.AddCount(inv, "RawUpgrades", type, 1); return ApiResponse.Json(new JObject { ["NewMods"] = new JArray(new JObject { ["ItemType"] = type, ["ItemCount"] = 1 }) }); }
var weights = new Dictionary<string, double> { ["COMMON"] = counts["COMMON"] * 95 + counts["UNCOMMON"] * 15 + counts["RARE"] * 4, ["UNCOMMON"] = counts["COMMON"] * 4 + counts["UNCOMMON"] * 80 + counts["RARE"] * 10, ["RARE"] = counts["COMMON"] + counts["UNCOMMON"] * 5 + counts["RARE"] * 50, ["LEGENDARY"] = 0 };
var pool = (GameData.Find("BoosterPacks", "/Lotus/Types/BoosterPacks/ModFuserResult")?["components"] as JArray ?? []).Where(x => { var m = Metadata(x.Value<string>("Item")!); return (polarity is null || m.Value<string>("polarity") == polarity) && m.Value<long>("introducedAt") > 0 && GameVersion.Parse(DateTimeOffset.FromUnixTimeSeconds(m.Value<long>("introducedAt")).ToString("yyyy.MM.dd.HH.mm")) <= build; }).Select(x => (JObject)x.DeepClone()).ToArray();
foreach (var group in pool.GroupBy(x => x.Value<string>("Rarity")!)) foreach (var item in group) item["probability"] = weights.GetValueOrDefault(group.Key) / group.Count();
var selected = SeededRng.RewardAt(new JArray(pool), Random.Shared.NextDouble()) ?? throw new ApiException(400, "No supported transmutation result"); var newType = selected.RequiredString("Item");
Inventory.AddCount(inv, "RawUpgrades", newType, 1); return ApiResponse.Json(new JObject { ["NewMods"] = new JArray(new JObject { ["ItemType"] = newType, ["ItemCount"] = 1 }) });
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class ModApi
{
public static readonly HashSet<string> Controllers = ["upgradesController", "artifactsController", "artifactTransmutationController"];
public ApiResponse Handle(string controller, JObject account, ApiRequest request)
{
var inv = account.Obj("Inventory"); var body = request.ObjectBody();
InfestationKing.ResolveRequest(inv, body);
FetchOverdrive.ResolveRequest(inv, body);
return controller switch { "upgradesController" => EquipmentUpgrades(inv, body), "artifactsController" => Fuse(inv, body, GameVersion.For(account)), "artifactTransmutationController" => Transmute(inv, body, GameVersion.For(account)), _ => throw new ApiException(404, "Unknown mod operation") };
}
private static ApiResponse EquipmentUpgrades(JObject inv, JObject body)
{
var legacy = body["Operations"] is not JArray; var category = body.Value<string>(legacy ? "Category" : "ItemCategory") ?? throw new ApiException(400, "Missing equipment category");
var gear = Inventory.Equipment(inv, category, Wire.Id(legacy ? body["Weapon"]?["ItemId"] : body["ItemId"]));
var inventoryChanges = new JObject();
var operations = body["Operations"] as JArray ?? new JArray();
if (legacy)
{
if (body.Value<bool>("IsSwappingOperation")) operations.Add(new JObject { ["OperationType"] = "UOT_SWAP_POLARITY", ["UpgradeRequirement"] = "", ["PolarityRemap"] = body["PolarityRemap"]?.DeepClone() ?? new JArray() });
else foreach (var key in new[] { "PolarizeReq", "UtilityReq", "UpgradeReq" }) if (body.Value<string>(key) is { Length: > 0 } requirement) operations.Add(new JObject { ["OperationType"] = "LEGACY", ["UpgradeRequirement"] = requirement, ["PolarizeSlot"] = body["PolarizeSlot"]?.DeepClone(), ["PolarizeValue"] = body["PolarizeValue"]?.DeepClone() });
}
foreach (var operation in operations.OfType<JObject>())
{
var op = operation.RequiredString("OperationType"); var requirement = operation.Value<string>("UpgradeRequirement") ?? "";
if (op == "UOT_ABILITY_OVERRIDE") { if (category != "Suits") throw new ApiException(400, "Abilities can only be installed on warframes"); var change = HelminthApi.Override(inv, gear, operation); Inventory.Combine(inventoryChanges, new JObject { ["Recipes"] = change["Recipes"]!.DeepClone() }); inventoryChanges["InfestedFoundry"] = change["InfestedFoundry"]!.DeepClone(); continue; }
if (op == "UOT_SWAP_POLARITY")
{
var remap = operation["PolarityRemap"] as JArray ?? throw new ApiException(400, "Missing polarity remap");
for (var i = 0; i < remap.Count; i++) if (legacy || remap[i].Value<int>("Slot") != i) SetPolarity(gear, i, remap[i].Value<string>("Value") ?? "AP_UNIVERSAL");
continue;
}
if (requirement is "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" or "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker")
{
Inventory.ChangeCurrency(inv, "PremiumCredits", -10); var key = requirement.EndsWith("/ModSlotUnlocker", StringComparison.Ordinal) ? "ModSlotPurchases" : "CustomizationSlotPurchases";
gear[key] = checked(gear.Value<int>(key) + 1); continue;
}
var feature = requirement switch
{
"/Lotus/Types/Items/MiscItems/OrokinReactor" or "/Lotus/Types/Items/MiscItems/OrokinCatalyst" => 1,
"/Lotus/Types/Items/MiscItems/UtilityUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponUtilityUnlocker" => 2,
"/Lotus/Types/Items/MiscItems/HeavyWeaponCatalyst" => 4,
"/Lotus/Types/Items/MiscItems/WeaponPrimaryArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponSecondaryArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponMeleeArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponAmpArcaneUnlocker" or "/Lotus/Types/Items/MiscItems/WeaponArchGunArcaneUnlocker" => op == "UOT_ARCANE_UNLOCK_1" ? 64 : 32,
"/Lotus/Types/Items/MiscItems/ValenceAdapter" => 1024, _ => 0
};
var forma = requirement is "/Lotus/Types/Items/MiscItems/Forma" or "/Lotus/Types/Items/MiscItems/FormaUmbra" or "/Lotus/Types/Items/MiscItems/FormaAura" or "/Lotus/Types/Items/MiscItems/FormaStance";
if (feature == 0 && !forma) throw new ApiException(400, "Unknown equipment upgrade requirement");
if (feature != 0 && (gear.Value<int>("Features") & feature) != 0) throw new ApiException(409, "Equipment feature already unlocked");
Inventory.AddCount(inv, "MiscItems", requirement, -1);
var affected = new List<JObject> { gear };
if (gear["AltWeaponModeId"] is JToken alternate && (forma || feature is 1 or 2)) affected.Add(Inventory.Equipment(inv, category == "Melee" ? "LongGuns" : "Melee", Wire.Id(alternate)));
foreach (var item in affected)
{
if (forma) { item["XP"] = 0; item["Polarized"] = checked(item.Value<int>("Polarized") + 1); SetPolarity(item, (int)operation.Integer("PolarizeSlot", 0, 63), operation.RequiredString("PolarizeValue")); }
else item["Features"] = item.Value<int>("Features") | feature;
}
}
if (legacy)
{
var configs = gear.Array("Configs"); if (configs.Count == 0) configs.Add(new JObject()); var mods = ((JObject)configs[0]).Array("Upgrades"); while (mods.Count < 11) mods.Add("");
foreach (var attach in (body["UpgradesToAttach"] as JArray ?? []).OfType<JObject>())
{
var id = Wire.Id(attach["ItemId"]); var type = attach.RequiredString("ItemType"); var slot = (int)attach.Integer("Slot", 1, 11) - 1;
var owned = inv.Array("Upgrades").FirstOrDefault(x => Wire.Id(x["ItemId"]) == id);
if (owned is null) { Inventory.AddCount(inv, "RawUpgrades", type, -1); id = Wire.NewId(); inv.Array("Upgrades").Add(new JObject { ["ItemId"] = Wire.Oid(id), ["ItemType"] = type, ["UpgradeFingerprint"] = "{\"lvl\":0}" }); }
else if (owned.Value<string>("ItemType") != type) throw new ApiException(400, "Attached mod type does not match owned mod");
for (var i = 0; i < mods.Count; i++) if (mods[i].ToString() == id) mods[i] = ""; mods[slot] = id;
}
foreach (var detached in body["UpgradesToDetach"] as JArray ?? []) for (var i = 0; i < mods.Count; i++) if (mods[i].ToString() == Wire.Id(detached["ItemId"])) mods[i] = "";
if (body["Weapon"]?["UpgradeNodes"] is JToken nodes) gear["UpgradeNodes"] = nodes.DeepClone();
if (body["Cost"] is not null) Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost"));
}
return ApiResponse.Json(new JObject { ["InventoryChanges"] = inventoryChanges });
}
private static void SetPolarity(JObject item, int slot, string value)
{
if (slot is < 0 or > 63 || !value.StartsWith("AP_", StringComparison.Ordinal)) throw new ApiException(400, "Invalid polarity slot");
var entry = item.Array("Polarity").OfType<JObject>().FirstOrDefault(x => x.Value<int>("Slot") == slot);
if (entry is null) item.Array("Polarity").Add(new JObject { ["Slot"] = slot, ["Value"] = value }); else entry["Value"] = value;
}
public static JObject Metadata(string type) => GameData.Find("Upgrades", type) ?? GameData.Find("Arcanes", type) ?? WorldData.Get("supplementalUpgrades")[type] as JObject ?? LegacyMods.Find(type) ?? throw new ApiException(400, "Unknown mod");
public static void Consume(JObject inv, JObject mod, int? countOverride = null)
{
var type = mod.RequiredString("ItemType"); var id = Wire.Id(mod["ItemId"]); var count = countOverride ?? mod.Value<int?>("ItemCount") ?? 1;
if (count is < 1 or > 100_000) throw new ApiException(400, "Invalid mod count");
if (id is "" or "000000000000000000000000") Inventory.AddCount(inv, "RawUpgrades", type, -count);
else
{
var owned = inv.Array("Upgrades").FirstOrDefault(x => Wire.Id(x["ItemId"]) == id) ?? throw new ApiException(404, "Mod is not owned");
if (count != 1 || owned.Value<string>("ItemType") != type) throw new ApiException(400, "Owned mod does not match request"); owned.Remove();
}
}
private static ApiResponse Fuse(JObject inv, JObject body, long build)
{
var upgrade = body["Upgrade"] as JObject ?? throw new ApiException(400, "Missing upgrade"); var type = upgrade.RequiredString("ItemType"); var meta = Metadata(type);
var id = Wire.Id(upgrade["ItemId"]); var owned = inv.Array("Upgrades").OfType<JObject>().FirstOrDefault(x => Wire.Id(x["ItemId"]) == id);
if (owned is not null && owned.Value<string>("ItemType") != type) throw new ApiException(400, "Mod type does not match owned mod");
var fingerprint = JObject.Parse(owned?.Value<string>("UpgradeFingerprint") ?? "{\"lvl\":0}"); var increment = (int)body.Integer("LevelDiff", 0, 20); var rank = fingerprint.Value<int>("lvl") + increment;
if (rank > meta.Value<int>("fusionLimit") || increment == 0 && body["VariantFusion"] is null) throw new ApiException(400, "Invalid fusion level");
Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost"));
if (build >= GameVersion.Of("18.18.0")) Inventory.ChangeCurrency(inv, build < GameVersion.Of("29.10.0") && type.StartsWith("/Lotus/Upgrades/Mods/Railjack/", StringComparison.Ordinal) ? "CrewShipFusionPoints" : "FusionPoints", -body.Integer("FusionPointCost"));
else foreach (var mod in (body["Consumed"] as JArray ?? []).OfType<JObject>()) { if (Wire.Id(mod["ItemId"]) == id && id.Length != 0) throw new ApiException(400, "Cannot consume the mod being fused"); Consume(inv, mod); }
if (body.Value<bool>("LegendaryFusion")) Inventory.AddCount(inv, "RawUpgrades", "/Lotus/Upgrades/Mods/Fusers/LegendaryModFuser", -1);
if (body["VariantFusion"] is not null) { fingerprint["variant"] = body.Integer("VariantFusion", 0, 1000); Inventory.AddCount(inv, "MiscItems", "/Lotus/Types/Items/MiscItems/FoilModUnlocker", -1); }
fingerprint["lvl"] = rank;
if (owned is null) { Inventory.AddCount(inv, "RawUpgrades", type, -1); id = Wire.NewId(); owned = new JObject { ["ItemId"] = Wire.Oid(id), ["ItemType"] = type }; inv.Array("Upgrades").Add(owned); }
owned["UpgradeFingerprint"] = fingerprint.ToString(Formatting.None); return ApiResponse.Text(id);
}
private static ApiResponse Transmute(JObject inv, JObject body, long build)
{
if (body.Value<bool>("RivenTransmute")) return RivenApi.Transmute(inv, body, build);
var consumed = body["Consumed"] as JArray ?? throw new ApiException(400, "Missing transmutation ingredients");
if (consumed.Sum(x => x.Value<int?>("ItemCount") ?? 1) != 4) throw new ApiException(400, "Transmutation requires four mods");
var counts = new Dictionary<string, int> { ["COMMON"] = 0, ["UNCOMMON"] = 0, ["RARE"] = 0, ["LEGENDARY"] = 0 }; string? polarity = null;
foreach (var mod in consumed.OfType<JObject>())
{
var type = mod.RequiredString("ItemType"); var meta = Metadata(type); var count = mod.Value<int?>("ItemCount") ?? 1; Consume(inv, mod); counts[meta.Value<string>("rarity")!] += count;
if (type.StartsWith("/Lotus/Upgrades/Mods/TransmuteCores/", StringComparison.Ordinal)) polarity = type.EndsWith("AttackTransmuteCore", StringComparison.Ordinal) ? "AP_ATTACK" : type.EndsWith("DefenseTransmuteCore", StringComparison.Ordinal) ? "AP_DEFENSE" : "AP_TACTIC";
}
Inventory.ChangeCurrency(inv, "RegularCredits", -body.Integer("Cost")); if (body["FusionPointCost"] is not null) Inventory.ChangeCurrency(inv, "FusionPoints", -body.Integer("FusionPointCost"));
var special = WorldData.Get("specialModSets").OfType<JArray>().FirstOrDefault(x => x.Any(t => t.ToString() == consumed[0]!.Value<string>("ItemType")));
if (special is not null) { var type = special[Random.Shared.Next(special.Count)].ToString(); Inventory.AddCount(inv, "RawUpgrades", type, 1); return ApiResponse.Json(new JObject { ["NewMods"] = new JArray(new JObject { ["ItemType"] = type, ["ItemCount"] = 1 }) }); }
var weights = new Dictionary<string, double> { ["COMMON"] = counts["COMMON"] * 95 + counts["UNCOMMON"] * 15 + counts["RARE"] * 4, ["UNCOMMON"] = counts["COMMON"] * 4 + counts["UNCOMMON"] * 80 + counts["RARE"] * 10, ["RARE"] = counts["COMMON"] + counts["UNCOMMON"] * 5 + counts["RARE"] * 50, ["LEGENDARY"] = 0 };
var pool = (GameData.Find("BoosterPacks", "/Lotus/Types/BoosterPacks/ModFuserResult")?["components"] as JArray ?? []).Where(x => { var m = Metadata(x.Value<string>("Item")!); return (polarity is null || m.Value<string>("polarity") == polarity) && m.Value<long>("introducedAt") > 0 && GameVersion.Parse(DateTimeOffset.FromUnixTimeSeconds(m.Value<long>("introducedAt")).ToString("yyyy.MM.dd.HH.mm")) <= build; }).Select(x => (JObject)x.DeepClone()).ToArray();
foreach (var group in pool.GroupBy(x => x.Value<string>("Rarity")!)) foreach (var item in group) item["probability"] = weights.GetValueOrDefault(group.Key) / group.Count();
var selected = SeededRng.RewardAt(new JArray(pool), Random.Shared.NextDouble()) ?? throw new ApiException(400, "No supported transmutation result"); var newType = selected.RequiredString("Item");
Inventory.AddCount(inv, "RawUpgrades", newType, 1); return ApiResponse.Json(new JObject { ["NewMods"] = new JArray(new JObject { ["ItemType"] = newType, ["ItemCount"] = 1 }) });
}
}