Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jul 15, 2024
1 parent f0139d7 commit dbba888
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 120 deletions.
32 changes: 23 additions & 9 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,17 @@ public Task OnLoaded()
//WishList
"ADDWISHLIST" or
"AW" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseAddWishlist(args[1], Utilities.GetArgsAsText(args, 2, ",")),
Wishlist.Command.ResponseAddWishlist(args[1], Utilities.GetArgsAsText(args, 2, ","), true),
"ADDWISHLIST" or
"AW" when access >= EAccess.Master =>
Wishlist.Command.ResponseAddWishlist(bot, args[1]),
Wishlist.Command.ResponseAddWishlist(bot, args[1], true),

"REMOVEWISHLIST" or
"RW" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseAddWishlist(args[1], Utilities.GetArgsAsText(args, 2, ","), false),
"REMOVEWISHLIST" or
"RW" when access >= EAccess.Master =>
Wishlist.Command.ResponseAddWishlist(bot, args[1], false),

"CHECK" or
"CK" when argLength > 2 && access >= EAccess.Master =>
Expand All @@ -1010,20 +1017,27 @@ public Task OnLoaded()
"FG" when access >= EAccess.Master =>
Wishlist.Command.ResponseFollowGame(bot, args[1], true),

"REMOVEWISHLIST" or
"RW" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseRemoveWishlist(args[1], Utilities.GetArgsAsText(args, 2, ",")),
"REMOVEWISHLIST" or
"RW" when access >= EAccess.Master =>
Wishlist.Command.ResponseRemoveWishlist(bot, args[1]),

"UNFOLLOWGAME" or
"UFG" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseFollowGame(args[1], Utilities.GetArgsAsText(args, 2, ","), false),
"UNFOLLOWGAME" or
"UFG" when access >= EAccess.Master =>
Wishlist.Command.ResponseFollowGame(bot, args[1], false),

"IGNOREGAME" or
"IG" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseIgnoreGame(args[1], Utilities.GetArgsAsText(args, 2, ","), true),
"IGNOREGAME" or
"IG" when access >= EAccess.Master =>
Wishlist.Command.ResponseIgnoreGame(bot, args[1], true),

"REMOVEIGNOREGAME" or
"RIG" when argLength > 2 && access >= EAccess.Master =>
Wishlist.Command.ResponseIgnoreGame(args[1], Utilities.GetArgsAsText(args, 2, ","), false),
"REMOVEIGNOREGAME" or
"RIG" when access >= EAccess.Master =>
Wishlist.Command.ResponseIgnoreGame(bot, args[1], false),

//Inventory
"STACKINVENTORY" or
"STACKINV" or
Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/Data/Common/AddWishlistResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace ASFEnhance.Data.Common;

Expand Down
15 changes: 15 additions & 0 deletions ASFEnhance/Data/Common/IgnoreGameResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace ASFEnhance.Data.Common;

/// <summary>
/// 添加愿望单响应
/// </summary>
public record IgnoreGameResponse
{
/// <summary>
/// 结果
/// </summary>
[JsonPropertyName("success")]
public bool Result { get; set; }
}
4 changes: 2 additions & 2 deletions ASFEnhance/IPC/Controllers/WishlistController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<ActionResult<GenericResponse>> AddWishlist(string botNames, [F
{
if (!bot.IsConnectedAndLoggedOn) { return (bot.BotName, false); }

var result = await Wishlist.WebRequest.AddWishlist(bot, appid).ConfigureAwait(false);
var result = await Wishlist.WebRequest.AddWishlist(bot, appid, true).ConfigureAwait(false);
return (bot.BotName, result?.Result == true);
}
)).ConfigureAwait(false);
Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task<ActionResult<GenericResponse>> RemoveWishlist(string botNames,
{
if (!bot.IsConnectedAndLoggedOn) { return (bot.BotName, false); }

var result = await Wishlist.WebRequest.RemoveWishlist(bot, appid).ConfigureAwait(false);
var result = await Wishlist.WebRequest.AddWishlist(bot, appid, false).ConfigureAwait(false);
return (bot.BotName, result?.Result == true);
}
)).ConfigureAwait(false);
Expand Down
95 changes: 49 additions & 46 deletions ASFEnhance/WishList/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ internal static class Command
/// </summary>
/// <param name="bot"></param>
/// <param name="targetGameIds"></param>
/// <param name="isAddWishlist"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseAddWishlist(Bot bot, string targetGameIds)
internal static async Task<string?> ResponseAddWishlist(Bot bot, string targetGameIds, bool isAddWishlist)
{
if (string.IsNullOrEmpty(targetGameIds))
{
Expand All @@ -38,7 +39,7 @@ internal static class Command
continue;
}

var result = await bot.AddWishlist(gameId).ConfigureAwait(false);
var result = await bot.AddWishlist(gameId, isAddWishlist).ConfigureAwait(false);

response.AppendLine(bot.FormatBotResponse(Strings.BotAddLicense, gameId, result?.Result == true ? Langs.Success : Langs.Failure));
}
Expand All @@ -51,9 +52,10 @@ internal static class Command
/// </summary>
/// <param name="botNames"></param>
/// <param name="targetGameIds"></param>
/// <param name="isAddWishlist"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseAddWishlist(string botNames, string targetGameIds)
internal static async Task<string?> ResponseAddWishlist(string botNames, string targetGameIds, bool isAddWishlist)
{
if (string.IsNullOrEmpty(botNames))
{
Expand All @@ -72,21 +74,22 @@ internal static class Command
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseAddWishlist(bot, targetGameIds))).ConfigureAwait(false);
var results = await Utilities.InParallel(bots.Select(bot => ResponseAddWishlist(bot, targetGameIds, isAddWishlist))).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>
/// <param name="bot"></param>
/// <param name="targetGameIds"></param>
/// <param name="isFollow"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseRemoveWishlist(Bot bot, string targetGameIds)
internal static async Task<string?> ResponseFollowGame(Bot bot, string targetGameIds, bool isFollow)
{
if (string.IsNullOrEmpty(targetGameIds))
{
Expand All @@ -110,22 +113,23 @@ internal static class Command
continue;
}

var result = await bot.RemoveWishlist(gameId).ConfigureAwait(false);
bool result = await bot.FollowGame(gameId, isFollow).ConfigureAwait(false);

response.AppendLine(bot.FormatBotResponse(Strings.BotAddLicense, gameId, result?.Result == true ? Langs.Success : Langs.Failure));
response.AppendLine(bot.FormatBotResponse(Strings.BotAddLicense, gameId, result ? Langs.Success : Langs.Failure));
}

return response.Length > 0 ? response.ToString() : null;
}

/// <summary>
/// 删除愿望单 (多个Bot)
/// 关注游戏 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="targetGameIds"></param>
/// <param name="isFollow"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseRemoveWishlist(string botNames, string targetGameIds)
internal static async Task<string?> ResponseFollowGame(string botNames, string targetGameIds, bool isFollow)
{
if (string.IsNullOrEmpty(botNames))
{
Expand All @@ -144,22 +148,22 @@ internal static class Command
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseRemoveWishlist(bot, targetGameIds))).ConfigureAwait(false);
var results = await Utilities.InParallel(bots.Select(bot => ResponseFollowGame(bot, targetGameIds, isFollow))).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>
/// <param name="bot"></param>
/// <param name="targetGameIds"></param>
/// <param name="isFollow"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseFollowGame(Bot bot, string targetGameIds, bool isFollow)
internal static async Task<string?> ResponseCheckGame(Bot bot, string targetGameIds)
{
if (string.IsNullOrEmpty(targetGameIds))
{
Expand All @@ -171,35 +175,43 @@ internal static class Command
return bot.FormatBotResponse(Strings.BotNotConnected);
}

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

string[] games = targetGameIds.Split(SeparatorDot, StringSplitOptions.RemoveEmptyEntries);
var games = targetGameIds.Split(SeparatorDot, StringSplitOptions.RemoveEmptyEntries);

foreach (string game in games)
{
if (!uint.TryParse(game, out uint gameId) || (gameId == 0))
{
response.AppendLine(bot.FormatBotResponse(Strings.ErrorIsInvalid, nameof(gameId)));
sb.AppendLineFormat(Langs.CheckGameItemError, game);
continue;
}

bool result = await bot.FollowGame(gameId, isFollow).ConfigureAwait(false);
var result = await bot.CheckGame(gameId).ConfigureAwait(false);

response.AppendLine(bot.FormatBotResponse(Strings.BotAddLicense, gameId, result ? Langs.Success : Langs.Failure));
if (result != null && result.Success)
{
sb.AppendLineFormat(Langs.CheckGameItemSuccess, gameId, result.Name, result.Owned ? "√" : "×", result.InWishlist ? "√" : "×", result.IsFollow ? "√" : "×");
}
else
{
sb.AppendLineFormat(Langs.CheckGameItemFailed, gameId, result?.Name ?? gameId.ToString());
}
}

return response.Length > 0 ? response.ToString() : null;
return sb.ToString();
}

/// <summary>
/// 关注游戏 (多个Bot)
/// 检查游戏拥有/愿望单/关注 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="targetGameIds"></param>
/// <param name="isFollow"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseFollowGame(string botNames, string targetGameIds, bool isFollow)
internal static async Task<string?> ResponseCheckGame(string botNames, string targetGameIds)
{
if (string.IsNullOrEmpty(botNames))
{
Expand All @@ -218,22 +230,22 @@ internal static class Command
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseFollowGame(bot, targetGameIds, isFollow))).ConfigureAwait(false);
var results = await Utilities.InParallel(bots.Select(bot => ResponseCheckGame(bot, targetGameIds))).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>
/// <param name="bot"></param>
/// <param name="targetGameIds"></param>
/// <param name="isIgnore"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseCheckGame(Bot bot, string targetGameIds)
internal static async Task<string?> ResponseIgnoreGame(Bot bot, string targetGameIds, bool isIgnore)
{
if (string.IsNullOrEmpty(targetGameIds))
{
Expand All @@ -245,43 +257,35 @@ internal static class Command
return bot.FormatBotResponse(Strings.BotNotConnected);
}

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

var games = targetGameIds.Split(SeparatorDot, StringSplitOptions.RemoveEmptyEntries);
string[] games = targetGameIds.Split(SeparatorDot, StringSplitOptions.RemoveEmptyEntries);

foreach (string game in games)
{
if (!uint.TryParse(game, out uint gameId) || (gameId == 0))
{
sb.AppendLineFormat(Langs.CheckGameItemError, game);
response.AppendLine(bot.FormatBotResponse(Strings.ErrorIsInvalid, nameof(gameId)));
continue;
}

var result = await bot.CheckGame(gameId).ConfigureAwait(false);
bool result = await bot.IgnoreGame(gameId, isIgnore).ConfigureAwait(false);

if (result != null && result.Success)
{
sb.AppendLineFormat(Langs.CheckGameItemSuccess, gameId, result.Name, result.Owned ? "√" : "×", result.InWishlist ? "√" : "×", result.IsFollow ? "√" : "×");
}
else
{
sb.AppendLineFormat(Langs.CheckGameItemFailed, gameId, result?.Name ?? gameId.ToString());
}
response.AppendLine(bot.FormatBotResponse(Strings.BotAddLicense, gameId, result ? Langs.Success : Langs.Failure));
}

return sb.ToString();
return response.Length > 0 ? response.ToString() : null;
}

/// <summary>
/// 检查游戏拥有/愿望单/关注 (多个Bot)
/// 关注游戏 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="targetGameIds"></param>
/// <param name="isIgnore"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseCheckGame(string botNames, string targetGameIds)
internal static async Task<string?> ResponseIgnoreGame(string botNames, string targetGameIds, bool isIgnore)
{
if (string.IsNullOrEmpty(botNames))
{
Expand All @@ -300,11 +304,10 @@ internal static class Command
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseCheckGame(bot, targetGameIds))).ConfigureAwait(false);
var results = await Utilities.InParallel(bots.Select(bot => ResponseIgnoreGame(bot, targetGameIds, isIgnore))).ConfigureAwait(false);

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

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

}
Loading

0 comments on commit dbba888

Please sign in to comment.