using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class FusionApi
{
public static readonly HashSet<string> Controllers = ["fusionTreasuresController", "archonFusionController", "evolveWeaponController"];
public ApiResponse Handle(string controller, JObject account, ApiRequest request)
{
var inv = account.Obj("Inventory"); var body = request.ObjectBody();
if (controller == "fusionTreasuresController") return Treasure(inv, body);
if (controller == "archonFusionController")
{
var consumed = (body["Consumed"] as JArray ?? throw new ApiException(400, "Missing shards")).OfType<JObject>().ToArray();
if (consumed.Length is < 1 or > 2) throw new ApiException(400, "Invalid shard fusion ingredients");
var types = consumed.Select(x => x.RequiredString("ItemType")).ToArray(); const string prefix = "/Lotus/Types/Gameplay/NarmerSorties/ArchonCrystal";
if (types.Any(x => !x.StartsWith(prefix, StringComparison.Ordinal) || GameData.Find("Resources", x) is null)) throw new ApiException(400, "Unknown archon shard");
string output;
if (body.Value<string>("FusionType") == "AFT_ASCENT")
{
if (consumed.Length != 1 || types[0].EndsWith("Mythic", StringComparison.Ordinal) || consumed[0].Integer("ItemCount", 1) != 3) throw new ApiException(400, "Ascent requires three ordinary shards of the same color");
output = types[0] + "Mythic";
}
else if (body.Value<string>("FusionType") == "AFT_COALESCENT")
{
if (consumed.Length != 2 || consumed.Any(x => x.Integer("ItemCount", 1) != 1) || types[0].EndsWith("Mythic", StringComparison.Ordinal) != types[1].EndsWith("Mythic", StringComparison.Ordinal)) throw new ApiException(400, "Coalescence requires two shards of matching quality");
var colors = types.Select(x => x[prefix.Length..].Replace("Mythic", "", StringComparison.Ordinal)).Order(StringComparer.Ordinal).ToArray();
var color = string.Join('+', colors) switch { "Amar+Nira" => "Orange", "Amar+Boreal" => "Violet", "Boreal+Nira" => "Green", _ => throw new ApiException(400, "These shard colors cannot be combined") };
output = prefix + color + (types[0].EndsWith("Mythic", StringComparison.Ordinal) ? "Mythic" : "");
}
else throw new ApiException(400, "Unknown archon fusion type");
foreach (var shard in consumed) Inventory.AddCount(inv, "MiscItems", shard.RequiredString("ItemType"), -shard.Integer("ItemCount", 1, 3));
Inventory.AddCount(inv, "MiscItems", output, 1); return ApiResponse.Json(new JObject { ["NewArchons"] = new JArray(new JObject { ["ItemType"] = output, ["ItemCount"] = 1 }) });
}
var recipe = GameData.Find("Recipes", body.RequiredString("Recipe")) ?? throw new ApiException(400, "Unknown Incarnon recipe");
var gear = Inventory.Equipment(inv, body.RequiredString("Category"), request.Q("ItemId")); var installed = (gear.Value<int>("Features") & 512) != 0;
if (body.Value<string>("Action") == "EWA_INSTALL")
{
if (installed) throw new ApiException(409, "Incarnon adapter already installed");
foreach (var ingredient in (recipe["ingredients"] as JArray ?? []).OfType<JObject>()) Inventory.AddCount(inv, "MiscItems", ingredient.RequiredString("ItemType"), -ingredient.Integer("ItemCount", 1));
gear["Features"] = gear.Value<int>("Features") | 512; gear["SkillTree"] = "0"; var type = body.RequiredString("EvoType");
if (!inv.Array("EvolutionProgress").Any(x => x.Value<string>("ItemType") == type)) inv.Array("EvolutionProgress").Add(new JObject { ["Progress"] = 0, ["Rank"] = 1, ["ItemType"] = type });
}
else if (body.Value<string>("Action") == "EWA_UNINSTALL")
{
if (!installed) throw new ApiException(409, "No Incarnon adapter installed");
Inventory.AddCount(inv, "MiscItems", recipe.RequiredString("resultType"), 1); gear["Features"] = gear.Value<int>("Features") & ~512;
}
else throw new ApiException(400, "Unknown Incarnon action");
return ApiResponse.Text();
}
private static ApiResponse Treasure(JObject inv, JObject body)
{
var legacy = body["ItemId"] is not null; string type; int oldSockets, newSockets; JObject? old;
if (legacy)
{
old = inv.Array("HybridFusionTreasures").OfType<JObject>().FirstOrDefault(x => Wire.Id(x["ItemId"]) == Wire.Id(body["ItemId"])) ?? throw new ApiException(404, "Sculpture is not owned");
type = old.RequiredString("ItemType"); oldSockets = old.Value<int>("Sockets"); newSockets = 0;
foreach (var key in (body["Sockets"] as JObject ?? throw new ApiException(400, "Missing sockets")).Properties()) { if (key.Name.Length != 1 || key.Name[0] is < 'a' or > 'p') throw new ApiException(400, "Invalid socket"); newSockets |= 1 << (key.Name[0] - 'a'); }
}
else
{
static (string Type, int Sockets) Parse(string name)
{
var split = name.LastIndexOf('_'); if (split < 0 || !int.TryParse(name[(split + 1)..], System.Globalization.NumberStyles.HexNumber, null, out var sockets) || sockets < 0) throw new ApiException(400, "Invalid sculpture name"); return (name[..split], sockets);
}
(type, oldSockets) = Parse(body.RequiredString("oldTreasureName")); var next = Parse(body.RequiredString("newTreasureName")); newSockets = next.Sockets;
if (next.Type != type) throw new ApiException(400, "Socketing cannot change sculpture type");
old = inv.Array("FusionTreasures").OfType<JObject>().FirstOrDefault(x => x.Value<string>("ItemType") == type && x.Value<int>("Sockets") == oldSockets && x.Value<int>("ItemCount") > 0) ?? throw new ApiException(400, "Sculpture is not owned");
}
var socketsMeta = GameData.Find("Resources", type)?["sockets"] as JArray ?? throw new ApiException(400, "Item has no sculpture sockets");
if (newSockets == oldSockets || (newSockets & oldSockets) != oldSockets || newSockets >= 1 << socketsMeta.Count) throw new ApiException(400, "Invalid sculpture socket change");
var changes = new JObject(); var filled = newSockets & ~oldSockets;
for (var i = 0; i < socketsMeta.Count; i++) if ((filled & 1 << i) != 0)
{
var star = socketsMeta[i].ToString(); Inventory.AddCount(inv, "MiscItems", star, -1); Inventory.Combine(changes, new JObject { ["MiscItems"] = new JArray(new JObject { ["ItemType"] = star, ["ItemCount"] = -1 }) });
}
if (legacy) { old["Sockets"] = newSockets; return ApiResponse.Text(); }
var count = old.Value<int>("ItemCount"); if (count == 1) old.Remove(); else old["ItemCount"] = count - 1;
var upgraded = inv.Array("FusionTreasures").OfType<JObject>().FirstOrDefault(x => x.Value<string>("ItemType") == type && x.Value<int>("Sockets") == newSockets);
if (upgraded is null) inv.Array("FusionTreasures").Add(new JObject { ["ItemType"] = type, ["Sockets"] = newSockets, ["ItemCount"] = 1 }); else upgraded["ItemCount"] = checked(upgraded.Value<int>("ItemCount") + 1);
changes["FusionTreasures"] = new JArray(new JObject { ["ItemType"] = type, ["Sockets"] = oldSockets, ["ItemCount"] = -1 }, new JObject { ["ItemType"] = type, ["Sockets"] = newSockets, ["ItemCount"] = 1 }); return ApiResponse.Json(changes);
}
}
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class FusionApi
{
public static readonly HashSet<string> Controllers = ["fusionTreasuresController", "archonFusionController", "evolveWeaponController"];
public ApiResponse Handle(string controller, JObject account, ApiRequest request)
{
var inv = account.Obj("Inventory"); var body = request.ObjectBody();
if (controller == "fusionTreasuresController") return Treasure(inv, body);
if (controller == "archonFusionController")
{
var consumed = (body["Consumed"] as JArray ?? throw new ApiException(400, "Missing shards")).OfType<JObject>().ToArray();
if (consumed.Length is < 1 or > 2) throw new ApiException(400, "Invalid shard fusion ingredients");
var types = consumed.Select(x => x.RequiredString("ItemType")).ToArray(); const string prefix = "/Lotus/Types/Gameplay/NarmerSorties/ArchonCrystal";
if (types.Any(x => !x.StartsWith(prefix, StringComparison.Ordinal) || GameData.Find("Resources", x) is null)) throw new ApiException(400, "Unknown archon shard");
string output;
if (body.Value<string>("FusionType") == "AFT_ASCENT")
{
if (consumed.Length != 1 || types[0].EndsWith("Mythic", StringComparison.Ordinal) || consumed[0].Integer("ItemCount", 1) != 3) throw new ApiException(400, "Ascent requires three ordinary shards of the same color");
output = types[0] + "Mythic";
}
else if (body.Value<string>("FusionType") == "AFT_COALESCENT")
{
if (consumed.Length != 2 || consumed.Any(x => x.Integer("ItemCount", 1) != 1) || types[0].EndsWith("Mythic", StringComparison.Ordinal) != types[1].EndsWith("Mythic", StringComparison.Ordinal)) throw new ApiException(400, "Coalescence requires two shards of matching quality");
var colors = types.Select(x => x[prefix.Length..].Replace("Mythic", "", StringComparison.Ordinal)).Order(StringComparer.Ordinal).ToArray();
var color = string.Join('+', colors) switch { "Amar+Nira" => "Orange", "Amar+Boreal" => "Violet", "Boreal+Nira" => "Green", _ => throw new ApiException(400, "These shard colors cannot be combined") };
output = prefix + color + (types[0].EndsWith("Mythic", StringComparison.Ordinal) ? "Mythic" : "");
}
else throw new ApiException(400, "Unknown archon fusion type");
foreach (var shard in consumed) Inventory.AddCount(inv, "MiscItems", shard.RequiredString("ItemType"), -shard.Integer("ItemCount", 1, 3));
Inventory.AddCount(inv, "MiscItems", output, 1); return ApiResponse.Json(new JObject { ["NewArchons"] = new JArray(new JObject { ["ItemType"] = output, ["ItemCount"] = 1 }) });
}
var recipe = GameData.Find("Recipes", body.RequiredString("Recipe")) ?? throw new ApiException(400, "Unknown Incarnon recipe");
var gear = Inventory.Equipment(inv, body.RequiredString("Category"), request.Q("ItemId")); var installed = (gear.Value<int>("Features") & 512) != 0;
if (body.Value<string>("Action") == "EWA_INSTALL")
{
if (installed) throw new ApiException(409, "Incarnon adapter already installed");
foreach (var ingredient in (recipe["ingredients"] as JArray ?? []).OfType<JObject>()) Inventory.AddCount(inv, "MiscItems", ingredient.RequiredString("ItemType"), -ingredient.Integer("ItemCount", 1));
gear["Features"] = gear.Value<int>("Features") | 512; gear["SkillTree"] = "0"; var type = body.RequiredString("EvoType");
if (!inv.Array("EvolutionProgress").Any(x => x.Value<string>("ItemType") == type)) inv.Array("EvolutionProgress").Add(new JObject { ["Progress"] = 0, ["Rank"] = 1, ["ItemType"] = type });
}
else if (body.Value<string>("Action") == "EWA_UNINSTALL")
{
if (!installed) throw new ApiException(409, "No Incarnon adapter installed");
Inventory.AddCount(inv, "MiscItems", recipe.RequiredString("resultType"), 1); gear["Features"] = gear.Value<int>("Features") & ~512;
}
else throw new ApiException(400, "Unknown Incarnon action");
return ApiResponse.Text();
}
private static ApiResponse Treasure(JObject inv, JObject body)
{
var legacy = body["ItemId"] is not null; string type; int oldSockets, newSockets; JObject? old;
if (legacy)
{
old = inv.Array("HybridFusionTreasures").OfType<JObject>().FirstOrDefault(x => Wire.Id(x["ItemId"]) == Wire.Id(body["ItemId"])) ?? throw new ApiException(404, "Sculpture is not owned");
type = old.RequiredString("ItemType"); oldSockets = old.Value<int>("Sockets"); newSockets = 0;
foreach (var key in (body["Sockets"] as JObject ?? throw new ApiException(400, "Missing sockets")).Properties()) { if (key.Name.Length != 1 || key.Name[0] is < 'a' or > 'p') throw new ApiException(400, "Invalid socket"); newSockets |= 1 << (key.Name[0] - 'a'); }
}
else
{
static (string Type, int Sockets) Parse(string name)
{
var split = name.LastIndexOf('_'); if (split < 0 || !int.TryParse(name[(split + 1)..], System.Globalization.NumberStyles.HexNumber, null, out var sockets) || sockets < 0) throw new ApiException(400, "Invalid sculpture name"); return (name[..split], sockets);
}
(type, oldSockets) = Parse(body.RequiredString("oldTreasureName")); var next = Parse(body.RequiredString("newTreasureName")); newSockets = next.Sockets;
if (next.Type != type) throw new ApiException(400, "Socketing cannot change sculpture type");
old = inv.Array("FusionTreasures").OfType<JObject>().FirstOrDefault(x => x.Value<string>("ItemType") == type && x.Value<int>("Sockets") == oldSockets && x.Value<int>("ItemCount") > 0) ?? throw new ApiException(400, "Sculpture is not owned");
}
var socketsMeta = GameData.Find("Resources", type)?["sockets"] as JArray ?? throw new ApiException(400, "Item has no sculpture sockets");
if (newSockets == oldSockets || (newSockets & oldSockets) != oldSockets || newSockets >= 1 << socketsMeta.Count) throw new ApiException(400, "Invalid sculpture socket change");
var changes = new JObject(); var filled = newSockets & ~oldSockets;
for (var i = 0; i < socketsMeta.Count; i++) if ((filled & 1 << i) != 0)
{
var star = socketsMeta[i].ToString(); Inventory.AddCount(inv, "MiscItems", star, -1); Inventory.Combine(changes, new JObject { ["MiscItems"] = new JArray(new JObject { ["ItemType"] = star, ["ItemCount"] = -1 }) });
}
if (legacy) { old["Sockets"] = newSockets; return ApiResponse.Text(); }
var count = old.Value<int>("ItemCount"); if (count == 1) old.Remove(); else old["ItemCount"] = count - 1;
var upgraded = inv.Array("FusionTreasures").OfType<JObject>().FirstOrDefault(x => x.Value<string>("ItemType") == type && x.Value<int>("Sockets") == newSockets);
if (upgraded is null) inv.Array("FusionTreasures").Add(new JObject { ["ItemType"] = type, ["Sockets"] = newSockets, ["ItemCount"] = 1 }); else upgraded["ItemCount"] = checked(upgraded.Value<int>("ItemCount") + 1);
changes["FusionTreasures"] = new JArray(new JObject { ["ItemType"] = type, ["Sockets"] = oldSockets, ["ItemCount"] = -1 }, new JObject { ["ItemType"] = type, ["Sockets"] = newSockets, ["ItemCount"] = 1 }); return ApiResponse.Json(changes);
}
}