From 3b2ca10b0554d5d6b83440f8676ef7230526e171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Mon, 13 May 2024 09:29:30 +0200 Subject: [PATCH] Closes #3203 When excessive amount of "missing amounts", so items in the set was missing on our side, there was a possibility for our logic to classify a good trade as bad one, because we didn't fill in enough holes, as the subtraction in the condition was calculated on each loop rather than once initially. Since this could only worsen the neutrality score, but never improve it (as the amounts were sorted ascending), there was no abusive possibility due to that, only ignoring otherwise valid trades classifying them as worse than they were in reality. --- ArchiSteamFarm.Tests/Trading.cs | 24 ++++++++++++++++++++++++ ArchiSteamFarm/Steam/Exchange/Trading.cs | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ArchiSteamFarm.Tests/Trading.cs b/ArchiSteamFarm.Tests/Trading.cs index a2ee00fb2..ca2006c9c 100644 --- a/ArchiSteamFarm.Tests/Trading.cs +++ b/ArchiSteamFarm.Tests/Trading.cs @@ -52,6 +52,30 @@ public sealed class Trading { Assert.IsFalse(IsTradeNeutralOrBetter(inventory, itemsToGive, itemsToReceive)); } + [TestMethod] + public void Issue3203() { + HashSet inventory = [ + CreateItem(1, amount: 2), + CreateItem(2, amount: 6), + CreateItem(3), + CreateItem(4) + ]; + + HashSet itemsToGive = [ + CreateItem(1), + CreateItem(2, amount: 2) + ]; + + HashSet itemsToReceive = [ + CreateItem(5), + CreateItem(6), + CreateItem(7) + ]; + + Assert.IsTrue(IsFairExchange(itemsToGive, itemsToReceive)); + Assert.IsTrue(IsTradeNeutralOrBetter(inventory, itemsToGive, itemsToReceive)); + } + [TestMethod] public void MismatchRarityIsNotFair() { HashSet itemsToGive = [ diff --git a/ArchiSteamFarm/Steam/Exchange/Trading.cs b/ArchiSteamFarm/Steam/Exchange/Trading.cs index ec134f9d4..3157115e4 100644 --- a/ArchiSteamFarm/Steam/Exchange/Trading.cs +++ b/ArchiSteamFarm/Steam/Exchange/Trading.cs @@ -178,7 +178,9 @@ public sealed class Trading : IDisposable { } // Otherwise, fill the missing holes in our data if needed, since we actually had zeros there - for (byte i = 0; i < afterAmounts.Count - beforeAmounts.Count; i++) { + byte missingAmounts = (byte) (afterAmounts.Count - beforeAmounts.Count); + + for (byte i = 0; i < missingAmounts; i++) { beforeAmounts.Insert(0, 0); }