Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ezhevita committed Jan 17, 2021
1 parent 780eb70 commit 673a35a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ArchiSteamFarm"]
path = ArchiSteamFarm
url = https://github.com/JustArchiNET/ArchiSteamFarm
1 change: 1 addition & 0 deletions ArchiSteamFarm
Submodule ArchiSteamFarm added at 945e94
81 changes: 81 additions & 0 deletions GameRemover/GameRemover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AngleSharp.Dom;
using ArchiSteamFarm;
using ArchiSteamFarm.Json;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Plugins;
using JetBrains.Annotations;

namespace GameRemover {
// ReSharper disable once UnusedMember.Global
[Export(typeof(IPlugin))]
[UsedImplicitly]
public class GameRemover : IBotCommand {
public void OnLoaded() {
ASF.ArchiLogger.LogGenericInfo(nameof(GameRemover) + " is loaded!");
}

public string Name => nameof(GameRemover);
public Version Version => Assembly.GetExecutingAssembly().GetName().Version ?? throw new InvalidOperationException(nameof(Version));

public async Task<string?> OnBotCommand(Bot bot, ulong steamID, string message, string[] args) {
return args[0].ToUpperInvariant() switch {
"DELETEGAME" when args.Length > 2 => await ResponseDeleteGame(steamID, args[1], args[2]),
"DELETEGAME" when args.Length > 1 => await ResponseDeleteGame(bot, steamID, args[1]).ConfigureAwait(false),
_ => null
};
}

private static async Task<string?> ResponseDeleteGame(Bot bot, ulong steamID, string appIDText) {
if (!bot.HasAccess(steamID, BotConfig.EAccess.Master)) {
return null;
}

if (!uint.TryParse(appIDText, out uint appID) || (appID == 0)) {
return bot.Commands.FormatBotResponse(string.Format(Strings.ErrorIsInvalid, nameof(appID)));
}

const string requestDeleteGamePage = "/en/wizard/HelpWithGameIssue/?appid={0}&issueid=123";
using IDocument? responseDeleteGamePage = (await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(ArchiWebHandler.SteamHelpURL, string.Format(requestDeleteGamePage, appID)).ConfigureAwait(false))?.Content;
if (responseDeleteGamePage == null) {
return bot.Commands.FormatBotResponse(string.Format(Strings.ErrorObjectIsNull, nameof(responseDeleteGamePage)));
}

IElement? node = responseDeleteGamePage.SelectSingleNode("//input[@id='packageid']");
if (node == null) {
return bot.Commands.FormatBotResponse(string.Format(Strings.ErrorObjectIsNull, nameof(node)));
}

if (!uint.TryParse(node.GetAttribute("value"), out uint packageID) || (packageID == 0)) {
return bot.Commands.FormatBotResponse(string.Format(Strings.ErrorIsInvalid, nameof(packageID)));
}

Dictionary<string, string> data = new(3) {
{"packageid", packageID.ToString()},
{"appid", appIDText}
};

const string requestDeleteGame = "/en/wizard/AjaxDoPackageRemove";
Steam.BooleanResponse? responseDeleteGame = (await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<Steam.BooleanResponse>(ArchiWebHandler.SteamHelpURL, requestDeleteGame, data: data, referer: $"https://help.steampowered.com/en/wizard/HelpWithGameIssue/?appid={appID}&issueid=123").ConfigureAwait(false))?.Content;

return bot.Commands.FormatBotResponse(responseDeleteGame?.Success == true ? Strings.Success : Strings.WarningFailed);
}

private static async Task<string?> ResponseDeleteGame(ulong steamID, string botNames, string appIDText) {
HashSet<Bot>? bots = Bot.GetBots(botNames);
if ((bots == null) || (bots.Count == 0)) {
return ASF.IsOwner(steamID) ? Commands.FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null;
}

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseDeleteGame(bot, steamID, appIDText))).ConfigureAwait(false);

List<string> responses = new(results.Where(result => !string.IsNullOrEmpty(result))!);
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
}
18 changes: 18 additions & 0 deletions GameRemover/GameRemover.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Nullable>enable</Nullable>
<TargetFrameworks>net5.0;net48</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="*" IncludeAssets="compile" />
<PackageReference Include="JetBrains.Annotations" Version="*" IncludeAssets="compile" />
<PackageReference Include="System.Composition.AttributedModel" Version="*" IncludeAssets="compile" />
<ProjectReference Include="..\ArchiSteamFarm\ArchiSteamFarm\ArchiSteamFarm.csproj" Private="false" ExcludeAssets="compile" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Vital7.GameRemover.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vital7.GameRemover", "Vital7.GameRemover\Vital7.GameRemover.csproj", "{0B254C0E-94C8-4546-AD71-DEBA432C258B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameRemover", "GameRemover\GameRemover.csproj", "{0B254C0E-94C8-4546-AD71-DEBA432C258B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
90 changes: 0 additions & 90 deletions Vital7.GameRemover/GameRemover.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Vital7.GameRemover/Vital7.GameRemover.csproj

This file was deleted.

0 comments on commit 673a35a

Please sign in to comment.