From 239d523513d0b77393dab5748a4af441e0fd6599 Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 18 Jan 2023 22:52:25 +0100 Subject: [PATCH] Skip announcements during matching --- .../RemoteCommunication.cs | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs b/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs index 4e60cf48f..19f161540 100644 --- a/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs +++ b/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs @@ -162,6 +162,11 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { return; } + if (MatchActivelySemaphore.CurrentCount == 0) { + // We shouldn't bother with announcements while we're matching, it can wait until we're done + return; + } + await RequestsSemaphore.WaitAsync().ConfigureAwait(false); try { @@ -169,6 +174,11 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { return; } + if (MatchActivelySemaphore.CurrentCount == 0) { + // We shouldn't bother with announcements while we're matching, it can wait until we're done + return; + } + // Don't announce if we don't meet conditions bool? eligible = await IsEligibleForListing().ConfigureAwait(false); @@ -420,7 +430,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { } // Request persona update if needed - if ((DateTime.UtcNow > LastPersonaStateRequest.AddMinutes(MinPersonaStateTTL)) && (DateTime.UtcNow > LastAnnouncement.AddMinutes(ShouldSendAnnouncementEarlier ? MinAnnouncementTTL : MaxAnnouncementTTL))) { + if (Bot.BotConfig.RemoteCommunication.HasFlag(BotConfig.ERemoteCommunication.PublicListing) && Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.SteamTradeMatcher) && (DateTime.UtcNow > LastPersonaStateRequest.AddMinutes(MinPersonaStateTTL)) && (DateTime.UtcNow > LastAnnouncement.AddMinutes(ShouldSendAnnouncementEarlier ? MinAnnouncementTTL : MaxAnnouncementTTL))) { LastPersonaStateRequest = DateTime.UtcNow; Bot.RequestPersonaStateUpdate(); } @@ -560,6 +570,8 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { return; } + bool tradesSent; + try { Bot.ArchiLogger.LogGenericInfo(Strings.Starting); @@ -625,16 +637,22 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { using (await Bot.Actions.GetTradingLock().ConfigureAwait(false)) { #pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose Bot.ArchiLogger.LogGenericInfo(Strings.Starting); - await MatchActively(response.Value.Users, ourInventory, acceptedMatchableTypes).ConfigureAwait(false); + + tradesSent = await MatchActively(response.Value.Users, ourInventory, acceptedMatchableTypes).ConfigureAwait(false); } Bot.ArchiLogger.LogGenericInfo(Strings.Done); } finally { MatchActivelySemaphore.Release(); } + + if (tradesSent && ShouldSendHeartBeats && (DateTime.UtcNow > LastAnnouncement.AddMinutes(ShouldSendAnnouncementEarlier ? MinAnnouncementTTL : MaxAnnouncementTTL))) { + // If we're announced, it makes sense to update our state now, at least once + Bot.RequestPersonaStateUpdate(); + } } - private async Task MatchActively(IReadOnlyCollection listedUsers, Dictionary ourInventory, IReadOnlyCollection acceptedMatchableTypes) { + private async Task MatchActively(IReadOnlyCollection listedUsers, Dictionary ourInventory, IReadOnlyCollection acceptedMatchableTypes) { if ((listedUsers == null) || (listedUsers.Count == 0)) { throw new ArgumentNullException(nameof(listedUsers)); } @@ -653,7 +671,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { // User doesn't have any more dupes in the inventory Bot.ArchiLogger.LogGenericTrace(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, $"{nameof(ourFullState)} || {nameof(ourTradableState)}")); - return; + return false; } HashSet pendingMobileTradeOfferIDs = new(); @@ -832,9 +850,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { if ((itemsToGive.Count != itemsToReceive.Count) || !Trading.IsFairExchange(itemsToGive, itemsToReceive)) { // Failsafe - Bot.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, Strings.ErrorAborted)); - - return; + throw new InvalidOperationException($"{nameof(itemsToGive)} && {nameof(itemsToReceive)}"); } Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Localization.Strings.MatchingFound, itemsToReceive.Count, listedUser.SteamID, listedUser.Nickname)); @@ -951,5 +967,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { } Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Localization.Strings.ActivelyMatchingItemsRound, matchedSets)); + + return matchedSets > 0; } }