Skip to content

Commit 18a9650

Browse files
committed
add multiple appids support
1 parent c077f9f commit 18a9650

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

GameRemover/GameRemoverPlugin.cs

+45-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Threading.Tasks;
8-
using AngleSharp.Dom;
98
using ArchiSteamFarm.Core;
109
using ArchiSteamFarm.Localization;
1110
using ArchiSteamFarm.Plugins.Interfaces;
@@ -40,64 +39,76 @@ public Task OnLoaded()
4039

4140
return args[0].ToUpperInvariant() switch
4241
{
43-
"DELETEGAME" when args.Length > 2 => ResponseDeleteGame(steamID, access, args[1], args[2]),
42+
"DELETEGAME" when args.Length > 2 => ResponseDeleteGame(steamID, access, args[1], Utilities.GetArgsAsText(args, 2, ",")),
4443
"DELETEGAME" when args.Length > 1 => ResponseDeleteGame(bot, access, args[1]),
4544
_ => Task.FromResult<string?>(null)
4645
};
4746
}
4847

49-
private static async Task<string?> ResponseDeleteGame(Bot bot, EAccess access, string appIDText)
48+
private static async Task<string?> ResponseDeleteGame(Bot bot, EAccess access, string appIDsText)
5049
{
5150
if (access < EAccess.Master)
5251
{
5352
return null;
5453
}
5554

56-
if (!uint.TryParse(appIDText, out uint appID) || (appID == 0))
55+
HashSet<uint> appIDs = new();
56+
var appIDTexts = appIDsText.Split(',', StringSplitOptions.RemoveEmptyEntries);
57+
foreach (var appIDText in appIDTexts)
5758
{
58-
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(appID)));
59+
if (!uint.TryParse(appIDText, out var appID) || (appID == 0) || !appIDs.Add(appID))
60+
{
61+
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, appIDText));
62+
}
5963
}
6064

61-
const string requestDeleteGamePage = "/en/wizard/HelpWithGameIssue/?appid={0}&issueid=123";
62-
Uri uriDeleteGamePage = new(ArchiWebHandler.SteamHelpURL, string.Format(CultureInfo.InvariantCulture, requestDeleteGamePage, appID));
63-
using IDocument? responseDeleteGamePage = (await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(uriDeleteGamePage).ConfigureAwait(false))?.Content;
64-
if (responseDeleteGamePage == null)
65+
ushort successCount = 0;
66+
foreach (var appID in appIDs)
6567
{
66-
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(responseDeleteGamePage)));
68+
const string RequestDeleteGamePage = "/en/wizard/HelpWithGameIssue/?appid={0}&issueid=123";
69+
Uri uriDeleteGamePage = new(ArchiWebHandler.SteamHelpURL, string.Format(CultureInfo.InvariantCulture, RequestDeleteGamePage, appID));
70+
using var responseDeleteGamePage = (await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(uriDeleteGamePage).ConfigureAwait(false))?.Content;
71+
if (responseDeleteGamePage == null)
72+
{
73+
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(responseDeleteGamePage)));
74+
}
75+
76+
var node = responseDeleteGamePage.SelectSingleNode("//input[@id='packageid']");
77+
if (node == null)
78+
{
79+
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(node)));
80+
}
81+
82+
if (!uint.TryParse(node.GetAttribute("value"), out var packageID) || (packageID == 0))
83+
{
84+
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(packageID)));
85+
}
86+
87+
Dictionary<string, string> data = new(3)
88+
{
89+
{"packageid", packageID.ToString(CultureInfo.InvariantCulture)},
90+
{"appid", appIDsText}
91+
};
92+
93+
const string RequestDeleteGame = "/en/wizard/AjaxDoPackageRemove";
94+
var responseDeleteGame = (await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<BooleanResponse>(new Uri(ArchiWebHandler.SteamHelpURL, RequestDeleteGame), data: data, referer: uriDeleteGamePage).ConfigureAwait(false))?.Content;
95+
96+
if (responseDeleteGame?.Success == true)
97+
successCount++;
6798
}
6899

69-
IElement? node = responseDeleteGamePage.SelectSingleNode("//input[@id='packageid']");
70-
if (node == null)
71-
{
72-
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(node)));
73-
}
74-
75-
if (!uint.TryParse(node.GetAttribute("value"), out uint packageID) || (packageID == 0))
76-
{
77-
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(packageID)));
78-
}
79-
80-
Dictionary<string, string> data = new(3)
81-
{
82-
{"packageid", packageID.ToString(CultureInfo.InvariantCulture)},
83-
{"appid", appIDText}
84-
};
85-
86-
const string requestDeleteGame = "/en/wizard/AjaxDoPackageRemove";
87-
BooleanResponse? responseDeleteGame = (await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<BooleanResponse>(new Uri(ArchiWebHandler.SteamHelpURL, requestDeleteGame), data: data, referer: uriDeleteGamePage).ConfigureAwait(false))?.Content;
88-
89-
return bot.Commands.FormatBotResponse(responseDeleteGame?.Success == true ? Strings.Success : Strings.WarningFailed);
100+
return bot.Commands.FormatBotResponse(successCount == appIDs.Count ? Strings.Success : $"{Strings.WarningFailed}: {successCount} / {appIDs.Count}");
90101
}
91102

92-
private static async Task<string?> ResponseDeleteGame(ulong steamID, EAccess access, string botNames, string appIDText)
103+
private static async Task<string?> ResponseDeleteGame(ulong steamID, EAccess access, string botNames, string appIDsText)
93104
{
94-
HashSet<Bot>? bots = Bot.GetBots(botNames);
105+
var bots = Bot.GetBots(botNames);
95106
if ((bots == null) || (bots.Count == 0))
96107
{
97108
return access >= EAccess.Owner ? Commands.FormatStaticResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames)) : null;
98109
}
99110

100-
IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseDeleteGame(bot, Commands.GetProxyAccess(bot, access, steamID), appIDText))).ConfigureAwait(false);
111+
var results = await Utilities.InParallel(bots.Select(bot => ResponseDeleteGame(bot, Commands.GetProxyAccess(bot, access, steamID), appIDsText))).ConfigureAwait(false);
101112

102113
List<string> responses = new(results.Where(result => !string.IsNullOrEmpty(result))!);
103114

0 commit comments

Comments
 (0)