Add !owns, closes #159

This commit is contained in:
JustArchi
2016-03-15 04:20:28 +01:00
parent 765f9d29bb
commit 895c69642d
3 changed files with 127 additions and 0 deletions

View File

@@ -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<Dictionary<uint, string>> 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<uint, string> result = new Dictionary<uint, string>(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<Steam.TradeOffer> GetTradeOffers() {
if (string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey)) {
return null;

View File

@@ -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<string> ResponseOwns(string games) {
if (string.IsNullOrEmpty(games)) {
return null;
}
Dictionary<uint, string> 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<uint, string> 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<string> 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<string> ResponsePlay(HashSet<uint> gameIDs) {
if (gameIDs == null || gameIDs.Count == 0) {
return null;

View File

@@ -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<XmlDocument> UrlGetToXML(string request, Dictionary<string, string> 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<HttpResponseMessage> UrlRequest(string request, HttpMethod httpMethod, Dictionary<string, string> data = null, Dictionary<string, string> cookies = null, string referer = null) {
if (string.IsNullOrEmpty(request) || httpMethod == null) {
return null;