Skip to content

Commit

Permalink
feat 新增 REDEEMPOINTSITEM REDEEMPOINTSBADGE 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jan 10, 2024
1 parent 9f56c5b commit 03122f1
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 115 deletions.
18 changes: 18 additions & 0 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,24 @@ public Task OnLoaded()
"VP" when access >= EAccess.Operator =>
Store.Command.ResponseViewPage(bot, args[1]),

"REDEEMPOINTSITEM" or
"REDEEMPOINTITEM" or
"RPI" when argLength > 2 && access >= EAccess.Master =>
Store.Command.ResponseUnlockPointItem(args[1], Utilities.GetArgsAsText(args, 2, ",")),
"REDEEMPOINTSITEM" or
"REDEEMPOINTITEM" or
"RPI" when access >= EAccess.Master =>
Store.Command.ResponseUnlockPointItem(bot, args[1]),

"REDEEMPOINTSBADGE" or
"REDEEMPOINTBADGE" or
"RPB" when argLength > 3 && access >= EAccess.Master =>
Store.Command.ResponseUnlockPointBadge(args[1], args[2], Utilities.GetArgsAsText(args, 3, ",")),
"REDEEMPOINTSBADGE" or
"REDEEMPOINTBADGE" or
"RPB" when argLength > 2 && access >= EAccess.Master =>
Store.Command.ResponseUnlockPointBadge(bot, args[1], args[2]),

//Wallet
"REDEEMWALLET" or
"RWA" when args.Length > 2 && access >= EAccess.Master =>
Expand Down
58 changes: 0 additions & 58 deletions ASFEnhance/Event/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,62 +639,4 @@ internal static class Command

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

/// <summary>
/// 购买点数徽章
/// </summary>
/// <param name="bot"></param>
/// <param name="defId"></param>
/// <param name="level"></param>
/// <returns></returns>
internal static async Task<string?> ResponseUnlockPointBadge(Bot bot, string defId, string level)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

if (!uint.TryParse(defId, out uint intDefId))
{
return bot.FormatBotResponse(string.Format(Langs.ArgumentNotInteger, nameof(defId)));
}

if (!uint.TryParse(level, out uint intLevel))
{
return bot.FormatBotResponse(string.Format(Langs.ArgumentNotInteger, nameof(level)));
}

string result = await WebRequest.RedeemPointsForBadgeLevel(bot, intDefId, intLevel).ConfigureAwait(false);

return bot.FormatBotResponse(result);
}

/// <summary>
/// 购买点数徽章 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="defId"></param>
/// <param name="level"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseUnlockPointBadge(string botNames, string defId, string level)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

HashSet<Bot>? bots = Bot.GetBots(botNames);

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

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseUnlockPointBadge(bot, defId, level))).ConfigureAwait(false);

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

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
29 changes: 0 additions & 29 deletions ASFEnhance/Event/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,33 +279,4 @@ internal static async Task MakeWinterSteamAwardVote(Bot bot, int gameID, int cat

return string.Format(Langs.CheckVote, data.UserVotes?.Count ?? -1, data.Definitions?.Votes?.Count ?? -1);
}

/// <summary>
/// 兑换点数徽章
/// </summary>
/// <param name="bot"></param>
/// <param name="defId"></param>
/// <param name="level"></param>
/// <returns></returns>
internal static async Task<string> RedeemPointsForBadgeLevel(Bot bot, uint defId, uint level)
{
(_, string? token) = await bot.ArchiWebHandler.CachedAccessToken.GetValue().ConfigureAwait(false);

if (string.IsNullOrEmpty(token))
{
return Langs.NetworkError;
}

var request = new Uri(SteamApiURL, "/ILoyaltyRewardsService/RedeemPointsForBadgeLevel/v1/");

var data = new Dictionary<string, string>(3) {
{ "access_token", token },
{ "defid", defId.ToString() },
{ "num_levels", level.ToString() },
};

var response = await bot.ArchiWebHandler.UrlPostToHtmlDocumentWithSession(request, data: data, session: ArchiWebHandler.ESession.None).ConfigureAwait(false);

return response?.StatusCode == System.Net.HttpStatusCode.OK ? Langs.Done : Langs.Failure;
}
}
Loading

0 comments on commit 03122f1

Please sign in to comment.