From 035b70458e1a03dcd7f8301936c7006b37f63232 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 17 Sep 2017 18:01:17 +0200 Subject: [PATCH] Rewrite ASF inventory fetching into new endpoint, #646 --- ArchiSteamFarm.Tests/Trading.cs | 134 ++++++------ ArchiSteamFarm/ArchiWebHandler.cs | 153 +++++--------- ArchiSteamFarm/Bot.cs | 24 +-- ArchiSteamFarm/BotConfig.cs | 12 +- ArchiSteamFarm/JSON/Steam.cs | 341 ++++++++++++++++++------------ ArchiSteamFarm/Statistics.cs | 2 +- ArchiSteamFarm/Trading.cs | 18 +- ArchiSteamFarm/WebBrowser.cs | 44 ---- 8 files changed, 352 insertions(+), 376 deletions(-) diff --git a/ArchiSteamFarm.Tests/Trading.cs b/ArchiSteamFarm.Tests/Trading.cs index ea5f46b27..8784a5617 100644 --- a/ArchiSteamFarm.Tests/Trading.cs +++ b/ArchiSteamFarm.Tests/Trading.cs @@ -33,140 +33,140 @@ namespace ArchiSteamFarm.Tests { public sealed class Trading { [TestMethod] public void TradingMultiGameBadReject() { - Steam.Item item1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Game1X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 9, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Game1X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 9, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item1Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 1, 730, Steam.Item.EType.TradingCard); - Steam.Item item2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 4, 1, 730, Steam.Item.EType.TradingCard); + Steam.Asset item1Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 1, 730, Steam.Asset.EType.TradingCard); + Steam.Asset item2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 4, 1, 730, Steam.Asset.EType.TradingCard); - HashSet inventory = new HashSet { item1Game1X9, item1Game2, item2Game2 }; - HashSet itemsToGive = new HashSet { item1Game1, item1Game2 }; - HashSet itemsToReceive = new HashSet { item2Game1, item2Game2 }; + HashSet inventory = new HashSet { item1Game1X9, item1Game2, item2Game2 }; + HashSet itemsToGive = new HashSet { item1Game1, item1Game2 }; + HashSet itemsToReceive = new HashSet { item2Game1, item2Game2 }; Assert.IsFalse(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingMultiGameMultiTypeBadReject() { - Steam.Item item1Type1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Type1Game1X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 9, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Type1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Type1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Type1Game1X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 9, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Type1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item3Type2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 1, 730, Steam.Item.EType.Emoticon); - Steam.Item item3Type2Game2X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 9, 730, Steam.Item.EType.Emoticon); - Steam.Item item4Type2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 4, 1, 730, Steam.Item.EType.Emoticon); + Steam.Asset item3Type2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 1, 730, Steam.Asset.EType.Emoticon); + Steam.Asset item3Type2Game2X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 9, 730, Steam.Asset.EType.Emoticon); + Steam.Asset item4Type2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 4, 1, 730, Steam.Asset.EType.Emoticon); - HashSet inventory = new HashSet { item1Type1Game1X9, item3Type2Game2X9, item4Type2Game2 }; - HashSet itemsToGive = new HashSet { item1Type1Game1, item4Type2Game2 }; - HashSet itemsToReceive = new HashSet { item2Type1Game1, item3Type2Game2 }; + HashSet inventory = new HashSet { item1Type1Game1X9, item3Type2Game2X9, item4Type2Game2 }; + HashSet itemsToGive = new HashSet { item1Type1Game1, item4Type2Game2 }; + HashSet itemsToReceive = new HashSet { item2Type1Game1, item3Type2Game2 }; Assert.IsFalse(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingMultiGameMultiTypeNeutralAccept() { - Steam.Item item1Type1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Type1Game1X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 9, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Type1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Type1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Type1Game1X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 9, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Type1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item3Type2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 1, 730, Steam.Item.EType.Emoticon); - Steam.Item item4Type2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 4, 1, 730, Steam.Item.EType.Emoticon); + Steam.Asset item3Type2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 1, 730, Steam.Asset.EType.Emoticon); + Steam.Asset item4Type2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 4, 1, 730, Steam.Asset.EType.Emoticon); - HashSet inventory = new HashSet { item1Type1Game1X9, item3Type2Game2 }; - HashSet itemsToGive = new HashSet { item1Type1Game1, item3Type2Game2 }; - HashSet itemsToReceive = new HashSet { item2Type1Game1, item4Type2Game2 }; + HashSet inventory = new HashSet { item1Type1Game1X9, item3Type2Game2 }; + HashSet itemsToGive = new HashSet { item1Type1Game1, item3Type2Game2 }; + HashSet itemsToReceive = new HashSet { item2Type1Game1, item4Type2Game2 }; Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingMultiGameNeutralAccept() { - Steam.Item item1Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Game1X2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 2, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Game1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Game1X2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 2, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Game1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item1Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 730, Steam.Item.EType.TradingCard); - Steam.Item item2Game2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 730, Steam.Item.EType.TradingCard); + Steam.Asset item1Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 730, Steam.Asset.EType.TradingCard); + Steam.Asset item2Game2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 730, Steam.Asset.EType.TradingCard); - HashSet inventory = new HashSet { item1Game1X2, item1Game2 }; - HashSet itemsToGive = new HashSet { item1Game1, item1Game2 }; - HashSet itemsToReceive = new HashSet { item2Game1, item2Game2 }; + HashSet inventory = new HashSet { item1Game1X2, item1Game2 }; + HashSet itemsToGive = new HashSet { item1Game1, item1Game2 }; + HashSet itemsToReceive = new HashSet { item2Game1, item2Game2 }; Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingSingleGameBadReject() { - Steam.Item item1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - HashSet inventory = new HashSet { item1, item2 }; - HashSet itemsToGive = new HashSet { item1 }; - HashSet itemsToReceive = new HashSet { item2 }; + HashSet inventory = new HashSet { item1, item2 }; + HashSet itemsToGive = new HashSet { item1 }; + HashSet itemsToReceive = new HashSet { item2 }; Assert.IsFalse(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingSingleGameGoodAccept() { - Steam.Item item1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1X2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 2, 570, Steam.Item.EType.TradingCard); - Steam.Item item2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1X2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 2, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - HashSet inventory = new HashSet { item1X2 }; - HashSet itemsToGive = new HashSet { item1 }; - HashSet itemsToReceive = new HashSet { item2 }; + HashSet inventory = new HashSet { item1X2 }; + HashSet itemsToGive = new HashSet { item1 }; + HashSet itemsToReceive = new HashSet { item2 }; Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingSingleGameMultiTypeBadReject() { - Steam.Item item1Type1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Type1X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 9, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Type1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Type1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Type1X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 9, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Type1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item3Type2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 1, 570, Steam.Item.EType.Emoticon); - Steam.Item item3Type2X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 9, 570, Steam.Item.EType.Emoticon); - Steam.Item item4Type2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 4, 1, 570, Steam.Item.EType.Emoticon); + Steam.Asset item3Type2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 1, 570, Steam.Asset.EType.Emoticon); + Steam.Asset item3Type2X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 9, 570, Steam.Asset.EType.Emoticon); + Steam.Asset item4Type2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 4, 1, 570, Steam.Asset.EType.Emoticon); - HashSet inventory = new HashSet { item1Type1X9, item3Type2X9, item4Type2 }; - HashSet itemsToGive = new HashSet { item1Type1, item4Type2 }; - HashSet itemsToReceive = new HashSet { item2Type1, item3Type2 }; + HashSet inventory = new HashSet { item1Type1X9, item3Type2X9, item4Type2 }; + HashSet itemsToGive = new HashSet { item1Type1, item4Type2 }; + HashSet itemsToReceive = new HashSet { item2Type1, item3Type2 }; Assert.IsFalse(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingSingleGameMultiTypeNeutralAccept() { - Steam.Item item1Type1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item1Type1X9 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 9, 570, Steam.Item.EType.TradingCard); - Steam.Item item2Type1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1Type1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item1Type1X9 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 9, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2Type1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - Steam.Item item3Type2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 3, 1, 570, Steam.Item.EType.Emoticon); - Steam.Item item4Type2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 4, 1, 570, Steam.Item.EType.Emoticon); + Steam.Asset item3Type2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 3, 1, 570, Steam.Asset.EType.Emoticon); + Steam.Asset item4Type2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 4, 1, 570, Steam.Asset.EType.Emoticon); - HashSet inventory = new HashSet { item1Type1X9, item3Type2 }; - HashSet itemsToGive = new HashSet { item1Type1, item3Type2 }; - HashSet itemsToReceive = new HashSet { item2Type1, item4Type2 }; + HashSet inventory = new HashSet { item1Type1X9, item3Type2 }; + HashSet itemsToGive = new HashSet { item1Type1, item3Type2 }; + HashSet itemsToReceive = new HashSet { item2Type1, item4Type2 }; Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } [TestMethod] public void TradingSingleGameNeutralAccept() { - Steam.Item item1 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 1, 1, 570, Steam.Item.EType.TradingCard); - Steam.Item item2 = new Steam.Item(Steam.Item.SteamAppID, Steam.Item.SteamCommunityContextID, 2, 1, 570, Steam.Item.EType.TradingCard); + Steam.Asset item1 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 1, 1, 570, Steam.Asset.EType.TradingCard); + Steam.Asset item2 = new Steam.Asset(Steam.Asset.SteamAppID, Steam.Asset.SteamCommunityContextID, 2, 1, 570, Steam.Asset.EType.TradingCard); - HashSet inventory = new HashSet { item1 }; - HashSet itemsToGive = new HashSet { item1 }; - HashSet itemsToReceive = new HashSet { item2 }; + HashSet inventory = new HashSet { item1 }; + HashSet itemsToGive = new HashSet { item1 }; + HashSet itemsToReceive = new HashSet { item2 }; Assert.IsTrue(AcceptsTrade(inventory, itemsToGive, itemsToReceive)); } - private static bool AcceptsTrade(HashSet inventory, HashSet itemsToGive, HashSet itemsToReceive) { + private static bool AcceptsTrade(HashSet inventory, HashSet itemsToGive, HashSet itemsToReceive) { Type trading = typeof(ArchiSteamFarm.Trading); MethodInfo method = trading.GetMethod("IsTradeNeutralOrBetter", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); return (bool) method.Invoke(null, new object[] { inventory, itemsToGive, itemsToReceive }); diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 27ba15ae5..924a878e8 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -251,7 +251,7 @@ namespace ArchiSteamFarm { return null; } - Dictionary descriptions = new Dictionary(); + Dictionary descriptions = new Dictionary(); foreach (KeyValue description in response["descriptions"].Children) { ulong classID = description["classid"].AsUnsignedLong(); if (classID == 0) { @@ -274,7 +274,7 @@ namespace ArchiSteamFarm { appID = description["appid"].AsUnsignedInteger(); } - Steam.Item.EType type = Steam.Item.EType.Unknown; + Steam.Asset.EType type = Steam.Asset.EType.Unknown; string descriptionType = description["type"].Value; if (!string.IsNullOrEmpty(descriptionType)) { @@ -477,125 +477,84 @@ namespace ArchiSteamFarm { } [SuppressMessage("ReSharper", "FunctionComplexityOverflow")] - internal async Task> GetMySteamInventory(bool trading, HashSet wantedTypes, HashSet wantedRealAppIDs = null) { - if ((wantedTypes == null) || (wantedTypes.Count == 0)) { - Bot.ArchiLogger.LogNullError(nameof(wantedTypes)); - return null; - } - + internal async Task> GetMySteamInventory(bool tradableOnly = false, HashSet wantedTypes = null, HashSet wantedRealAppIDs = null) { if (!await RefreshSessionIfNeeded().ConfigureAwait(false)) { return null; } - HashSet result = new HashSet(); + HashSet result = new HashSet(); - string request = SteamCommunityURL + "/my/inventory/json/" + Steam.Item.SteamAppID + "/" + Steam.Item.SteamCommunityContextID + "?l=english" + (trading ? "&trading=1" : "") + "&start="; - uint currentPage = 0; + // 5000 is maximum allowed count per single request + string request = SteamCommunityURL + "/inventory/" + SteamID + "/" + Steam.Asset.SteamAppID + "/" + Steam.Asset.SteamCommunityContextID + "?l=english&count=5000"; + ulong startAssetID = 0; await InventorySemaphore.WaitAsync().ConfigureAwait(false); try { while (true) { - JObject jObject = await WebBrowser.UrlGetToJObjectRetry(request + currentPage).ConfigureAwait(false); + Steam.InventoryResponse response = await WebBrowser.UrlGetToJsonResultRetry(request + (startAssetID > 0 ? "&start_assetid=" + startAssetID : "")).ConfigureAwait(false); - IEnumerable descriptions = jObject?.SelectTokens("$.rgDescriptions.*"); - if (descriptions == null) { - return null; // OK, empty inventory + if (response == null) { + return null; } - Dictionary descriptionMap = new Dictionary(); - foreach (JToken description in descriptions.Where(description => description != null)) { - string classIDString = description["classid"]?.ToString(); - if (string.IsNullOrEmpty(classIDString)) { - Bot.ArchiLogger.LogNullError(nameof(classIDString)); - continue; + 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)); + return null; } - if (!ulong.TryParse(classIDString, out ulong classID) || (classID == 0)) { - Bot.ArchiLogger.LogNullError(nameof(classID)); - continue; - } - - if (descriptionMap.ContainsKey(classID)) { + if (descriptionMap.ContainsKey(description.ClassID)) { continue; } uint appID = 0; - string hashName = description["market_hash_name"]?.ToString(); - if (!string.IsNullOrEmpty(hashName)) { - appID = GetAppIDFromMarketHashName(hashName); + if (!string.IsNullOrEmpty(description.MarketHashName)) { + appID = GetAppIDFromMarketHashName(description.MarketHashName); } if (appID == 0) { - string appIDString = description["appid"]?.ToString(); - if (string.IsNullOrEmpty(appIDString)) { - Bot.ArchiLogger.LogNullError(nameof(appIDString)); + appID = description.AppID; + } + + Steam.Asset.EType type = Steam.Asset.EType.Unknown; + + if (!string.IsNullOrEmpty(description.Type)) { + type = GetItemType(description.Type); + } + + descriptionMap[description.ClassID] = (appID, type, description.Tradable); + } + + foreach (Steam.Asset asset in response.Assets.Where(asset => asset != null)) { + if (descriptionMap.TryGetValue(asset.ClassID, out (uint AppID, Steam.Asset.EType Type, bool Tradable) description)) { + if (tradableOnly && !description.Tradable) { continue; } - if (!uint.TryParse(appIDString, out appID) || (appID == 0)) { - Bot.ArchiLogger.LogNullError(nameof(appID)); - continue; - } + asset.RealAppID = description.AppID; + asset.Type = description.Type; } - Steam.Item.EType type = Steam.Item.EType.Unknown; - - string descriptionType = description["type"]?.ToString(); - if (!string.IsNullOrEmpty(descriptionType)) { - type = GetItemType(descriptionType); - } - - descriptionMap[classID] = (appID, type); - } - - IEnumerable items = jObject.SelectTokens("$.rgInventory.*"); - if (items == null) { - Bot.ArchiLogger.LogNullError(nameof(items)); - return null; - } - - foreach (JToken item in items.Where(item => item != null)) { - Steam.Item steamItem; - - try { - steamItem = item.ToObject(); - } catch (JsonException e) { - Bot.ArchiLogger.LogGenericException(e); - return null; - } - - if (steamItem == null) { - Bot.ArchiLogger.LogNullError(nameof(steamItem)); - return null; - } - - steamItem.AppID = Steam.Item.SteamAppID; - steamItem.ContextID = Steam.Item.SteamCommunityContextID; - - if (descriptionMap.TryGetValue(steamItem.ClassID, out (uint AppID, Steam.Item.EType Type) description)) { - steamItem.RealAppID = description.AppID; - steamItem.Type = description.Type; - } - - if (!wantedTypes.Contains(steamItem.Type) || (wantedRealAppIDs?.Contains(steamItem.RealAppID) == false)) { + if ((wantedTypes?.Contains(asset.Type) == false) || (wantedRealAppIDs?.Contains(asset.AppID) == false)) { continue; } - result.Add(steamItem); + result.Add(asset); } - if (!bool.TryParse(jObject["more"]?.ToString(), out bool more) || !more) { + if (!response.MoreItems) { break; // OK, last page } - if (!uint.TryParse(jObject["more_start"]?.ToString(), out uint nextPage) || (nextPage <= currentPage)) { - Bot.ArchiLogger.LogNullError(nameof(nextPage)); + if (response.LastAssetID == 0) { + Bot.ArchiLogger.LogNullError(nameof(response.LastAssetID)); return null; } - currentPage = nextPage; + startAssetID = response.LastAssetID; } return result; @@ -1030,7 +989,7 @@ namespace ArchiSteamFarm { return (response.Result, response.PurchaseResultDetail); } - internal async Task SendTradeOffer(HashSet inventory, ulong partnerID, string token = null) { + internal async Task SendTradeOffer(HashSet inventory, ulong partnerID, string token = null) { if ((inventory == null) || (inventory.Count == 0) || (partnerID == 0)) { Bot.ArchiLogger.LogNullError(nameof(inventory) + " || " + nameof(inventory.Count) + " || " + nameof(partnerID)); return false; @@ -1049,7 +1008,7 @@ namespace ArchiSteamFarm { Steam.TradeOfferRequest singleTrade = new Steam.TradeOfferRequest(); HashSet trades = new HashSet { singleTrade }; - foreach (Steam.Item item in inventory) { + foreach (Steam.Asset item in inventory) { if (singleTrade.ItemsToGive.Assets.Count >= Trading.MaxItemsPerTrade) { if (trades.Count >= Trading.MaxTradesPerAccount) { break; @@ -1255,31 +1214,31 @@ namespace ArchiSteamFarm { return uint.TryParse(hashName.Substring(0, index), out uint appID) ? appID : 0; } - private static Steam.Item.EType GetItemType(string name) { + private static Steam.Asset.EType GetItemType(string name) { if (string.IsNullOrEmpty(name)) { ASF.ArchiLogger.LogNullError(nameof(name)); - return Steam.Item.EType.Unknown; + return Steam.Asset.EType.Unknown; } switch (name) { case "Booster Pack": - return Steam.Item.EType.BoosterPack; + return Steam.Asset.EType.BoosterPack; case "Steam Gems": - return Steam.Item.EType.SteamGems; + return Steam.Asset.EType.SteamGems; default: if (name.EndsWith("Emoticon", StringComparison.Ordinal)) { - return Steam.Item.EType.Emoticon; + return Steam.Asset.EType.Emoticon; } if (name.EndsWith("Foil Trading Card", StringComparison.Ordinal)) { - return Steam.Item.EType.FoilTradingCard; + return Steam.Asset.EType.FoilTradingCard; } if (name.EndsWith("Profile Background", StringComparison.Ordinal)) { - return Steam.Item.EType.ProfileBackground; + return Steam.Asset.EType.ProfileBackground; } - return name.EndsWith("Trading Card", StringComparison.Ordinal) ? Steam.Item.EType.TradingCard : Steam.Item.EType.Unknown; + return name.EndsWith("Trading Card", StringComparison.Ordinal) ? Steam.Asset.EType.TradingCard : Steam.Asset.EType.Unknown; } } @@ -1311,7 +1270,7 @@ namespace ArchiSteamFarm { return !uri?.AbsolutePath.StartsWith("/login", StringComparison.Ordinal); } - private static bool ParseItems(Dictionary descriptions, List input, HashSet output) { + private static bool ParseItems(Dictionary descriptions, List input, HashSet output) { if ((descriptions == null) || (input == null) || (input.Count == 0) || (output == null)) { ASF.ArchiLogger.LogNullError(nameof(descriptions) + " || " + nameof(input) + " || " + nameof(output)); return false; @@ -1343,15 +1302,15 @@ namespace ArchiSteamFarm { } uint realAppID = appID; - Steam.Item.EType type = Steam.Item.EType.Unknown; + Steam.Asset.EType type = Steam.Asset.EType.Unknown; - if (descriptions.TryGetValue(classID, out (uint AppID, Steam.Item.EType Type) description)) { + if (descriptions.TryGetValue(classID, out (uint AppID, Steam.Asset.EType Type) description)) { realAppID = description.AppID; type = description.Type; } - Steam.Item steamItem = new Steam.Item(appID, contextID, classID, amount, realAppID, type); - output.Add(steamItem); + Steam.Asset steamAsset = new Steam.Asset(appID, contextID, classID, amount, realAppID, type); + output.Add(steamAsset); } return true; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 469a01b8f..1f049efe7 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -2973,7 +2973,7 @@ namespace ArchiSteamFarm { } try { - HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, BotConfig.LootableTypes).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, BotConfig.LootableTypes).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -4031,45 +4031,45 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotLootingYourself); } - HashSet transferTypes = new HashSet(); + HashSet transferTypes = new HashSet(); string[] modes = mode.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string singleMode in modes) { switch (singleMode.ToUpper()) { case "A": case "ALL": - foreach (Steam.Item.EType type in Enum.GetValues(typeof(Steam.Item.EType))) { + foreach (Steam.Asset.EType type in Enum.GetValues(typeof(Steam.Asset.EType))) { transferTypes.Add(type); } break; case "BG": case "BACKGROUND": - transferTypes.Add(Steam.Item.EType.ProfileBackground); + transferTypes.Add(Steam.Asset.EType.ProfileBackground); break; case "BO": case "BOOSTER": - transferTypes.Add(Steam.Item.EType.BoosterPack); + transferTypes.Add(Steam.Asset.EType.BoosterPack); break; case "C": case "CARD": - transferTypes.Add(Steam.Item.EType.TradingCard); + transferTypes.Add(Steam.Asset.EType.TradingCard); break; case "E": case "EMOTICON": - transferTypes.Add(Steam.Item.EType.Emoticon); + transferTypes.Add(Steam.Asset.EType.Emoticon); break; case "F": case "FOIL": - transferTypes.Add(Steam.Item.EType.FoilTradingCard); + transferTypes.Add(Steam.Asset.EType.FoilTradingCard); break; case "G": case "GEMS": - transferTypes.Add(Steam.Item.EType.SteamGems); + transferTypes.Add(Steam.Asset.EType.SteamGems); break; case "U": case "UNKNOWN": - transferTypes.Add(Steam.Item.EType.Unknown); + transferTypes.Add(Steam.Asset.EType.Unknown); break; default: return FormatBotResponse(string.Format(Strings.ErrorIsInvalid, mode)); @@ -4081,7 +4081,7 @@ namespace ArchiSteamFarm { } try { - HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, transferTypes).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, transferTypes).ConfigureAwait(false); if ((inventory == null) || (inventory.Count == 0)) { return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); } @@ -4171,7 +4171,7 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotNotConnected); } - HashSet inventory = await ArchiWebHandler.GetMySteamInventory(false, new HashSet { Steam.Item.EType.BoosterPack }).ConfigureAwait(false); + HashSet inventory = await ArchiWebHandler.GetMySteamInventory(false, 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/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index bc776fb5c..7a8bc1760 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -96,15 +96,15 @@ namespace ArchiSteamFarm { #pragma warning restore 649 [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)] - internal readonly HashSet LootableTypes = new HashSet { - Steam.Item.EType.BoosterPack, - Steam.Item.EType.FoilTradingCard, - Steam.Item.EType.TradingCard + internal readonly HashSet LootableTypes = new HashSet { + Steam.Asset.EType.BoosterPack, + Steam.Asset.EType.FoilTradingCard, + Steam.Asset.EType.TradingCard }; [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)] - internal readonly HashSet MatchableTypes = new HashSet { - Steam.Item.EType.TradingCard + internal readonly HashSet MatchableTypes = new HashSet { + Steam.Asset.EType.TradingCard }; [JsonProperty(Required = Required.DisallowNull)] diff --git a/ArchiSteamFarm/JSON/Steam.cs b/ArchiSteamFarm/JSON/Steam.cs index e700800b7..7adac4b99 100644 --- a/ArchiSteamFarm/JSON/Steam.cs +++ b/ArchiSteamFarm/JSON/Steam.cs @@ -32,6 +32,136 @@ using SteamKit2; namespace ArchiSteamFarm.JSON { internal static class Steam { + // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_Asset + internal sealed class Asset { + internal const ushort SteamAppID = 753; + internal const byte SteamCommunityContextID = 6; + + internal uint Amount { get; private set; } + + [JsonProperty(PropertyName = "appid", Required = Required.DisallowNull)] + [SuppressMessage("ReSharper", "UnusedMember.Local")] + internal uint AppID { get; private set; } + + internal ulong AssetID { get; private set; } + internal ulong ClassID { get; private set; } + internal ulong ContextID { get; private set; } + internal uint RealAppID { get; set; } + internal EType Type { get; set; } + + [JsonProperty(PropertyName = "amount", Required = Required.Always)] + [SuppressMessage("ReSharper", "UnusedMember.Local")] + private string AmountString { + get => Amount.ToString(); + + set { + if (string.IsNullOrEmpty(value)) { + ASF.ArchiLogger.LogNullError(nameof(value)); + return; + } + + if (!uint.TryParse(value, out uint amount) || (amount == 0)) { + ASF.ArchiLogger.LogNullError(nameof(amount)); + return; + } + + Amount = amount; + } + } + + [JsonProperty(PropertyName = "assetid", Required = Required.DisallowNull)] + private string AssetIDString { + get => AssetID.ToString(); + + set { + if (string.IsNullOrEmpty(value)) { + ASF.ArchiLogger.LogNullError(nameof(value)); + return; + } + + if (!ulong.TryParse(value, out ulong assetID) || (assetID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(assetID)); + return; + } + + AssetID = assetID; + } + } + + [JsonProperty(PropertyName = "classid", Required = Required.DisallowNull)] + [SuppressMessage("ReSharper", "UnusedMember.Local")] + private string ClassIDString { + get => ClassID.ToString(); + + set { + if (string.IsNullOrEmpty(value)) { + ASF.ArchiLogger.LogNullError(nameof(value)); + return; + } + + if (!ulong.TryParse(value, out ulong classID) || (classID == 0)) { + return; + } + + ClassID = classID; + } + } + + [JsonProperty(PropertyName = "contextid", Required = Required.DisallowNull)] + [SuppressMessage("ReSharper", "UnusedMember.Local")] + private string ContextIDString { + get => ContextID.ToString(); + + set { + if (string.IsNullOrEmpty(value)) { + ASF.ArchiLogger.LogNullError(nameof(value)); + return; + } + + if (!ulong.TryParse(value, out ulong contextID) || (contextID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(contextID)); + return; + } + + ContextID = contextID; + } + } + + [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)] + [SuppressMessage("ReSharper", "UnusedMember.Local")] + private string ID { + get => AssetIDString; + set => AssetIDString = value; + } + + // Constructed from trades being received + internal Asset(uint appID, ulong contextID, ulong classID, uint amount, uint realAppID, EType type = EType.Unknown) { + if ((appID == 0) || (contextID == 0) || (classID == 0) || (amount == 0) || (realAppID == 0)) { + throw new ArgumentNullException(nameof(classID) + " || " + nameof(contextID) + " || " + nameof(classID) + " || " + nameof(amount) + " || " + nameof(realAppID)); + } + + AppID = appID; + ContextID = contextID; + ClassID = classID; + Amount = amount; + RealAppID = realAppID; + Type = type; + } + + // Deserialized from JSON + private Asset() { } + + internal enum EType : byte { + Unknown, + BoosterPack, + Emoticon, + FoilTradingCard, + ProfileBackground, + TradingCard, + SteamGems + } + } + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] internal sealed class ConfirmationDetails { @@ -232,149 +362,80 @@ namespace ArchiSteamFarm.JSON { private GenericResponse() { } } - // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_Asset - internal sealed class Item { - internal const ushort SteamAppID = 753; - internal const byte SteamCommunityContextID = 6; + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + internal sealed class InventoryResponse { + [JsonProperty(PropertyName = "assets", Required = Required.Always)] + internal readonly HashSet Assets; - internal uint Amount { get; private set; } - internal uint AppID { get; set; } - internal ulong AssetID { get; private set; } - internal ulong ClassID { get; private set; } - internal ulong ContextID { get; set; } - internal uint RealAppID { get; set; } - internal EType Type { get; set; } + [JsonProperty(PropertyName = "descriptions", Required = Required.Always)] + internal readonly HashSet Descriptions; - [JsonProperty(PropertyName = "amount", Required = Required.Always)] - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private string AmountString { - get => Amount.ToString(); + internal ulong LastAssetID { get; private set; } + internal bool MoreItems { get; private set; } + [JsonProperty(PropertyName = "last_assetid", Required = Required.DisallowNull)] + private string LastAssetIDString { set { if (string.IsNullOrEmpty(value)) { ASF.ArchiLogger.LogNullError(nameof(value)); return; } - if (!uint.TryParse(value, out uint amount) || (amount == 0)) { - ASF.ArchiLogger.LogNullError(nameof(amount)); + if (!ulong.TryParse(value, out ulong lastAssetID) || (lastAssetID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(lastAssetID)); return; } - Amount = amount; + LastAssetID = lastAssetID; } } - [JsonProperty(PropertyName = "appid", Required = Required.DisallowNull)] - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private string AppIDString { - get => AppID.ToString(); - - set { - if (string.IsNullOrEmpty(value)) { - ASF.ArchiLogger.LogNullError(nameof(value)); - return; - } - - if (!uint.TryParse(value, out uint appID) || (appID == 0)) { - ASF.ArchiLogger.LogNullError(nameof(appID)); - return; - } - - AppID = appID; - } - } - - [JsonProperty(PropertyName = "assetid", Required = Required.DisallowNull)] - private string AssetIDString { - get => AssetID.ToString(); - - set { - if (string.IsNullOrEmpty(value)) { - ASF.ArchiLogger.LogNullError(nameof(value)); - return; - } - - if (!ulong.TryParse(value, out ulong assetID) || (assetID == 0)) { - ASF.ArchiLogger.LogNullError(nameof(assetID)); - return; - } - - AssetID = assetID; - } - } - - [JsonProperty(PropertyName = "classid", Required = Required.DisallowNull)] - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private string ClassIDString { - get => ClassID.ToString(); - - set { - if (string.IsNullOrEmpty(value)) { - ASF.ArchiLogger.LogNullError(nameof(value)); - return; - } - - if (!ulong.TryParse(value, out ulong classID) || (classID == 0)) { - return; - } - - ClassID = classID; - } - } - - [JsonProperty(PropertyName = "contextid", Required = Required.DisallowNull)] - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private string ContextIDString { - get => ContextID.ToString(); - - set { - if (string.IsNullOrEmpty(value)) { - ASF.ArchiLogger.LogNullError(nameof(value)); - return; - } - - if (!ulong.TryParse(value, out ulong contextID) || (contextID == 0)) { - ASF.ArchiLogger.LogNullError(nameof(contextID)); - return; - } - - ContextID = contextID; - } - } - - [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)] - [SuppressMessage("ReSharper", "UnusedMember.Local")] - private string ID { - get => AssetIDString; - set => AssetIDString = value; - } - - // Constructed from trades being received - internal Item(uint appID, ulong contextID, ulong classID, uint amount, uint realAppID, EType type = EType.Unknown) { - if ((appID == 0) || (contextID == 0) || (classID == 0) || (amount == 0) || (realAppID == 0)) { - throw new ArgumentNullException(nameof(classID) + " || " + nameof(contextID) + " || " + nameof(classID) + " || " + nameof(amount) + " || " + nameof(realAppID)); - } - - AppID = appID; - ContextID = contextID; - ClassID = classID; - Amount = amount; - RealAppID = realAppID; - Type = type; + [JsonProperty(PropertyName = "more_items", Required = Required.DisallowNull)] + private byte MoreItemsNumber { + set => MoreItems = value > 0; } // Deserialized from JSON - private Item() { } + private InventoryResponse() { } - internal enum EType : byte { - Unknown, - BoosterPack, - Emoticon, - FoilTradingCard, - ProfileBackground, - TradingCard, - SteamGems + internal sealed class Description { + [JsonProperty(PropertyName = "appid", Required = Required.Always)] + internal readonly uint AppID; + + [JsonProperty(PropertyName = "market_hash_name", Required = Required.Always)] + internal readonly string MarketHashName; + + [JsonProperty(PropertyName = "type", Required = Required.Always)] + internal readonly string Type; + + internal ulong ClassID { get; private set; } + + internal bool Tradable { get; private set; } + + [JsonProperty(PropertyName = "classid", Required = Required.Always)] + private string ClassIDString { + set { + if (string.IsNullOrEmpty(value)) { + ASF.ArchiLogger.LogNullError(nameof(value)); + return; + } + + if (!ulong.TryParse(value, out ulong classID) || (classID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(classID)); + return; + } + + ClassID = classID; + } + } + + [JsonProperty(PropertyName = "tradable", Required = Required.Always)] + private byte TradableNumber { + set => Tradable = value > 0; + } + + // Deserialized from JSON + private Description() { } } } @@ -409,8 +470,8 @@ namespace ArchiSteamFarm.JSON { // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_TradeOffer internal sealed class TradeOffer { - internal readonly HashSet ItemsToGive = new HashSet(); - internal readonly HashSet ItemsToReceive = new HashSet(); + internal readonly HashSet ItemsToGive = new HashSet(); + internal readonly HashSet ItemsToReceive = new HashSet(); internal readonly ETradeOfferState State; internal readonly ulong TradeOfferID; @@ -446,10 +507,10 @@ namespace ArchiSteamFarm.JSON { } internal bool IsFairTypesExchange() { - Dictionary> itemsToGivePerGame = new Dictionary>(); - foreach (Item item in ItemsToGive) { - if (!itemsToGivePerGame.TryGetValue(item.RealAppID, out Dictionary itemsPerType)) { - itemsPerType = new Dictionary { [item.Type] = item.Amount }; + Dictionary> itemsToGivePerGame = new Dictionary>(); + foreach (Asset item in ItemsToGive) { + if (!itemsToGivePerGame.TryGetValue(item.RealAppID, out Dictionary itemsPerType)) { + itemsPerType = new Dictionary { [item.Type] = item.Amount }; itemsToGivePerGame[item.RealAppID] = itemsPerType; } else { if (itemsPerType.TryGetValue(item.Type, out uint amount)) { @@ -460,10 +521,10 @@ namespace ArchiSteamFarm.JSON { } } - Dictionary> itemsToReceivePerGame = new Dictionary>(); - foreach (Item item in ItemsToReceive) { - if (!itemsToReceivePerGame.TryGetValue(item.RealAppID, out Dictionary itemsPerType)) { - itemsPerType = new Dictionary { + Dictionary> itemsToReceivePerGame = new Dictionary>(); + foreach (Asset item in ItemsToReceive) { + if (!itemsToReceivePerGame.TryGetValue(item.RealAppID, out Dictionary itemsPerType)) { + itemsPerType = new Dictionary { { item.Type, item.Amount } }; @@ -478,12 +539,12 @@ namespace ArchiSteamFarm.JSON { } // Ensure that amount of items to give is at least amount of items to receive (per game and per type) - foreach (KeyValuePair> itemsPerGame in itemsToGivePerGame) { - if (!itemsToReceivePerGame.TryGetValue(itemsPerGame.Key, out Dictionary otherItemsPerType)) { + foreach (KeyValuePair> itemsPerGame in itemsToGivePerGame) { + if (!itemsToReceivePerGame.TryGetValue(itemsPerGame.Key, out Dictionary otherItemsPerType)) { return false; } - foreach (KeyValuePair itemsPerType in itemsPerGame.Value) { + foreach (KeyValuePair itemsPerType in itemsPerGame.Value) { if (!otherItemsPerType.TryGetValue(itemsPerType.Key, out uint otherAmount)) { return false; } @@ -497,13 +558,13 @@ namespace ArchiSteamFarm.JSON { return true; } - internal bool IsValidSteamItemsRequest(HashSet acceptedTypes) { + internal bool IsValidSteamItemsRequest(HashSet acceptedTypes) { if ((acceptedTypes == null) || (acceptedTypes.Count == 0)) { ASF.ArchiLogger.LogNullError(nameof(acceptedTypes)); return false; } - bool result = ItemsToGive.All(item => (item.AppID == Item.SteamAppID) && (item.ContextID == Item.SteamCommunityContextID) && acceptedTypes.Contains(item.Type)); + bool result = ItemsToGive.All(item => (item.AppID == Asset.SteamAppID) && (item.ContextID == Asset.SteamCommunityContextID) && acceptedTypes.Contains(item.Type)); return result; } @@ -534,7 +595,7 @@ namespace ArchiSteamFarm.JSON { internal sealed class ItemList { [JsonProperty(PropertyName = "assets", Required = Required.Always)] - internal readonly HashSet Assets = new HashSet(); + internal readonly HashSet Assets = new HashSet(); } } } diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index 0515e8187..a5db86190 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -121,7 +121,7 @@ namespace ArchiSteamFarm { return; } - HashSet inventory = await Bot.ArchiWebHandler.GetMySteamInventory(true, Bot.BotConfig.MatchableTypes).ConfigureAwait(false); + HashSet inventory = await Bot.ArchiWebHandler.GetMySteamInventory(true, Bot.BotConfig.MatchableTypes).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 fa8b7196e..256d264f9 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -71,7 +71,7 @@ namespace ArchiSteamFarm { } } - private static bool IsTradeNeutralOrBetter(HashSet inventory, HashSet itemsToGive, HashSet itemsToReceive) { + private static bool IsTradeNeutralOrBetter(HashSet inventory, HashSet itemsToGive, HashSet itemsToReceive) { if ((inventory == null) || (inventory.Count == 0) || (itemsToGive == null) || (itemsToGive.Count == 0) || (itemsToReceive == null) || (itemsToReceive.Count == 0)) { ASF.ArchiLogger.LogNullError(nameof(inventory) + " || " + nameof(itemsToGive) + " || " + nameof(itemsToReceive)); return false; @@ -80,7 +80,7 @@ namespace ArchiSteamFarm { // Now let's create a map which maps items to their amount in our EQ // This has to be done as we might have multiple items of given ClassID with multiple amounts Dictionary itemAmounts = new Dictionary(); - foreach (Steam.Item item in inventory) { + foreach (Steam.Asset item in inventory) { if (itemAmounts.TryGetValue(item.ClassID, out uint amount)) { itemAmounts[item.ClassID] = amount + item.Amount; } else { @@ -89,9 +89,9 @@ namespace ArchiSteamFarm { } // Calculate our value of items to give on per-game basis - Dictionary<(Steam.Item.EType Type, uint AppID), List> itemAmountToGivePerGame = new Dictionary<(Steam.Item.EType Type, uint AppID), List>(); + Dictionary<(Steam.Asset.EType Type, uint AppID), List> itemAmountToGivePerGame = new Dictionary<(Steam.Asset.EType Type, uint AppID), List>(); Dictionary itemAmountsToGive = new Dictionary(itemAmounts); - foreach (Steam.Item item in itemsToGive) { + foreach (Steam.Asset item in itemsToGive) { if (!itemAmountToGivePerGame.TryGetValue((item.Type, item.RealAppID), out List amountsToGive)) { amountsToGive = new List(); itemAmountToGivePerGame[(item.Type, item.RealAppID)] = amountsToGive; @@ -112,9 +112,9 @@ namespace ArchiSteamFarm { } // Calculate our value of items to receive on per-game basis - Dictionary<(Steam.Item.EType Type, uint AppID), List> itemAmountToReceivePerGame = new Dictionary<(Steam.Item.EType Type, uint AppID), List>(); + Dictionary<(Steam.Asset.EType Type, uint AppID), List> itemAmountToReceivePerGame = new Dictionary<(Steam.Asset.EType Type, uint AppID), List>(); Dictionary itemAmountsToReceive = new Dictionary(itemAmounts); - foreach (Steam.Item item in itemsToReceive) { + foreach (Steam.Asset item in itemsToReceive) { if (!itemAmountToReceivePerGame.TryGetValue((item.Type, item.RealAppID), out List amountsToReceive)) { amountsToReceive = new List(); itemAmountToReceivePerGame[(item.Type, item.RealAppID)] = amountsToReceive; @@ -315,15 +315,15 @@ namespace ArchiSteamFarm { // Get appIDs/types we're interested in HashSet appIDs = new HashSet(); - HashSet types = new HashSet(); + HashSet types = new HashSet(); - foreach (Steam.Item item in tradeOffer.ItemsToGive) { + foreach (Steam.Asset item in tradeOffer.ItemsToGive) { appIDs.Add(item.RealAppID); types.Add(item.Type); } // Now check if it's worth for us to do the trade - HashSet inventory = await Bot.ArchiWebHandler.GetMySteamInventory(false, types, appIDs).ConfigureAwait(false); + HashSet inventory = await Bot.ArchiWebHandler.GetMySteamInventory(false, types, 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))); diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index fff4670c3..9c9c1b463 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -31,7 +31,6 @@ using System.Xml; using ArchiSteamFarm.Localization; using HtmlAgilityPack; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace ArchiSteamFarm { internal sealed class WebBrowser : IDisposable { @@ -133,26 +132,6 @@ namespace ArchiSteamFarm { return null; } - internal async Task UrlGetToJObjectRetry(string request, string referer = null) { - if (string.IsNullOrEmpty(request)) { - ArchiLogger.LogNullError(nameof(request)); - return null; - } - - JObject result = null; - for (byte i = 0; (i < MaxTries) && (result == null); i++) { - result = await UrlGetToJObject(request, referer).ConfigureAwait(false); - } - - if (result != null) { - return result; - } - - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; - } - internal async Task UrlGetToJsonResultRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { ArchiLogger.LogNullError(nameof(request)); @@ -362,29 +341,6 @@ namespace ArchiSteamFarm { return !string.IsNullOrEmpty(content) ? StringToHtmlDocument(content) : null; } - private async Task UrlGetToJObject(string request, string referer = null) { - if (string.IsNullOrEmpty(request)) { - ArchiLogger.LogNullError(nameof(request)); - return null; - } - - string json = await UrlGetToContent(request, referer).ConfigureAwait(false); - if (string.IsNullOrEmpty(json)) { - return null; - } - - JObject jObject; - - try { - jObject = JObject.Parse(json); - } catch (JsonException e) { - ArchiLogger.LogGenericException(e); - return null; - } - - return jObject; - } - private async Task UrlGetToResponse(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { ArchiLogger.LogNullError(nameof(request));