Skip to content

Commit

Permalink
优化显示格式
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed May 28, 2022
1 parent 8b583a4 commit 35075e1
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 61 deletions.
4 changes: 2 additions & 2 deletions ASFEnhance/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;

[assembly: System.CLSCompliant(false)]
[assembly: AssemblyVersion("1.6.2.451")]
[assembly: AssemblyFileVersion("1.6.2.451")]
[assembly: AssemblyVersion("1.6.2.457")]
[assembly: AssemblyFileVersion("1.6.2.457")]

[assembly: AssemblyCopyright("Copyright © 2022 Chr_")]
[assembly: AssemblyProduct("ASFEnhance")]
Expand Down
43 changes: 29 additions & 14 deletions ASFEnhance/Cart/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,46 @@ internal static class Command
return bot.FormatBotResponse(Strings.BotNotConnected);
}

Dictionary<string, SteamGameID> gameIDs = FetchGameIDs(query, SteamGameIDType.App);
Dictionary<string, SteamGameID> itemIDs = FetchItemIDs(query);

if (itemIDs.Count < 2)
{
return bot.FormatBotResponse("参数错误, 最少需要两个参数");
}
else if (itemIDs.First().Value.GameType == SteamGameIDType.Error)
{
return bot.FormatBotResponse(string.Format(Langs.ArgumentNotInteger, "AppID"));
}

StringBuilder response = new();
response.AppendLine(Langs.MultipleLineResult);

foreach (KeyValuePair<string, SteamGameID> item in gameIDs)
uint appID = itemIDs.First().Value.GameID;
response.AppendLine(string.Format("AppID : {0}", appID));

bool first = true;
foreach (KeyValuePair<string, SteamGameID> item in itemIDs)
{
if (response.Length != 0) { response.AppendLine(); }
if (first)
{
first = false;
continue;
}

string input = item.Key;
SteamGameID gameID = item.Value;
SteamGameID itemID = item.Value;

switch (gameID.GameType)
if (itemID.GameType == SteamGameIDType.Item)
{
case SteamGameIDType.Sub:
case SteamGameIDType.Bundle:
bool? success = await WebRequest.AddCart(bot, gameID).ConfigureAwait(false);
response.AppendLine(bot.FormatBotResponse(string.Format(Strings.BotAddLicense, input, success == null ? Langs.CartNetworkError : (bool)success ? EResult.OK : EResult.Fail)));
break;
default:
response.AppendLine(bot.FormatBotResponse(string.Format(Langs.CartInvalidType, input)));
break;
EResult? success = await WebRequest.AddCart(bot, appID, itemID.GameID).ConfigureAwait(false);
response.AppendLine(bot.FormatBotResponse(string.Format("- {0}|{1} : {2}", appID, itemID.GameID, success == null ? Langs.CartNetworkError : success)));
}
else
{
response.AppendLine(bot.FormatBotResponse(string.Format("- {0} : {1}", input, string.Format(Langs.ArgumentNotInteger, "itemID"))));
}
}
return response.Length > 0 ? response.ToString() : null;
return response.ToString();
}

/// <summary>
Expand Down
20 changes: 11 additions & 9 deletions ASFEnhance/Cart/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

using ArchiSteamFarm.Core;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Data;
using ArchiSteamFarm.Web.Responses;
using ASFEnhance.Data;
using ASFEnhance.Localization;
using static ASFEnhance.Cart.Response;
using static ASFEnhance.Data.SteamGameID;
using static ASFEnhance.Utils;

namespace ASFEnhance.Cart
Expand Down Expand Up @@ -59,14 +61,11 @@ internal static class WebRequest
Uri request = new(SteamStoreURL, "/cart/");
Uri referer = new(SteamStoreURL, $"/{type}/{subID}");

Random random = new();

Dictionary<string, string> data = new(5, StringComparer.Ordinal)
{
{ "action", "add_to_cart" },
{ type + "id", subID.ToString() },
{ "originating_snr", "1_direct-navigation__" },
{ "snr", string.Format("{0}_{1}_{2}__{3}", 1, random.Next(1, 10), random.Next(1, 10), random.Next(100, 999)) }
};

HtmlDocumentResponse? response = await bot.ArchiWebHandler.UrlPostToHtmlDocumentWithSession(request, data: data, referer: referer).ConfigureAwait(false);
Expand All @@ -75,29 +74,32 @@ internal static class WebRequest
}

/// <summary>
/// 添加购物车
/// 添加购物车(游戏物品)
/// </summary>
/// <param name="bot"></param>
/// <param name="appID"></param>
/// <param name="classID"></param>
/// <returns></returns>
internal static async Task<bool?> AddCart(Bot bot, uint appID, uint classID)
internal static async Task<SteamKit2.EResult?> AddCart(Bot bot, uint appID, uint classID)
{
Uri request = new(SteamStoreURL, "/cart/addtocart");
Uri referer = new(SteamStoreURL, $"/itemstore/{appID}/detail/{classID}/");

Random random = new();

Dictionary<string, string> data = new(5, StringComparer.Ordinal)
{
{ "action", "add_to_cart" },
{ "microtxnappid", appID.ToString() },
{ "microtxnassetclassid", classID.ToString() },
};

HtmlDocumentResponse? response = await bot.ArchiWebHandler.UrlPostToHtmlDocumentWithSession(request, data: data, referer: referer).ConfigureAwait(false);
ObjectResponse<ResultResponse>? response = await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<ResultResponse>(request, data: data, referer: referer).ConfigureAwait(false);

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

return response.Content.Result;
}

/// <summary>
Expand Down
19 changes: 11 additions & 8 deletions ASFEnhance/Data/SteamGameID.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
namespace ASFEnhance.Data
using ASFEnhance.Localization;

namespace ASFEnhance.Data
{
internal sealed class SteamGameID
{
internal SteamGameIDType GameType { get; set; }
internal uint GameID { get; set; }

public SteamGameIDType GameType { get; set; }
public uint GameID { get; set; }


internal SteamGameID(SteamGameIDType type, uint gameID)
public SteamGameID(SteamGameIDType type, uint gameID)
{
GameType = type;
GameID = gameID;
}
}


public override string ToString()
{
return $"{GameType}/{GameID}";
}
}
internal enum SteamGameIDType
{
Error = 0,
Expand Down
1 change: 0 additions & 1 deletion ASFEnhance/Event/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ internal static class WebRequest

Dictionary<string, string> data = new(3, StringComparer.Ordinal)
{
{ "sessionid" , bot.GetBotSessionID() },
{ "authwgtoken", authwgToken },
{ "door_index", "5" },
};
Expand Down
8 changes: 4 additions & 4 deletions ASFEnhance/Localization/Langs.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ASFEnhance/Localization/Langs.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@
<value>Response is empty</value>
</data>
<data name="CartInvalidType" xml:space="preserve">
<value>{0}: Invalid type [SUB|BUNDLE]</value>
<value>- {0}: Invalid type [SUB|BUNDLE]</value>
</data>
<data name="CartIsEmpty" xml:space="preserve">
<value>Shopping cart is emoty</value>
</data>
<data name="CartItemInfo" xml:space="preserve">
<value>{0} {1} {2:F2}</value>
<value>- {0} {1} {2:F2}</value>
</data>
<data name="CartNetworkError" xml:space="preserve">
<value>Network Error</value>
Expand Down Expand Up @@ -253,10 +253,10 @@
<value>Set billing address: {0}</value>
</data>
<data name="StoreItem" xml:space="preserve">
<value>{0}/{1} {2} {3:F2} {4}</value>
<value> - {0}/{1} {2} {3:F2} {4}</value>
</data>
<data name="StoreItemHeader" xml:space="preserve">
<value>{0}/{1}: {2}</value>
<value>- {0}: {1}</value>
</data>
<data name="StorePageNotFound" xml:space="preserve">
<value>Store page not found</value>
Expand Down
8 changes: 4 additions & 4 deletions ASFEnhance/Localization/Langs.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@
<value>响应为空</value>
</data>
<data name="CartInvalidType" xml:space="preserve">
<value>{0}: 类型无效 [SUB|BUNDLE]</value>
<value>- {0}: 类型无效 [SUB|BUNDLE]</value>
</data>
<data name="CartIsEmpty" xml:space="preserve">
<value>购物车是空的</value>
</data>
<data name="CartItemInfo" xml:space="preserve">
<value>{0} {1} {2:F2}</value>
<value>- {0} {1} {2:F2}</value>
</data>
<data name="CartNetworkError" xml:space="preserve">
<value>网络错误</value>
Expand Down Expand Up @@ -253,10 +253,10 @@
<value>结算货币设置: {0}</value>
</data>
<data name="StoreItem" xml:space="preserve">
<value>{0}/{1} {2} {3:F2} {4}</value>
<value> - {0}/{1} {2} {3:F2} {4}</value>
</data>
<data name="StoreItemHeader" xml:space="preserve">
<value>{0}/{1}: {2}</value>
<value>- {0}: {1}</value>
</data>
<data name="StorePageNotFound" xml:space="preserve">
<value>未找到商店页</value>
Expand Down
8 changes: 4 additions & 4 deletions ASFEnhance/Localization/Langs.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@
<value>响应为空</value>
</data>
<data name="CartInvalidType" xml:space="preserve">
<value>{0}: 类型无效 [SUB|BUNDLE]</value>
<value>- {0}: 类型无效 [SUB|BUNDLE]</value>
</data>
<data name="CartIsEmpty" xml:space="preserve">
<value>购物车是空的</value>
</data>
<data name="CartItemInfo" xml:space="preserve">
<value>{0} {1} {2:F2}</value>
<value>- 0} {1} {2:F2}</value>
</data>
<data name="CartNetworkError" xml:space="preserve">
<value>网络错误</value>
Expand Down Expand Up @@ -253,10 +253,10 @@
<value>结算货币设置: {0}</value>
</data>
<data name="StoreItem" xml:space="preserve">
<value>{0}/{1} {2} {3:F2} {4}</value>
<value> - {0}/{1} {2} {3:F2} {4}</value>
</data>
<data name="StoreItemHeader" xml:space="preserve">
<value>{0}/{1}: {2}</value>
<value>- {0}: {1}</value>
</data>
<data name="StorePageNotFound" xml:space="preserve">
<value>未找到商店页</value>
Expand Down
20 changes: 12 additions & 8 deletions ASFEnhance/Store/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using ASFEnhance.Localization;
using SteamKit2;
using System.Text;
using static ASFEnhance.Store.Response;
using static ASFEnhance.Utils;
using static ASFEnhance.Data.SteamGameID;
using static ASFEnhance.Store.Response;

namespace ASFEnhance.Store
{
Expand All @@ -35,14 +36,18 @@ internal static class Command

string walletCurrency = bot.WalletCurrency != ECurrencyCode.Invalid ? bot.WalletCurrency.ToString() : Langs.WalletAreaUnknown;

if (CurrencyHelper.Currency2Symbol.ContainsKey(walletCurrency))
{
walletCurrency = CurrencyHelper.Currency2Symbol[walletCurrency];
}

Dictionary<string, SteamGameID> gameIDs = FetchGameIDs(query, SteamGameIDType.App);

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

foreach (KeyValuePair<string, SteamGameID> item in gameIDs)
{
if (response.Length != 0) { response.AppendLine(); }

string input = item.Key;
SteamGameID gameID = item.Value;

Expand All @@ -52,17 +57,15 @@ internal static class Command
case SteamGameIDType.Sub:
case SteamGameIDType.Bundle:

string type = gameID.GameType.ToString();

GameStorePageResponse? storeResponse = await WebRequest.GetStoreSubs(bot, gameID).ConfigureAwait(false);

if (storeResponse.SubDatas.Count == 0)
{
response.AppendLine(bot.FormatBotResponse(string.Format(Langs.StoreItemHeader, type, gameID, storeResponse.GameName)));
response.AppendLine(string.Format(Langs.StoreItemHeader, gameID, storeResponse.GameName));
}
else
{
response.AppendLine(bot.FormatBotResponse(string.Format(Langs.StoreItemHeader, type, gameID, storeResponse.GameName)));
response.AppendLine(string.Format(Langs.StoreItemHeader, gameID, storeResponse.GameName));

foreach (SingleSubData sub in storeResponse.SubDatas)
{
Expand All @@ -76,7 +79,8 @@ internal static class Command
break;
}
}
return response.Length > 0 ? response.ToString() : null;

return response.ToString();
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions ASFEnhance/Store/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public SingleSubData(bool bundle = false, uint subID = 0, string name = "", uint
this.Name = name;
this.Price = price;
}

public override string ToString()
{
return base.ToString();
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 35075e1

Please sign in to comment.