Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Aug 15, 2024
1 parent 18b0287 commit 1fade00
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 62 deletions.
23 changes: 22 additions & 1 deletion ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace ASFEnhance;

[Export(typeof(IPlugin))]
internal sealed class ASFEnhance : IASF, IBotCommand2, IBotFriendRequest, IGitHubPluginUpdates
internal sealed class ASFEnhance : IASF, IBotCommand2, IBotFriendRequest, IBotModules, IGitHubPluginUpdates
{
public string Name => nameof(ASFEnhance);

Expand Down Expand Up @@ -1243,4 +1243,25 @@ public Task<bool> OnBotFriendRequest(Bot bot, ulong steamId)

return Task.FromResult(result);
}

/// <inheritdoc/>
public Task OnBotInitModules(Bot bot, IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties)
{
if (additionalConfigProperties != null)
{
foreach (var (configProperty, configValue) in additionalConfigProperties)
{
if (configProperty == "UserCountry" && configValue.ValueKind == JsonValueKind.String)
{
var countryCode = configValue.GetString();
if (!string.IsNullOrEmpty(countryCode))
{
CustomUserCountry.TryAdd(bot, countryCode.ToUpperInvariant());
}
break;
}
}
}
return Task.CompletedTask;
}
}
10 changes: 5 additions & 5 deletions ASFEnhance/Cart/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class WebRequest
internal static async Task<GetCartResponse?> GetAccountCart(this Bot bot)
{
var token = bot.AccessToken ?? throw new AccessTokenNullException();
var userCountry = WalletCurrency2UserCountry(bot.WalletCurrency);
var userCountry = bot.GetUserCountryCode();
var request = new Uri(SteamApiURL, $"/IAccountCartService/GetCart/v1/?access_token={token}&user_country={userCountry}");
var response = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession<AbstractResponse<GetCartResponse>>(request, referer: SteamStoreURL).ConfigureAwait(false);
return response?.Content?.Response;
Expand Down Expand Up @@ -51,7 +51,7 @@ internal static class WebRequest
var payload = new AddItemsToCartRequest
{
Items = items.ToList(),
UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency),
UserCountry = bot.GetUserCountryCode(),
Navdata = new NavdataData
{
Domain = "store.steampowered.com",
Expand Down Expand Up @@ -97,7 +97,7 @@ internal static class WebRequest
var payload = new AddItemsToCartRequest
{
Items = items,
UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency),
UserCountry = bot.GetUserCountryCode(),
Navdata = new NavdataData
{
Domain = "store.steampowered.com",
Expand Down Expand Up @@ -146,7 +146,7 @@ internal static class WebRequest
var payload = new ModifyLineItemRequest
{
LineItemId = lineItemId,
UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency),
UserCountry = bot.GetUserCountryCode(),
GiftInfo = giftInfo,
Flags = new FlagsData
{
Expand Down Expand Up @@ -179,7 +179,7 @@ internal static class WebRequest
var payload = new ModifyLineItemRequest
{
LineItemId = lineItemId,
UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency),
UserCountry = bot.GetUserCountryCode(),
};

var json = payload.ToJsonText();
Expand Down
117 changes: 61 additions & 56 deletions ASFEnhance/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ASFEnhance.Data.Plugin;
using ProtoBuf;
using SteamKit2;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Text;
Expand All @@ -21,6 +22,8 @@ internal static class Utils
/// </summary>
internal static PluginConfig Config { get; set; } = new();

internal static ConcurrentDictionary<Bot, string?> CustomUserCountry { get; } = [];

/// <summary>
/// 格式化返回文本
/// </summary>
Expand Down Expand Up @@ -65,12 +68,6 @@ internal static string FormatBotResponse(this Bot bot, string message, params ob
return bot.FormatBotResponse(string.Format(message, args));
}

[Obsolete("请使用 AppendLine")]
internal static StringBuilder AppendLineFormat(this StringBuilder sb, string format)
{
return sb.AppendLine(format);
}

internal static StringBuilder AppendLineFormat(this StringBuilder sb, string format, params object?[] args)
{
return sb.AppendLine(string.Format(format, args));
Expand Down Expand Up @@ -392,56 +389,64 @@ internal static string GetGifteeProfile(ulong accountId)
/// <summary>
/// 货币代码转国家代码
/// </summary>
/// <param name="currencyCode"></param>
/// <param name="bot"></param>
/// <returns></returns>
internal static string WalletCurrency2UserCountry(ECurrencyCode currencyCode) => currencyCode switch
internal static string GetUserCountryCode(this Bot bot)
{
ECurrencyCode.USD => "US",
ECurrencyCode.GBP => "GB",
ECurrencyCode.EUR => "EU",
ECurrencyCode.CHF => "CH",
ECurrencyCode.RUB => "RU",
ECurrencyCode.PLN => "PL",
ECurrencyCode.BRL => "BR",
ECurrencyCode.JPY => "JP",
ECurrencyCode.NOK => "NO",
ECurrencyCode.IDR => "ID",
ECurrencyCode.MYR => "MY",
ECurrencyCode.PHP => "PH",
ECurrencyCode.SGD => "SG",
ECurrencyCode.THB => "TH",
ECurrencyCode.VND => "VN",
ECurrencyCode.KRW => "KR",
ECurrencyCode.TRY => "TR",
ECurrencyCode.UAH => "UA",
ECurrencyCode.MXN => "MX",
ECurrencyCode.CAD => "CA",
ECurrencyCode.AUD => "CX",
ECurrencyCode.NZD => "CK",
ECurrencyCode.CNY => "CN",
ECurrencyCode.INR => "IN",
ECurrencyCode.CLP => "CL",
ECurrencyCode.PEN => "PE",
ECurrencyCode.COP => "CO",
ECurrencyCode.ZAR => "ZA",
ECurrencyCode.HKD => "HK",
ECurrencyCode.TWD => "TW",
ECurrencyCode.SAR => "SA",
ECurrencyCode.AED => "AE",
ECurrencyCode.ARS => "AR",
ECurrencyCode.ILS => "IL",
ECurrencyCode.BYN => "BY",
ECurrencyCode.KZT => "KZ",
ECurrencyCode.KWD => "KW",
ECurrencyCode.QAR => "QA",
ECurrencyCode.CRC => "CT",
ECurrencyCode.UYU => "UY",
ECurrencyCode.BGN => "BG",
ECurrencyCode.HRK => "HR",
ECurrencyCode.CZK => "CZ",
ECurrencyCode.DKK => "DK",
ECurrencyCode.HUF => "HU",
ECurrencyCode.RON => "RO",
_ => Langs.CountryCode,
};
if (CustomUserCountry.TryGetValue(bot, out var code) && !string.IsNullOrEmpty(code))
{
return code;
}

return bot.WalletCurrency switch
{
ECurrencyCode.USD => "US",
ECurrencyCode.GBP => "GB",
ECurrencyCode.EUR => "EU",
ECurrencyCode.CHF => "CH",
ECurrencyCode.RUB => "RU",
ECurrencyCode.PLN => "PL",
ECurrencyCode.BRL => "BR",
ECurrencyCode.JPY => "JP",
ECurrencyCode.NOK => "NO",
ECurrencyCode.IDR => "ID",
ECurrencyCode.MYR => "MY",
ECurrencyCode.PHP => "PH",
ECurrencyCode.SGD => "SG",
ECurrencyCode.THB => "TH",
ECurrencyCode.VND => "VN",
ECurrencyCode.KRW => "KR",
ECurrencyCode.TRY => "TR",
ECurrencyCode.UAH => "UA",
ECurrencyCode.MXN => "MX",
ECurrencyCode.CAD => "CA",
ECurrencyCode.AUD => "CX",
ECurrencyCode.NZD => "CK",
ECurrencyCode.CNY => "CN",
ECurrencyCode.INR => "IN",
ECurrencyCode.CLP => "CL",
ECurrencyCode.PEN => "PE",
ECurrencyCode.COP => "CO",
ECurrencyCode.ZAR => "ZA",
ECurrencyCode.HKD => "HK",
ECurrencyCode.TWD => "TW",
ECurrencyCode.SAR => "SA",
ECurrencyCode.AED => "AE",
ECurrencyCode.ARS => "AR",
ECurrencyCode.ILS => "IL",
ECurrencyCode.BYN => "BY",
ECurrencyCode.KZT => "KZ",
ECurrencyCode.KWD => "KW",
ECurrencyCode.QAR => "QA",
ECurrencyCode.CRC => "CT",
ECurrencyCode.UYU => "UY",
ECurrencyCode.BGN => "BG",
ECurrencyCode.HRK => "HR",
ECurrencyCode.CZK => "CZ",
ECurrencyCode.DKK => "DK",
ECurrencyCode.HUF => "HU",
ECurrencyCode.RON => "RO",
_ => Langs.CountryCode,
};
}
}

0 comments on commit 1fade00

Please sign in to comment.