From 895c69642d13b1a0679662cd203b4b0f4d34aaf4 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 15 Mar 2016 04:20:28 +0100 Subject: [PATCH] Add !owns, closes #159 --- ArchiSteamFarm/ArchiWebHandler.cs | 46 ++++++++++++++++++++++++ ArchiSteamFarm/Bot.cs | 58 +++++++++++++++++++++++++++++++ ArchiSteamFarm/WebBrowser.cs | 23 ++++++++++++ 3 files changed, 127 insertions(+) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index c4ad0d11f..fb56d0a11 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -31,6 +31,7 @@ using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using System.Xml; namespace ArchiSteamFarm { internal sealed class ArchiWebHandler { @@ -154,6 +155,51 @@ namespace ArchiSteamFarm { return false; } + internal async Task> GetOwnedGames() { + if (SteamID == 0) { + return null; + } + + string request = SteamCommunityURL + "/profiles/" + SteamID + "/games/?xml=1"; + + XmlDocument response = null; + for (byte i = 0; i < WebBrowser.MaxRetries && response == null; i++) { + response = await WebBrowser.UrlGetToXML(request, Cookie).ConfigureAwait(false); + } + + if (response == null) { + Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); + return null; + } + + XmlNodeList xmlNodeList = response.SelectNodes("gamesList/games/game"); + if (xmlNodeList == null || xmlNodeList.Count == 0) { + return null; + } + + Dictionary result = new Dictionary(xmlNodeList.Count); + foreach (XmlNode xmlNode in xmlNodeList) { + XmlNode appNode = xmlNode.SelectSingleNode("appID"); + if (appNode == null) { + continue; + } + + uint appID; + if (!uint.TryParse(appNode.InnerText, out appID)) { + continue; + } + + XmlNode nameNode = xmlNode.SelectSingleNode("name"); + if (nameNode == null) { + continue; + } + + result[appID] = nameNode.InnerText; + } + + return result; + } + internal List GetTradeOffers() { if (string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey)) { return null; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 553e81f53..f97714abd 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -361,6 +361,12 @@ namespace ArchiSteamFarm { return await ResponseFarm(args[1]).ConfigureAwait(false); case "!loot": return await ResponseSendTrade(args[1]).ConfigureAwait(false); + case "!owns": + if (args.Length > 2) { + return await ResponseOwns(args[1], args[2]).ConfigureAwait(false); + } else { + return await ResponseOwns(BotName, args[1]).ConfigureAwait(false); + } case "!play": if (args.Length > 2) { return await ResponsePlay(args[1], args[2]).ConfigureAwait(false); @@ -855,6 +861,58 @@ namespace ArchiSteamFarm { return await bot.ResponseAddLicense(gamesToRedeem).ConfigureAwait(false); } + private async Task ResponseOwns(string games) { + if (string.IsNullOrEmpty(games)) { + return null; + } + + Dictionary ownedGames = await ArchiWebHandler.GetOwnedGames().ConfigureAwait(false); + if (ownedGames == null || ownedGames.Count == 0) { + return "List of owned games is empty!"; + } + + // Check if this is uint + uint appID; + if (uint.TryParse(games, out appID)) { + string ownedName; + if (ownedGames.TryGetValue(appID, out ownedName)) { + return "Owned already: " + appID + " | " + ownedName; + } else { + return "Not owned yet: " + appID; + } + } + + StringBuilder response = new StringBuilder(); + + // This is a string + foreach (KeyValuePair game in ownedGames) { + if (game.Value.IndexOf(games, StringComparison.OrdinalIgnoreCase) < 0) { + continue; + } + + response.AppendLine(Environment.NewLine + "Owned already: " + game.Key + " | " + game.Value); + } + + if (response.Length > 0) { + return response.ToString(); + } else { + return "Not owned yet: " + games; + } + } + + private static async Task ResponseOwns(string botName, string games) { + if (string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(games)) { + return null; + } + + Bot bot; + if (!Bots.TryGetValue(botName, out bot)) { + return "Couldn't find any bot named " + botName + "!"; + } + + return await bot.ResponseOwns(games).ConfigureAwait(false); + } + private async Task ResponsePlay(HashSet gameIDs) { if (gameIDs == null || gameIDs.Count == 0) { return null; diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index 5e1d60170..1e74f74fb 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -31,6 +31,7 @@ using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using System.Xml; namespace ArchiSteamFarm { internal static class WebBrowser { @@ -155,6 +156,28 @@ namespace ArchiSteamFarm { return jObject; } + internal static async Task UrlGetToXML(string request, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + string content = await UrlGetToContent(request, cookies, referer).ConfigureAwait(false); + if (string.IsNullOrEmpty(content)) { + return null; + } + + XmlDocument xmlDocument = new XmlDocument(); + + try { + xmlDocument.LoadXml(content); + } catch (XmlException e) { + Logging.LogGenericException(e); + return null; + } + + return xmlDocument; + } + private static async Task UrlRequest(string request, HttpMethod httpMethod, Dictionary data = null, Dictionary cookies = null, string referer = null) { if (string.IsNullOrEmpty(request) || httpMethod == null) { return null;