Skip to content

Commit 944e0d1

Browse files
committed
feat 新增市场相关命令
1 parent 15e6fd4 commit 944e0d1

6 files changed

Lines changed: 270 additions & 32 deletions

File tree

ASFEnhance/ASFEnhance.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@ public Task OnLoaded()
367367
"FAMILYGROUP" when access >= EAccess.Master =>
368368
Family.Command.ResponseFamilyGroup(bot),
369369

370+
//Market
371+
"MARKETORDER" or
372+
"MARKETORDERS" when access >= EAccess.Operator =>
373+
Market.Command.ResponseGetMarketOrders(bot),
374+
370375
//Friend
371376
"DELETEALLFRIEND" when access >= EAccess.Master =>
372377
Friend.Command.ResponseDeleteAllFriend(bot),
@@ -809,12 +814,19 @@ public Task OnLoaded()
809814
"MARKETINFO" when access >= EAccess.Operator =>
810815
Market.Command.ResponseGetMarketInfo(bot, args[1]),
811816

812-
//"EDITFAMILYGROUP" or
813-
//"EFG" when argLength > 2 && access >= EAccess.Master =>
814-
// Family.Command.ResponseFamilyGroupName(args[1], Utilities.GetArgsAsText(message, 2)),
815-
//"EDITFAMILYGROUP" or
816-
//"EFG" when access >= EAccess.Master =>
817-
// Family.Command.ResponseFamilyGroupName(bot, args[1]),
817+
"MARKETORDER" or
818+
"MARKETORDERS" when argLength > 2 && access >= EAccess.Operator =>
819+
Market.Command.ResponseGetMarketOrders(Utilities.GetArgsAsText(args, 1, ",")),
820+
821+
"MARKETBUY" when argLength == 5 && access >= EAccess.Operator =>
822+
Market.Command.ResponseBuyMarketItem(args[1], args[2], args[3], args[4]),
823+
"MARKETBUY" when argLength == 4 && access >= EAccess.Operator =>
824+
Market.Command.ResponseBuyMarketItem(bot, args[1], args[2], args[3]),
825+
826+
"MARKETCANCEL" when argLength > 2 && access >= EAccess.Operator =>
827+
Market.Command.ResponseCancelOrder(args[1], Utilities.GetArgsAsText(args, 2, ",")),
828+
"MARKETCANCEL" when access >= EAccess.Operator =>
829+
Market.Command.ResponseCancelOrder(bot, args[1]),
818830

819831
//Friend
820832
"ADDBOTFRIEND" or
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ASFEnhance.Data.WebApi;
2+
3+
/// <summary>
4+
///
5+
/// </summary>
6+
/// <param name="Name"></param>
7+
/// <param name="Price"></param>
8+
/// <param name="Amount"></param>
9+
/// <param name="OrderId"></param>
10+
public sealed record OrderInfoData(string? Name, string? Price, string? Amount, string? OrderId);
11+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
namespace ASFEnhance.Data.WebApi;
22

33
public sealed record SellInfoData(decimal Price, int Amount, string? Summary);
4+

ASFEnhance/Market/Command.cs

Lines changed: 167 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ internal static class Command
1111
/// 获取市场商品信息
1212
/// </summary>
1313
/// <param name="bot"></param>
14-
/// <param name="query"></param>
14+
/// <param name="marketUrl"></param>
1515
/// <returns></returns>
16-
internal static async Task<string?> ResponseGetMarketInfo(Bot bot, string query)
16+
internal static async Task<string?> ResponseGetMarketInfo(Bot bot, string marketUrl)
1717
{
1818
if (!bot.IsConnectedAndLoggedOn)
1919
{
2020
return bot.FormatBotResponse(Strings.BotNotConnected);
2121
}
2222

23-
var match = RegexUtils.MatchMarketUrl().Match(query);
23+
var match = RegexUtils.MatchMarketUrl().Match(marketUrl);
2424
if (!match.Success)
2525
{
2626
return bot.FormatBotResponse("市场链接似乎无效");
@@ -36,7 +36,7 @@ internal static class Command
3636
return bot.FormatBotResponse(Langs.NetworkError);
3737
}
3838

39-
var detail = await WebRequest.GetMarketDetail(bot, baseInfo.ItemId, bot.GetUserCountryCode(), bot.WalletCurrency).ConfigureAwait(false);
39+
var detail = await WebRequest.GetMarketPriceInfo(bot, baseInfo.ItemId, bot.GetUserCountryCode(), bot.WalletCurrency).ConfigureAwait(false);
4040

4141
var sb = new StringBuilder();
4242

@@ -63,10 +63,10 @@ internal static class Command
6363
/// 获取市场商品信息 (多个Bot)
6464
/// </summary>
6565
/// <param name="botNames"></param>
66-
/// <param name="query"></param>
66+
/// <param name="marketUrl"></param>
6767
/// <returns></returns>
6868
/// <exception cref="ArgumentNullException"></exception>
69-
internal static async Task<string?> ResponseGetMarketInfo(string botNames, string query)
69+
internal static async Task<string?> ResponseGetMarketInfo(string botNames, string marketUrl)
7070
{
7171
if (string.IsNullOrEmpty(botNames))
7272
{
@@ -80,7 +80,7 @@ internal static class Command
8080
return FormatStaticResponse(Strings.BotNotFound, botNames);
8181
}
8282

83-
var results = await Utilities.InParallel(bots.Select(x => ResponseGetMarketInfo(x, query))).ConfigureAwait(false);
83+
var results = await Utilities.InParallel(bots.Select(x => ResponseGetMarketInfo(x, marketUrl))).ConfigureAwait(false);
8484
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));
8585

8686
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
@@ -90,21 +90,33 @@ internal static class Command
9090
/// 创建市场求购订单
9191
/// </summary>
9292
/// <param name="bot"></param>
93-
/// <param name="query"></param>
93+
/// <param name="marketUrl"></param>
94+
/// <param name="strPrice"></param>
95+
/// <param name="strAmount"></param>
9496
/// <returns></returns>
95-
internal static async Task<string?> ResponseBuyMarketItem(Bot bot, string query)
97+
internal static async Task<string?> ResponseBuyMarketItem(Bot bot, string marketUrl, string strPrice, string strAmount)
9698
{
9799
if (!bot.IsConnectedAndLoggedOn)
98100
{
99101
return bot.FormatBotResponse(Strings.BotNotConnected);
100102
}
101103

102-
var match = RegexUtils.MatchMarketUrl().Match(query);
104+
var match = RegexUtils.MatchMarketUrl().Match(marketUrl);
103105
if (!match.Success)
104106
{
105107
return bot.FormatBotResponse("市场链接似乎无效");
106108
}
107109

110+
if (!decimal.TryParse(strPrice, out var price) || price <= 0)
111+
{
112+
return bot.FormatBotResponse("求购价格无效");
113+
}
114+
115+
if (!int.TryParse(strAmount, out var amount) || amount <= 0)
116+
{
117+
return bot.FormatBotResponse("求购数量无效");
118+
}
119+
108120
var appId = match.Groups[1].Value;
109121
var marketHash = match.Groups[2].Value;
110122

@@ -115,36 +127,98 @@ internal static class Command
115127
return bot.FormatBotResponse(Langs.NetworkError);
116128
}
117129

118-
var detail = await WebRequest.GetMarketDetail(bot, baseInfo.ItemId, bot.GetUserCountryCode(), bot.WalletCurrency).ConfigureAwait(false);
130+
var buyResponse = await WebRequest.CreateBuyOrder(bot, appId, marketHash, price, amount, bot.WalletCurrency, null).ConfigureAwait(false);
131+
132+
if (buyResponse == null)
133+
{
134+
return bot.FormatBotResponse(Langs.NetworkError);
135+
}
136+
137+
if (buyResponse.Success == SteamKit2.EResult.Pending)
138+
{
139+
return bot.FormatBotResponse("创建求购订单需要验证令牌。请在移动设备上确认订单, 或者使用 2FAOK");
140+
}
119141

120142
var sb = new StringBuilder();
121143

122144
sb.AppendLine(Langs.MultipleLineResult);
123145
sb.AppendLineFormat("物品名称: {0}", baseInfo.Name);
124-
sb.AppendLineFormat("物品Id: {0} - {1}", baseInfo.AppId, baseInfo.ItemId);
146+
sb.AppendLineFormat("求购Id: {0}", buyResponse.BuyOrderId);
125147

126-
if (detail?.SellInfoList != null)
148+
return bot.FormatBotResponse(sb.ToString());
149+
}
150+
151+
/// <summary>
152+
/// 创建市场求购订单 (多个Bot)
153+
/// </summary>
154+
/// <param name="botNames"></param>
155+
/// <param name="marketUrl"></param>
156+
/// <param name="strPrice"></param>
157+
/// <param name="strAmount"></param>
158+
/// <returns></returns>
159+
/// <exception cref="ArgumentNullException"></exception>
160+
internal static async Task<string?> ResponseBuyMarketItem(string botNames, string marketUrl, string strPrice, string strAmount)
161+
{
162+
if (string.IsNullOrEmpty(botNames))
127163
{
128-
var price = detail.SellInfoList.FirstOrDefault();
129-
sb.AppendLineFormat("最低出售价: {1}{0}{2}", price?.Price, detail.PricePrefix, detail.PriceSuffix);
164+
throw new ArgumentNullException(nameof(botNames));
130165
}
131-
if (detail?.BuyInfoList != null)
166+
167+
var bots = Bot.GetBots(botNames);
168+
169+
if (bots == null || bots.Count == 0)
132170
{
133-
var price = detail.BuyInfoList.FirstOrDefault();
134-
sb.AppendLineFormat("最高求购价: {1}{0}{2}", price?.Price, detail.PricePrefix, detail.PriceSuffix);
171+
return FormatStaticResponse(Strings.BotNotFound, botNames);
172+
}
173+
174+
var results = await Utilities.InParallel(bots.Select(x => ResponseBuyMarketItem(x, marketUrl, strPrice, strAmount))).ConfigureAwait(false);
175+
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));
176+
177+
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
178+
}
179+
180+
/// <summary>
181+
/// 获取求购订单列表
182+
/// </summary>
183+
/// <param name="bot"></param>
184+
/// <returns></returns>
185+
internal static async Task<string?> ResponseGetMarketOrders(Bot bot)
186+
{
187+
if (!bot.IsConnectedAndLoggedOn)
188+
{
189+
return bot.FormatBotResponse(Strings.BotNotConnected);
190+
}
191+
192+
var results = await WebRequest.GetOrderList(bot).ConfigureAwait(false);
193+
194+
if (results == null)
195+
{
196+
return bot.FormatBotResponse(Langs.NetworkError);
197+
}
198+
199+
if (results.Count == 0)
200+
{
201+
return bot.FormatBotResponse("市场求购列表为空");
202+
}
203+
204+
var sb = new StringBuilder();
205+
sb.AppendLine(Langs.MultipleLineResult);
206+
sb.AppendLine("求购订单列表:");
207+
foreach (var item in results)
208+
{
209+
sb.AppendLineFormat("{0} : {1} {2} * {3}", item.OrderId, item.Name, item.Price, item.Amount);
135210
}
136211

137212
return bot.FormatBotResponse(sb.ToString());
138213
}
139214

140215
/// <summary>
141-
/// 创建市场求购订单 (多个Bot)
216+
/// 获取求购订单列表 (多个Bot)
142217
/// </summary>
143218
/// <param name="botNames"></param>
144-
/// <param name="query"></param>
145219
/// <returns></returns>
146220
/// <exception cref="ArgumentNullException"></exception>
147-
internal static async Task<string?> ResponseBuyMarketItem(string botNames, string query)
221+
internal static async Task<string?> ResponseGetMarketOrders(string botNames)
148222
{
149223
if (string.IsNullOrEmpty(botNames))
150224
{
@@ -158,13 +232,84 @@ internal static class Command
158232
return FormatStaticResponse(Strings.BotNotFound, botNames);
159233
}
160234

161-
var results = await Utilities.InParallel(bots.Select(x => ResponseBuyMarketItem(x, query))).ConfigureAwait(false);
235+
var results = await Utilities.InParallel(bots.Select(ResponseGetMarketOrders)).ConfigureAwait(false);
162236
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));
163237

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

241+
/// <summary>
242+
/// 取消市场求购订单
243+
/// </summary>
244+
/// <param name="bot"></param>
245+
/// <param name="query"></param>
246+
/// <returns></returns>
247+
internal static async Task<string?> ResponseCancelOrder(Bot bot, string query)
248+
{
249+
if (!bot.IsConnectedAndLoggedOn)
250+
{
251+
return bot.FormatBotResponse(Strings.BotNotConnected);
252+
}
253+
254+
var entities = query.Split(SeparatorDot, StringSplitOptions.RemoveEmptyEntries);
255+
256+
var sb = new StringBuilder();
257+
258+
if (entities.Length > 1)
259+
{
260+
sb.AppendLine(Langs.MultipleLineResult);
261+
}
262+
263+
foreach (var entity in entities)
264+
{
265+
if (!long.TryParse(entity, out var orderId) || (orderId == 0))
266+
{
267+
var response = await WebRequest.CancelBuyOrder(bot, orderId).ConfigureAwait(false);
268+
269+
if (response?.Success == SteamKit2.EResult.OK)
270+
{
271+
sb.AppendLineFormat(Langs.CookieItem, entity, "求购取消成功");
272+
}
273+
else
274+
{
275+
sb.AppendLineFormat(Langs.CookieItem, entity, "求购取消失败");
276+
}
277+
278+
continue;
279+
}
280+
else
281+
{
282+
sb.AppendLineFormat(Langs.CookieItem, entity, "输入无效");
283+
}
284+
}
285+
286+
return bot.FormatBotResponse(sb.ToString());
287+
}
288+
289+
/// <summary>
290+
/// 取消市场求购订单 (多个Bot)
291+
/// </summary>
292+
/// <param name="botNames"></param>
293+
/// <param name="query"></param>
294+
/// <returns></returns>
295+
/// <exception cref="ArgumentNullException"></exception>
296+
internal static async Task<string?> ResponseCancelOrder(string botNames, string query)
297+
{
298+
if (string.IsNullOrEmpty(botNames))
299+
{
300+
throw new ArgumentNullException(nameof(botNames));
301+
}
302+
303+
var bots = Bot.GetBots(botNames);
167304

305+
if (bots == null || bots.Count == 0)
306+
{
307+
return FormatStaticResponse(Strings.BotNotFound, botNames);
308+
}
168309

310+
var results = await Utilities.InParallel(bots.Select(x => ResponseCancelOrder(x, query))).ConfigureAwait(false);
311+
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));
169312

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

0 commit comments

Comments
 (0)