using Newtonsoft.Json.Linq; using XFEWarframe.Core; using XFEWarframe.Core.Data; using XFEWarframe.Core.Services; using Xunit; namespace XFEWarframe.Tests; public sealed partial class EngineTests { private Task Trade(Dictionary who, Dictionary other, string op, object? offer = null, string revision = "0", string? clan = null) { var query = new Dictionary(who) { ["buddyId"] = other["accountId"], ["op"] = op, ["revision"] = revision }; if (clan is not null) query["guildId"] = clan; return Call("/api/trading.php", offer ?? new { }, query); } [Fact] public async Task TradeUsesOwnedModFingerprintAndPaidPlatinumAndPersistsClanTax() { var a = await Login(); var b = await Login("buyer@test.local"); var guild = await CreateClan(a, "Trading"); await Give(a, "/Lotus/Types/Keys/DojoKey"); var clan = Wire.Id(guild["_id"]); var mod = GameData.Table("Upgrades").Properties().First(x => x.Value.Value("tradable") && x.Value.Value("rarity") == "COMMON"); var modId = Wire.NewId(); await Import(a, new { PlayerLevel = 5, TradesRemaining = 5, RegularCredits = 5000, Upgrades = new[] { new { ItemId = Wire.Oid(modId), ItemType = mod.Name, UpgradeFingerprint = "{\"lvl\":2}" } } }); await Import(b, new { PlayerLevel = 5, TradesRemaining = 5, RegularCredits = 5000, PremiumCredits = 60, PremiumCreditsFree = 50 }); Assert.Equal(200, (await Call("/api/trading.php", new { }, new(a) { ["op"] = "5", ["tax"] = "20" })).Status); var offered = await Trade(a, b, "0", new { Upgrades = new[] { new { ItemId = Wire.Oid(modId), ItemType = mod.Name, UpgradeFingerprint = "{\"lvl\":10}" } } }, clan: clan); Assert.True(offered.Status == 200, offered.Body); Assert.Equal("{\"lvl\":2}", JObject.Parse(offered.Body)["PendingTrades"]![0]!["Giving"]!["Upgrades"]![0]!.Value("UpgradeFingerprint")); Assert.Single((await InventoryOf(b))["PendingTrades"]!); Assert.Equal(400, (await Trade(b, a, "1", new { PremiumCredits = 11 })).Status); Assert.Equal(200, (await Trade(b, a, "1", new { PremiumCredits = 5 })).Status); Assert.Equal(409, (await Trade(a, b, "6")).Status); Assert.Equal(200, (await Trade(a, b, "6", revision: "1")).Status); Assert.Equal(200, (await Trade(b, a, "6", revision: "1")).Status); Assert.Equal("11", (await Trade(a, b, "2", revision: "1")).Body); var completed = await Trade(b, a, "2", revision: "1"); Assert.True(completed.Status == 200, completed.Body); Assert.Equal("12", completed.Body); var afterA = await InventoryOf(a); var afterB = await InventoryOf(b); Assert.Empty(afterA["Upgrades"]!); Assert.Equal("{\"lvl\":2}", afterB["Upgrades"]![0]!.Value("UpgradeFingerprint")); Assert.Equal(5, afterA.Value("PremiumCredits")); Assert.Equal(55, afterB.Value("PremiumCredits")); Assert.Equal(50, afterB.Value("PremiumCreditsFree")); Assert.Equal(2000, afterA.Value("RegularCredits")); Assert.Equal(2600, afterB.Value("RegularCredits")); Assert.Equal(4, afterA.Value("TradesRemaining")); Assert.Equal(4, afterB.Value("TradesRemaining")); Assert.Equal(900, JObject.Parse((await Call("/api/getVault.php", query: a)).Body)["Vault"]!.Value("DojoRefundRegularCredits")); Assert.Equal(404, (await Trade(a, b, "2", revision: "1")).Status); Assert.Empty(afterA["PendingTrades"]!); } [Fact] public async Task TradeRevisionClearsConsentAndUnaffordableTradeRollsBackBothSides() { var a = await Login(); var b = await Login("other@test.local"); const string type = "/Lotus/Types/Items/MiscItems/Forma"; await Import(a, new { PlayerLevel = 3, TradesRemaining = 3, RegularCredits = 100000 }); await Import(b, new { PlayerLevel = 3, TradesRemaining = 3 }); await Give(a, type); Assert.Equal(200, (await Trade(a, b, "0", new { MiscItems = new[] { new { ItemType = type, ItemCount = 1 } } })).Status); await Trade(a, b, "6"); await Trade(b, a, "6"); Assert.Equal("11", (await Trade(a, b, "2")).Body); var revised = await Trade(b, a, "1", new { PremiumCredits = 0 }); var info = JObject.Parse(revised.Body)["PendingTrades"]![0]!; Assert.False(info.Value("SelfReady")); Assert.False(info.Value("BuddyReady")); Assert.Equal(2, info.Value("State")); Assert.Equal(409, (await Trade(b, a, "2", revision: "1")).Status); await Trade(a, b, "6", revision: "1"); await Trade(b, a, "6", revision: "1"); await Trade(a, b, "2", revision: "1"); var beforeA = await InventoryOf(a); var beforeB = await InventoryOf(b); Assert.Equal(400, (await Trade(b, a, "2", revision: "1")).Status); Assert.True(JToken.DeepEquals(beforeA, await InventoryOf(a))); Assert.True(JToken.DeepEquals(beforeB, await InventoryOf(b))); } [Fact] public async Task SellingAnOfferedItemMakesTradeFailWithoutMintingTheItem() { var a = await Login(); var b = await Login("other@test.local"); const string type = "/Lotus/Types/Items/MiscItems/Forma"; foreach (var player in new[] { a, b }) await Import(player, new { PlayerLevel = 3, TradesRemaining = 3, RegularCredits = 100000 }); await Give(a, type); await Trade(a, b, "0", new { MiscItems = new[] { new { ItemType = type, ItemCount = 1 } } }); await Trade(a, b, "6"); await Trade(b, a, "6"); await Trade(a, b, "2"); Assert.Equal(200, (await Call("/api/sell.php", new { Items = new { MiscItems = new[] { new { String = type, Count = 1 } } }, SellPrice = 1 }, a)).Status); Assert.Equal(400, (await Trade(b, a, "2")).Status); Assert.Equal(0, Count(await InventoryOf(b), "MiscItems", type)); Assert.Equal(3, (await InventoryOf(b)).Value("TradesRemaining")); } [Fact] public async Task DucatSalesValidatePriceAndInvalidMixedSalesRollback() { var q = await Login(); var prime = GameData.Table("Resources").Properties().First(x => x.Value.Value("primeSellingPrice") > 0); await Give(q, prime.Name, 2); var value = prime.Value.Value("primeSellingPrice"); var before = await InventoryOf(q); var sale = new { Items = new { MiscItems = new[] { new { String = prime.Name, Count = 2 } } }, SellPrice = value * 2, SellCurrency = "SC_PrimeBucks" }; Assert.Equal(400, (await Call("/api/sell.php", new { sale.Items, SellPrice = value * 3, sale.SellCurrency }, q)).Status); Assert.True(JToken.DeepEquals(before, await InventoryOf(q))); Assert.Equal(404, (await Call("/api/sell.php", new { Items = new { sale.Items.MiscItems, LongGuns = new[] { new { String = Wire.NewId(), Count = 1 } } }, SellPrice = 5000 }, q)).Status); Assert.True(JToken.DeepEquals(before, await InventoryOf(q))); Assert.Equal(200, (await Call("/api/sell.php", sale, q)).Status); Assert.Equal(value * 2, Count(await InventoryOf(q), "MiscItems", "/Lotus/Types/Items/MiscItems/PrimeBucks")); Assert.Equal(400, (await Call("/api/sell.php", sale, q)).Status); } [Fact] public async Task FeedingPrinceHonorsVisionGatesAndConsumesFoodOnce() { var q = await Login(); const string type = "/Lotus/Types/Items/MiscItems/MushroomFood"; await Give(q, type, 30); var blocked = await Call("/api/feedPrince.php", new { Mode = "r", Amount = 21 }, q); Assert.Equal(200, blocked.Status); Assert.False(JObject.Parse(blocked.Body).Value("FeedSucceeded")); Assert.Equal(30, Count(await InventoryOf(q), "MiscItems", type)); Assert.Equal(200, (await Call("/api/feedPrince.php", new { Mode = "r", Amount = 20 }, q)).Status); Assert.Equal(10, Count(await InventoryOf(q), "MiscItems", type)); await Import(q, new { NodeIntrosCompleted = new[] { "CompletedVision1" } }); Assert.Equal(400, (await Call("/api/feedPrince.php", new { Mode = "r", Amount = 11 }, q)).Status); Assert.Equal(20, (await InventoryOf(q))["NokkoColony"]!.Value("FeedLevel")); } [Fact] public async Task WeeklyClanBonusRollIsStableAcrossRetriesAndKeepsUnclaimedWeeks() { var q = await Login(); var query = new Dictionary(q) { ["gvbMode"] = "r" }; var first = await Call("/api/guildVaultBonusRewards.php", new { WeekCount = 95 }, query); Assert.True(first.Status == 200, first.Body); Assert.Equal(first.Body, (await Call("/api/guildVaultBonusRewards.php", new { WeekCount = 95 }, query)).Body); Assert.Equal(200, (await Call("/api/guildVaultBonusRewards.php", new { WeekCount = 96 }, query)).Status); Assert.Equal(2, (await InventoryOf(q))["WeeklyGuildVaultBonusInfo"]!.Count()); } }