using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class GiftingApi(ItemCatalog catalog, TimeProvider clock, bool syncOfficialEvents = true)
{
public static readonly HashSet<string> Controllers = ["giftingController"];
public ApiResponse Handle(JObject state, JObject sender, ApiRequest request)
{
var body = request.ObjectBody(); var inv = sender.Obj("Inventory"); new PlayerApi(clock).DailyReset(inv);
var recipient = Accounts.All(state).FirstOrDefault(x => body["RecipientId"] is null ? x.Value<string>("DisplayName") == body.Value<string>("Recipient") : x.Value<string>("id") == Wire.Id(body["RecipientId"])) ?? throw new ApiException(400, "9");
if (ReferenceEquals(sender, recipient)) throw new ApiException(400, "Cannot send a gift to yourself"); var target = recipient.Obj("Inventory");
if (target.Array("Suits").Count == 0) throw new ApiException(400, "14"); var mode = target.Obj("Settings").Value<string>("GiftMode");
bool Friend(string a, string b) => state.Array("friendships").Any(x => x.Value<string>("owner") == a && x.Value<string>("friend") == b);
if (mode == "GIFT_MODE_NONE" || mode == "GIFT_MODE_FRIENDS" && !(Friend(sender.RequiredString("id"), recipient.RequiredString("id")) && Friend(recipient.RequiredString("id"), sender.RequiredString("id")))) throw new ApiException(400, "17");
if (recipient.Array("Ignored").Any(x => x.ToString() == sender.Value<string>("id"))) throw new ApiException(403, "Recipient ignores this sender");
var p = body["PurchaseParams"] as JObject;
if (p is null)
{
var product = (request.Q("productName") ?? throw new ApiException(400, "Missing gift product")).Split(';');
p = new JObject { ["Source"] = 0, ["StoreItem"] = product[0], ["Quantity"] = int.TryParse(request.Q("quantity"), out var count) ? count : 1, ["UsePremium"] = true, ["Durability"] = product.Length > 1 ? int.Parse(product[1]) : 0 };
}
var (category, meta) = catalog.Get(p.RequiredString("StoreItem")); if (meta.Value<int>("masteryReq") > target.Value<int>("PlayerLevel")) throw new ApiException(400, "2");
if (!inv.Value<bool>("infiniteGifts") && inv.Value<int>("GiftsRemaining") <= 0) throw new ApiException(400, "10");
var purchase = new PurchaseApi(catalog, clock, syncOfficialEvents).Handle(state, sender, request with { Method = "POST", Body = new JObject { ["PurchaseParams"] = p.DeepClone() }.ToString(Formatting.None) }, gift: true); var response = JObject.Parse(purchase.Body); var changes = response.Obj("InventoryChanges");
if (!inv.Value<bool>("infiniteGifts")) { inv["GiftsRemaining"] = inv.Value<int>("GiftsRemaining") - 1; changes["GiftsRemaining"] = -1; }
if (category == "Bundles" && meta.Value<string>("giftingBonus") is string bonus) Inventory.Combine(changes, new ItemAcquisition(catalog, clock, sender).Add(inv, bonus, premium: true));
var name = sender.RequiredString("DisplayName") + "#" + sender.RequiredString("id")[^3..];
InboxApi.Create(recipient, new JObject { ["SenderId"] = sender["id"]!.DeepClone(), ["sndr"] = name, ["msg"] = string.IsNullOrEmpty(body.Value<string>("Message")) ? "/Lotus/Language/Menu/GiftReceivedBody_NoCustomMessage" : body.Value<string>("Message"), ["sub"] = "/Lotus/Language/Menu/GiftReceivedSubject", ["icon"] = GameData.Find("Flavour", inv.Value<string>("ActiveAvatarImageType"))?["icon"]?.DeepClone(), ["arg"] = new JArray(new JObject { ["Key"] = "GIFTER_NAME", ["Tag"] = name }, new JObject { ["Key"] = "GIFT_QUANTITY", ["Tag"] = p.Integer("Quantity", 1, 100) }), ["gifts"] = new JArray(new JObject { ["GiftType"] = p["StoreItem"]!.DeepClone(), ["Quantity"] = p["Quantity"]!.DeepClone(), ["DurabilityDays"] = new[] { 3, 7, 30, 90 }[p.Value<int>("Durability")] }) }, clock.GetUtcNow());
return ApiResponse.Json(response);
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XFEWarframe.Core.Data;
namespace XFEWarframe.Core.Services;
public sealed class GiftingApi(ItemCatalog catalog, TimeProvider clock, bool syncOfficialEvents = true)
{
public static readonly HashSet<string> Controllers = ["giftingController"];
public ApiResponse Handle(JObject state, JObject sender, ApiRequest request)
{
var body = request.ObjectBody(); var inv = sender.Obj("Inventory"); new PlayerApi(clock).DailyReset(inv);
var recipient = Accounts.All(state).FirstOrDefault(x => body["RecipientId"] is null ? x.Value<string>("DisplayName") == body.Value<string>("Recipient") : x.Value<string>("id") == Wire.Id(body["RecipientId"])) ?? throw new ApiException(400, "9");
if (ReferenceEquals(sender, recipient)) throw new ApiException(400, "Cannot send a gift to yourself"); var target = recipient.Obj("Inventory");
if (target.Array("Suits").Count == 0) throw new ApiException(400, "14"); var mode = target.Obj("Settings").Value<string>("GiftMode");
bool Friend(string a, string b) => state.Array("friendships").Any(x => x.Value<string>("owner") == a && x.Value<string>("friend") == b);
if (mode == "GIFT_MODE_NONE" || mode == "GIFT_MODE_FRIENDS" && !(Friend(sender.RequiredString("id"), recipient.RequiredString("id")) && Friend(recipient.RequiredString("id"), sender.RequiredString("id")))) throw new ApiException(400, "17");
if (recipient.Array("Ignored").Any(x => x.ToString() == sender.Value<string>("id"))) throw new ApiException(403, "Recipient ignores this sender");
var p = body["PurchaseParams"] as JObject;
if (p is null)
{
var product = (request.Q("productName") ?? throw new ApiException(400, "Missing gift product")).Split(';');
p = new JObject { ["Source"] = 0, ["StoreItem"] = product[0], ["Quantity"] = int.TryParse(request.Q("quantity"), out var count) ? count : 1, ["UsePremium"] = true, ["Durability"] = product.Length > 1 ? int.Parse(product[1]) : 0 };
}
var (category, meta) = catalog.Get(p.RequiredString("StoreItem")); if (meta.Value<int>("masteryReq") > target.Value<int>("PlayerLevel")) throw new ApiException(400, "2");
if (!inv.Value<bool>("infiniteGifts") && inv.Value<int>("GiftsRemaining") <= 0) throw new ApiException(400, "10");
var purchase = new PurchaseApi(catalog, clock, syncOfficialEvents).Handle(state, sender, request with { Method = "POST", Body = new JObject { ["PurchaseParams"] = p.DeepClone() }.ToString(Formatting.None) }, gift: true); var response = JObject.Parse(purchase.Body); var changes = response.Obj("InventoryChanges");
if (!inv.Value<bool>("infiniteGifts")) { inv["GiftsRemaining"] = inv.Value<int>("GiftsRemaining") - 1; changes["GiftsRemaining"] = -1; }
if (category == "Bundles" && meta.Value<string>("giftingBonus") is string bonus) Inventory.Combine(changes, new ItemAcquisition(catalog, clock, sender).Add(inv, bonus, premium: true));
var name = sender.RequiredString("DisplayName") + "#" + sender.RequiredString("id")[^3..];
InboxApi.Create(recipient, new JObject { ["SenderId"] = sender["id"]!.DeepClone(), ["sndr"] = name, ["msg"] = string.IsNullOrEmpty(body.Value<string>("Message")) ? "/Lotus/Language/Menu/GiftReceivedBody_NoCustomMessage" : body.Value<string>("Message"), ["sub"] = "/Lotus/Language/Menu/GiftReceivedSubject", ["icon"] = GameData.Find("Flavour", inv.Value<string>("ActiveAvatarImageType"))?["icon"]?.DeepClone(), ["arg"] = new JArray(new JObject { ["Key"] = "GIFTER_NAME", ["Tag"] = name }, new JObject { ["Key"] = "GIFT_QUANTITY", ["Tag"] = p.Integer("Quantity", 1, 100) }), ["gifts"] = new JArray(new JObject { ["GiftType"] = p["StoreItem"]!.DeepClone(), ["Quantity"] = p["Quantity"]!.DeepClone(), ["DurabilityDays"] = new[] { 3, 7, 30, 90 }[p.Value<int>("Durability")] }) }, clock.GetUtcNow());
return ApiResponse.Json(response);
}
}