Skip to content

Commit

Permalink
misc 移除了一些命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Aug 3, 2024
1 parent f0332c4 commit fccf803
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 247 deletions.
20 changes: 0 additions & 20 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ public Task OnLoaded()
"L" when access >= EAccess.Operator =>
Account.Command.ResponseGetAccountLicenses(bot, false),

"REMOVEDEMOS" or
"REMOVEDEMO" or
"RD" when access >= EAccess.Master =>
Account.Command.ResponseRemoveAllDemos(bot),

"EMAILOPTIONS" or
"EMAILOPTION" or
"EO" when access >= EAccess.Operator =>
Expand Down Expand Up @@ -524,21 +519,6 @@ public Task OnLoaded()
"L" when access >= EAccess.Operator =>
Account.Command.ResponseGetAccountLicenses(Utilities.GetArgsAsText(args, 1, ","), false),

"REMOVEDEMOS" or
"REMOVEDEMO" or
"RD" when access >= EAccess.Master =>
Account.Command.ResponseRemoveAllDemos(Utilities.GetArgsAsText(args, 1, ",")),

"REMOVELICENSES" or
"REMOVELICENSE" or
"RL" when argLength > 2 && access >= EAccess.Master =>
Account.Command.ResponseRemoveFreeLicenses(args[1], Utilities.GetArgsAsText(args, 2, ",")),

"REMOVELICENSES" or
"REMOVELICENSE" or
"RL" when access >= EAccess.Master =>
Account.Command.ResponseRemoveFreeLicenses(bot, args[1]),

"EMAILOPTIONS" or
"EMAILOPTION" or
"EO" when access >= EAccess.Operator =>
Expand Down
225 changes: 0 additions & 225 deletions ASFEnhance/Account/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,231 +133,6 @@ internal static class Command
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 移除免费许可证
/// </summary>
/// <param name="bot"></param>
/// <param name="query"></param>
/// <returns></returns>
internal static async Task<string?> ResponseRemoveFreeLicenses(Bot bot, string query)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

if (string.IsNullOrEmpty(query))
{
return bot.FormatBotResponse(Langs.ArgsIsEmpty);
}

var licensesOld = await WebRequest.GetOwnedLicenses(bot).ConfigureAwait(false);

if (licensesOld == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var oldSubs = licensesOld.Where(x => x.PackageId > 0 && x.Type == LicenseType.Complimentary).ToDictionary(x => x.PackageId, x => x.Name);
var gameIds = FetchGameIds(query, ESteamGameIdType.Sub, ESteamGameIdType.Sub);

var sema = new SemaphoreSlim(3, 3);

async Task workThread(uint subId)
{
try
{
sema.Wait();
try
{
await WebRequest.RemoveLicense(bot, subId).ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false);
}
finally
{
sema.Release();
}
}
catch (Exception ex)
{
ASFLogger.LogGenericException(ex);
}
}

var subIds = gameIds.Where(x => x.Type == ESteamGameIdType.Sub).Select(x => x.Id);
var tasks = subIds.Where(x => oldSubs.ContainsKey(x)).Select(x => workThread(x));
if (tasks.Any())
{
await Utilities.InParallel(gameIds.Select(x => WebRequest.RemoveLicense(bot, x.Id))).ConfigureAwait(false);
await Task.Delay(1000).ConfigureAwait(false);
}

var licensesNew = await WebRequest.GetOwnedLicenses(bot).ConfigureAwait(false);

if (licensesNew == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var newSubs = licensesNew.Where(x => x.PackageId > 0 && x.Type == LicenseType.Complimentary).Select(x => x.PackageId).ToHashSet();

var sb = new StringBuilder();
sb.AppendLine(bot.FormatBotResponse(Langs.MultipleLineResult));

foreach (var gameId in gameIds)
{
string msg;
if (gameId.Type == ESteamGameIdType.Error)
{
msg = Langs.AccountSubInvalidArg;
}
else
{
uint subId = gameId.Id;
if (oldSubs.TryGetValue(subId, out var name))
{
bool succ = !newSubs.Contains(subId);
msg = string.Format(Langs.AccountSubRemovedItem, name, succ ? Langs.Success : Langs.Failure);
}
else
{
msg = Langs.AccountSubNotOwn;
}
}
sb.AppendLine(bot.FormatBotResponse(Langs.CookieItem, gameId.Input, msg));
}

return sb.ToString();
}

/// <summary>
/// 移除免费许可证 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="query"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseRemoveFreeLicenses(string botNames, string query)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0))
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseRemoveFreeLicenses(bot, query))).ConfigureAwait(false);

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

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 移除所有Demo
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseRemoveAllDemos(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

var licensesOld = await WebRequest.GetOwnedLicenses(bot).ConfigureAwait(false);

if (licensesOld == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var oldSubs = licensesOld
.Where(static x => x.PackageId > 0 && x.Type == LicenseType.Complimentary && x?.Name?.EndsWith("Demo") == true)
.Select(static x => x.PackageId)
.ToHashSet();

if (oldSubs.Count == 0)
{
return bot.FormatBotResponse(Langs.AccountSubDemoSubNotFount);
}

var sema = new SemaphoreSlim(3, 3);

async Task workThread(uint subId)
{
try
{
sema.Wait();
try
{
await WebRequest.RemoveLicense(bot, subId).ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false);
}
finally
{
sema.Release();
}
}
catch (Exception ex)
{
ASFLogger.LogGenericException(ex);
}
}

var tasks = oldSubs.Select(x => workThread(x));
await Utilities.InParallel(tasks).ConfigureAwait(false);

await Task.Delay(1000).ConfigureAwait(false);

var licensesNew = await WebRequest.GetOwnedLicenses(bot).ConfigureAwait(false);

if (licensesNew == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var newSubs = licensesNew
.Where(static x => x.PackageId > 0 && x.Type == LicenseType.Complimentary && x?.Name?.EndsWith("Demo") == true)
.Select(static x => x.PackageId)
.ToHashSet();
var count = oldSubs.Where(x => !newSubs.Contains(x)).Count();

return bot.FormatBotResponse(Langs.AccountSubRemovedDemos, count);
}

/// <summary>
/// 移除所有Demo (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseRemoveAllDemos(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0))
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseRemoveAllDemos(bot))).ConfigureAwait(false);

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

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 获取邮箱偏好
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions ASFEnhance/Profile/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static class HtmlParser
return null;
}

IDocument content = response.Content;
var content = response.Content;

var eleNickName = content.SelectSingleNode("//div[@class='persona_name']/span[1]");
string nickName = eleNickName?.TextContent ?? "";
Expand Down Expand Up @@ -62,7 +62,7 @@ internal static class HtmlParser
var eleFriendsCount = content.SelectSingleNode("//a[contains(@href,'/friends/')]/span[last()]");
string? strFriendsCount = eleFriendsCount?.TextContent.Replace(",", "");

StringBuilder result = new();
var result = new StringBuilder();
result.AppendLine(Langs.MultipleLineResult);
result.AppendLine(Langs.ProfileHeader);
result.AppendLineFormat(Langs.ProfileNickname, nickName);
Expand Down

0 comments on commit fccf803

Please sign in to comment.