Skip to content

Commit

Permalink
feat 新增deck命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Dec 1, 2022
1 parent c7d8055 commit 35989e8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ private static async Task<string> ResponseCommand(Bot bot, EAccess access, strin
case "DL2" when access >= EAccess.Operator:
return await Event.Command.ResponseDL2(bot).ConfigureAwait(false);

case "DECK" when access >= EAccess.Operator:
return await Event.Command.ResponseSteamDeck(bot).ConfigureAwait(false);

//Shortcut
case "P":
return await bot.Commands.Response(access, "POINTS", steamID).ConfigureAwait(false);
Expand Down Expand Up @@ -313,6 +316,9 @@ private static async Task<string> ResponseCommand(Bot bot, EAccess access, strin

case "DL2" when access >= EAccess.Operator:
return await Event.Command.ResponseDL2(Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);

case "DECK" when access >= EAccess.Operator:
return await Event.Command.ResponseSteamDeck(Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);

//Shortcut
case "AL":
Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Reflection;

[assembly: CLSCompliant(false)]
[assembly: AssemblyVersion("1.6.22.1")]
[assembly: AssemblyVersion("1.6.23.0")]
[assembly: AssemblyCopyright("Copyright © 2022 Chr_")]
[assembly: AssemblyProduct("ASFEnhance")]
[assembly: AssemblyCompany("chrxw.com")]
Expand Down
49 changes: 49 additions & 0 deletions ASFEnhance/Event/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,54 @@ internal static class Command

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

/// <summary>
/// 获取Steam Deck 贴纸 12.1 - ?
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseSteamDeck(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

string token = await WebRequest.FetchSteamDeckEventToken(bot).ConfigureAwait(false);
if (token == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}
await WebRequest.ClaimSteamDeckStick(bot, token).ConfigureAwait(false);

return bot.FormatBotResponse("Done!");
}

/// <summary>
/// 获取Steam Deck 贴纸 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseSteamDeck(string botNames)
{
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 => ResponseSteamDeck(bot))).ConfigureAwait(false);

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

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
}
46 changes: 46 additions & 0 deletions ASFEnhance/Event/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,51 @@ internal static async Task DoEventTask(Bot bot, string clan_accountid, uint door

return match.Success ? match.Groups[1].Value : null;
}

/// <summary>
/// 获取Token
/// </summary>
/// <param name="bot"></param>
/// <param name="salePage"></param>
/// <returns></returns>
internal static async Task<string?> FetchSteamDeckEventToken(Bot bot)
{
Uri request = new(SteamStoreURL, $"/sale/thegameawardssteamdeckdrop2022");

var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request).ConfigureAwait(false);

if (response == null)
{
return null;
}

var configEle = response.Content.QuerySelector("#application_config");
var token = configEle?.GetAttribute("data-loyalty_webapi_token");

if (string.IsNullOrEmpty(token))
{
return null;
}

return token.Substring(1,token.Length-2);
}

/// <summary>
/// 领取贴纸
/// </summary>
/// <param name="bot"></param>
/// <param name="clan_accountid"></param>
/// <param name="door_index"></param>
/// <returns></returns>
internal static async Task ClaimSteamDeckStick(Bot bot, string token)
{
Uri request = new($"https://api.steampowered.com/ISaleItemRewardsService/ClaimItem/v1?access_token={token}");

Dictionary<string, string> data = new(1) {
{"input_protobuf_encoded", "CghzY2hpbmVzZQ=="},
};

_ = await bot.ArchiWebHandler.UrlPostWithSession(request, data: data, session: ArchiSteamFarm.Steam.Integration.ArchiWebHandler.ESession.None).ConfigureAwait(false);
}
}
}

0 comments on commit 35989e8

Please sign in to comment.