From 4f833c8fc7cb5bbcad53b245a40cacbe42ca98f0 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 27 Jun 2018 21:18:54 +0200 Subject: [PATCH] ArchiBoT/ASF code unification --- ArchiSteamFarm/ArchiWebHandler.cs | 87 +++++++++++++++++++++++++------ ArchiSteamFarm/Bot.cs | 10 ++-- ArchiSteamFarm/Statistics.cs | 2 +- ArchiSteamFarm/Trading.cs | 2 +- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 81cd3ce8d..b90a273b5 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -359,6 +359,17 @@ namespace ArchiSteamFarm { return null; } + if (SteamID == 0) { + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } + + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + return null; + } + } + string request = "/mobileconf/details/" + confirmation.ID + "?a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&l=english&m=android&p=" + WebUtility.UrlEncode(deviceID) + "&t=" + time + "&tag=conf"; Steam.ConfirmationDetails response = await UrlGetToJsonObjectWithSession(SteamCommunityURL, request).ConfigureAwait(false); @@ -376,6 +387,17 @@ namespace ArchiSteamFarm { return null; } + if (SteamID == 0) { + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } + + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + return null; + } + } + string request = "/mobileconf/conf?a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&l=english&m=android&p=" + WebUtility.UrlEncode(deviceID) + "&t=" + time + "&tag=conf"; return await UrlGetToHtmlDocumentWithSession(SteamCommunityURL, request).ConfigureAwait(false); } @@ -425,16 +447,31 @@ namespace ArchiSteamFarm { } [SuppressMessage("ReSharper", "FunctionComplexityOverflow")] - internal async Task> GetMyInventory(bool tradableOnly = false, uint appID = Steam.Asset.SteamAppID, byte contextID = Steam.Asset.SteamCommunityContextID, IReadOnlyCollection wantedTypes = null, IReadOnlyCollection wantedRealAppIDs = null) { + internal async Task> GetInventory(ulong steamID = 0, uint appID = Steam.Asset.SteamAppID, byte contextID = Steam.Asset.SteamCommunityContextID, bool? tradable = null, IReadOnlyCollection wantedTypes = null, IReadOnlyCollection wantedRealAppIDs = null) { if ((appID == 0) || (contextID == 0)) { Bot.ArchiLogger.LogNullError(nameof(appID) + " || " + nameof(contextID)); return null; } + if (steamID == 0) { + if (SteamID == 0) { + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } + + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + return null; + } + } + + steamID = SteamID; + } + HashSet result = new HashSet(); // 5000 is maximum allowed count per single request - string request = "/inventory/" + SteamID + "/" + appID + "/" + contextID + "?count=5000&l=english"; + string request = "/inventory/" + steamID + "/" + appID + "/" + contextID + "?count=5000&l=english"; ulong startAssetID = 0; await InventorySemaphore.WaitAsync().ConfigureAwait(false); @@ -462,7 +499,7 @@ namespace ArchiSteamFarm { return null; } - Dictionary descriptionMap = new Dictionary(); + Dictionary descriptionMap = new Dictionary(); foreach (Steam.InventoryResponse.Description description in response.Descriptions.Where(description => description != null)) { if (description.ClassID == 0) { Bot.ArchiLogger.LogNullError(nameof(description.ClassID)); @@ -473,6 +510,12 @@ namespace ArchiSteamFarm { continue; } + Steam.Asset.EType type = Steam.Asset.EType.Unknown; + + if (!string.IsNullOrEmpty(description.Type)) { + type = GetItemType(description.Type); + } + uint realAppID = 0; if (!string.IsNullOrEmpty(description.MarketHashName)) { @@ -483,18 +526,12 @@ namespace ArchiSteamFarm { realAppID = description.AppID; } - Steam.Asset.EType type = Steam.Asset.EType.Unknown; - - if (!string.IsNullOrEmpty(description.Type)) { - type = GetItemType(description.Type); - } - - descriptionMap[description.ClassID] = (realAppID, type, description.Tradable); + descriptionMap[description.ClassID] = (description.Tradable, type, realAppID); } foreach (Steam.Asset asset in response.Assets.Where(asset => asset != null)) { - if (descriptionMap.TryGetValue(asset.ClassID, out (uint RealAppID, Steam.Asset.EType Type, bool Tradable) description)) { - if (tradableOnly && !description.Tradable) { + if (descriptionMap.TryGetValue(asset.ClassID, out (bool Tradable, Steam.Asset.EType Type, uint RealAppID) description)) { + if ((tradable.HasValue && (description.Tradable != tradable.Value)) || (wantedTypes?.Contains(description.Type) == false) || (wantedRealAppIDs?.Contains(description.RealAppID) == false)) { continue; } @@ -502,10 +539,6 @@ namespace ArchiSteamFarm { asset.Type = description.Type; } - if ((wantedTypes?.Contains(asset.Type) == false) || (wantedRealAppIDs?.Contains(asset.RealAppID) == false)) { - continue; - } - result.Add(asset); } @@ -821,6 +854,17 @@ namespace ArchiSteamFarm { return null; } + if (SteamID == 0) { + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } + + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + return null; + } + } + string request = "/mobileconf/ajaxop?a=" + SteamID + "&cid=" + confirmationID + "&ck=" + confirmationKey + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&l=english&m=android&op=" + (accept ? "allow" : "cancel") + "&p=" + WebUtility.UrlEncode(deviceID) + "&t=" + time + "&tag=conf"; Steam.BooleanResponse response = await UrlGetToJsonObjectWithSession(SteamCommunityURL, request).ConfigureAwait(false); @@ -833,6 +877,17 @@ namespace ArchiSteamFarm { return null; } + if (SteamID == 0) { + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } + + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + return null; + } + } + const string request = "/mobileconf/multiajaxop"; // Extra entry for sessionID diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 2a6896364..ade4e4039 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -2772,7 +2772,7 @@ namespace ArchiSteamFarm { await LootingSemaphore.WaitAsync().ConfigureAwait(false); try { - HashSet inventory = await ArchiWebHandler.GetMyInventory(true, appID, contextID).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetInventory(CachedSteamID, appID, contextID, true).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -3652,7 +3652,7 @@ namespace ArchiSteamFarm { LootingScheduled = false; } - HashSet inventory = await ArchiWebHandler.GetMyInventory(true, wantedTypes: BotConfig.LootableTypes).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetInventory(CachedSteamID, tradable: true, wantedTypes: BotConfig.LootableTypes).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -3760,7 +3760,7 @@ namespace ArchiSteamFarm { await LootingSemaphore.WaitAsync().ConfigureAwait(false); try { - HashSet inventory = await ArchiWebHandler.GetMyInventory(true, wantedRealAppIDs: realAppIDs).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetInventory(CachedSteamID, tradable: true, wantedRealAppIDs: realAppIDs).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -5030,7 +5030,7 @@ namespace ArchiSteamFarm { LootingScheduled = false; } - HashSet inventory = await ArchiWebHandler.GetMyInventory(true, wantedTypes: transferTypes).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetInventory(CachedSteamID, tradable: true, wantedTypes: transferTypes).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -5120,7 +5120,7 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotNotConnected); } - HashSet inventory = await ArchiWebHandler.GetMyInventory(false, wantedTypes: new HashSet { Steam.Asset.EType.BoosterPack }).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetInventory(CachedSteamID, wantedTypes: new HashSet { Steam.Asset.EType.BoosterPack }).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index 790f71da4..5fe5d673a 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -111,7 +111,7 @@ namespace ArchiSteamFarm { return; } - HashSet inventory = await Bot.ArchiWebHandler.GetMyInventory(true, wantedTypes: acceptedMatchableTypes).ConfigureAwait(false); + HashSet inventory = await Bot.ArchiWebHandler.GetInventory(Bot.CachedSteamID, tradable: true, wantedTypes: acceptedMatchableTypes).ConfigureAwait(false); // This is actually inventory failure, so we'll stop sending heartbeats but not record it as valid check if (inventory == null) { diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 10d600e1a..5d53e6442 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -308,7 +308,7 @@ namespace ArchiSteamFarm { } // Now check if it's worth for us to do the trade - HashSet inventory = await Bot.ArchiWebHandler.GetMyInventory(false, wantedTypes: types, wantedRealAppIDs: appIDs).ConfigureAwait(false); + HashSet inventory = await Bot.ArchiWebHandler.GetInventory(Bot.CachedSteamID, wantedTypes: types, wantedRealAppIDs: appIDs).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { // If we can't check our inventory when not using MatchEverything, this is a temporary failure Bot.ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorIsEmpty, nameof(inventory)));