From c4f47c56da5cd58e74c37196c7788c0cf25fd6d3 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 8 Jun 2016 13:01:41 +0200 Subject: [PATCH] Misc --- ArchiSteamFarm/ArchiWebHandler.cs | 71 ++++++++++++++++--------------- ArchiSteamFarm/Bot.cs | 2 +- ArchiSteamFarm/Trading.cs | 2 +- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 1cee1eaa9..fbbfe6685 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -393,16 +393,14 @@ namespace ArchiSteamFarm { State = trade["trade_offer_state"].AsEnum() }; - foreach (KeyValue item in trade["items_to_give"].Children) { - Steam.Item steamItem = new Steam.Item { - AppID = (uint) item["appid"].AsUnsignedLong(), - ContextID = item["contextid"].AsUnsignedLong(), - AssetID = item["assetid"].AsUnsignedLong(), - ClassID = item["classid"].AsUnsignedLong(), - InstanceID = item["instanceid"].AsUnsignedLong(), - Amount = (uint) item["amount"].AsUnsignedLong() - }; - + foreach (Steam.Item steamItem in trade["items_to_give"].Children.Select(item => new Steam.Item { + AppID = (uint) item["appid"].AsUnsignedLong(), + ContextID = item["contextid"].AsUnsignedLong(), + AssetID = item["assetid"].AsUnsignedLong(), + ClassID = item["classid"].AsUnsignedLong(), + InstanceID = item["instanceid"].AsUnsignedLong(), + Amount = (uint) item["amount"].AsUnsignedLong() + })) { Tuple description; if (descriptions.TryGetValue(steamItem.ClassID, out description)) { steamItem.RealAppID = description.Item1; @@ -412,16 +410,14 @@ namespace ArchiSteamFarm { tradeOffer.ItemsToGive.Add(steamItem); } - foreach (KeyValue item in trade["items_to_receive"].Children) { - Steam.Item steamItem = new Steam.Item { - AppID = (uint) item["appid"].AsUnsignedLong(), - ContextID = item["contextid"].AsUnsignedLong(), - AssetID = item["assetid"].AsUnsignedLong(), - ClassID = item["classid"].AsUnsignedLong(), - InstanceID = item["instanceid"].AsUnsignedLong(), - Amount = (uint) item["amount"].AsUnsignedLong() - }; - + foreach (Steam.Item steamItem in trade["items_to_receive"].Children.Select(item => new Steam.Item { + AppID = (uint) item["appid"].AsUnsignedLong(), + ContextID = item["contextid"].AsUnsignedLong(), + AssetID = item["assetid"].AsUnsignedLong(), + ClassID = item["classid"].AsUnsignedLong(), + InstanceID = item["instanceid"].AsUnsignedLong(), + Amount = (uint) item["amount"].AsUnsignedLong() + })) { Tuple description; if (descriptions.TryGetValue(steamItem.ClassID, out description)) { steamItem.RealAppID = description.Item1; @@ -476,7 +472,7 @@ namespace ArchiSteamFarm { using (dynamic iEconService = WebAPI.GetInterface("IEconService", Bot.BotConfig.SteamApiKey)) { iEconService.Timeout = Timeout; - for (byte i = 0; i < WebBrowser.MaxRetries && response == null; i++) { + for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++) { try { response = iEconService.DeclineTradeOffer( tradeofferid: tradeID.ToString(), @@ -489,24 +485,24 @@ namespace ArchiSteamFarm { } } - if (response == null) { - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; + if (response != null) { + return true; } - return true; + Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); + return false; } - internal async Task> GetMyTradableInventory() { + internal async Task> GetMyInventory(bool tradable) { if (!await RefreshSessionIfNeeded().ConfigureAwait(false)) { return null; } HashSet result = new HashSet(); - ushort nextPage = 0; + uint currentPage = 0; while (true) { - string request = SteamCommunityURL + "/my/inventory/json/" + Steam.Item.SteamAppID + "/" + Steam.Item.SteamContextID + "?trading=1&start=" + nextPage; + string request = SteamCommunityURL + "/my/inventory/json/" + Steam.Item.SteamAppID + "/" + Steam.Item.SteamContextID + "?trading=" + (tradable ? "1" : "0") + "&start=" + currentPage; JObject jObject = await WebBrowser.UrlGetToJObjectRetry(request).ConfigureAwait(false); if (jObject == null) { @@ -537,13 +533,14 @@ namespace ArchiSteamFarm { } uint appID = 0; - Steam.Item.EType type = Steam.Item.EType.Unknown; string hashName = description["market_hash_name"].ToString(); if (!string.IsNullOrEmpty(hashName)) { appID = GetAppIDFromMarketHashName(hashName); } + Steam.Item.EType type = Steam.Item.EType.Unknown; + string descriptionType = description["type"].ToString(); if (!string.IsNullOrEmpty(descriptionType)) { type = GetItemType(descriptionType); @@ -559,11 +556,10 @@ namespace ArchiSteamFarm { } foreach (JToken item in items) { - Steam.Item steamItem; try { - steamItem = JsonConvert.DeserializeObject(item.ToString()); + steamItem = item.ToObject(); } catch (JsonException e) { Logging.LogGenericException(e, Bot.BotName); continue; @@ -588,12 +584,17 @@ namespace ArchiSteamFarm { break; // OK, last page } - if (ushort.TryParse(jObject["more_start"].ToString(), out nextPage)) { - continue; + uint nextPage; + if (!uint.TryParse(jObject["more_start"].ToString(), out nextPage)) { + Logging.LogNullError(nameof(nextPage), Bot.BotName); + break; } - Logging.LogNullError(nameof(nextPage), Bot.BotName); - break; + if (nextPage <= currentPage) { + break; + } + + currentPage = nextPage; } return result; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index ef672f3d0..10b56a169 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -658,7 +658,7 @@ namespace ArchiSteamFarm { } await Trading.LimitInventoryRequestsAsync().ConfigureAwait(false); - HashSet inventory = await ArchiWebHandler.GetMyTradableInventory().ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetMyInventory(true).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return "Nothing to send, inventory seems empty!"; diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 639659c39..181ccfd50 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -153,7 +153,7 @@ namespace ArchiSteamFarm { // At this point we're sure that STM trade is valid // Now check if it's worth for us to do the trade - HashSet inventory = await Bot.ArchiWebHandler.GetMyTradableInventory().ConfigureAwait(false); + HashSet inventory = await Bot.ArchiWebHandler.GetMyInventory(false).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return true; // OK, assume that this trade is valid, we can't check our EQ }