diff --git a/ArchiSteamFarm.Tests/Trading.cs b/ArchiSteamFarm.Tests/Trading.cs index c415e1d21..620e95c01 100644 --- a/ArchiSteamFarm.Tests/Trading.cs +++ b/ArchiSteamFarm.Tests/Trading.cs @@ -249,6 +249,28 @@ namespace ArchiSteamFarm.Tests { Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } + [TestMethod] + public void SingleGameSingleTypeBigDifferenceReject() { + HashSet inventory = new HashSet { + CreateItem(1), + CreateItem(2, 2), + CreateItem(3, 2), + CreateItem(4, 3), + CreateItem(5, 10) + }; + + HashSet itemsToGive = new HashSet { + CreateItem(2), + CreateItem(5) + }; + HashSet itemsToReceive = new HashSet { + CreateItem(3), + CreateItem(4) + }; + + Assert.IsFalse(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); + } + [TestMethod] public void SingleGameSingleTypeGoodAccept() { HashSet inventory = new HashSet { CreateItem(1, 2) }; diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 8c0b8d7d2..cc3192898 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -177,9 +177,15 @@ namespace ArchiSteamFarm { // At this point we're sure that both number of unique items in the set stays the same, as well as number of our actual sets // We need to ensure set progress here and keep in mind overpaying, so we'll calculate neutrality as a difference in amounts at appropriate indexes - // The higher the neutrality the better the trade, with 0 being the absolute minimum we're willing to accept - if (afterAmounts.Select((t, i) => (int) (t - beforeAmounts[i])).Sum() < 0) { - return false; + // Neutrality can't reach value below 0 at any single point of calculation, as that would imply a loss of progress even if we'd end up with a positive value by the end + int neutrality = 0; + + for (byte i = 0; i < afterAmounts.Count; i++) { + neutrality += (int) (afterAmounts[i] - beforeAmounts[i]); + + if (neutrality < 0) { + return false; + } } }