Skip to content

Commit

Permalink
feat 修改IPC接口,适配新的购物车
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Mar 8, 2024
1 parent 6b75a4d commit 7154f4d
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 290 deletions.
3 changes: 1 addition & 2 deletions ASFEnhance/ASFEnhance.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
Expand Down Expand Up @@ -53,7 +53,6 @@

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(TargetPath)" DestinationFolder="..\ArchiSteamFarm\ArchiSteamFarm\bin\$(Configuration)\$(TargetFramework)\plugins\" SkipUnchangedFiles="true" />
<Copy SourceFiles="$(TargetPath)" DestinationFolder="C:\Users\chr11\Downloads\asf-5.5.3.4\plugins\" SkipUnchangedFiles="true" />
</Target>

</Project>
2 changes: 1 addition & 1 deletion ASFEnhance/Account/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ internal static async Task<bool> RemoveLicense(Bot bot, uint subId)
new(NotificationType.SteamTurnNotification,option.SteamTurnNotification),
};

var json = JsonConvert.SerializeObject(optionList);
var json = JsonConvert.SerializeObject(optionList, JsonOptions);

var data = new Dictionary<string, string>(11) {
{ "notificationpreferences", json },
Expand Down
12 changes: 6 additions & 6 deletions ASFEnhance/Cart/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ internal static class Command
{
if (item.PackageId > 0)
{
gameIds.Add(new MyGameId { Id = item.PackageId, Type = EGameIdType.PackageId });
gameIds.Add(new MyGameId { Id = item.PackageId.Value, Type = EGameIdType.PackageId });
}
else if (item.BundleId > 0)
{
gameIds.Add(new MyGameId { Id = item.BundleId, Type = EGameIdType.BundleId });
gameIds.Add(new MyGameId { Id = item.BundleId.Value, Type = EGameIdType.BundleId });
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ internal static class Command
}

var price = item.PriceWhenAdded?.FormattedAmount ?? "??";
if (!gameNameDict.TryGetValue(item.PackageId + item.BundleId, out var gameName))
if (!gameNameDict.TryGetValue(item.PackageId ?? item.BundleId ?? 0, out var gameName))
{
gameName = Langs.KeyNotFound;
}
Expand Down Expand Up @@ -212,11 +212,11 @@ internal static class Command
{
if (item.PackageId > 0)
{
ids.Add(new MyGameId { Id = item.PackageId, Type = EGameIdType.PackageId });
ids.Add(new MyGameId { Id = item.PackageId.Value, Type = EGameIdType.PackageId });
}
else if (item.BundleId > 0)
{
ids.Add(new MyGameId { Id = item.BundleId, Type = EGameIdType.BundleId });
ids.Add(new MyGameId { Id = item.BundleId.Value, Type = EGameIdType.BundleId });
}
}

Expand All @@ -243,7 +243,7 @@ internal static class Command
}

var price = item.PriceWhenAdded?.FormattedAmount ?? "??";
if (!gameNameDict.TryGetValue(item.PackageId + item.BundleId, out var gameName))
if (!gameNameDict.TryGetValue(item.PackageId ?? item.BundleId ?? 0, out var gameName))
{
gameName = Langs.KeyNotFound;
}
Expand Down
111 changes: 56 additions & 55 deletions ASFEnhance/Cart/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,15 @@ internal static class WebRequest
return response?.Content?.Response;
}

internal static Task<AddItemsToCartResponse?> AddItemToAccountCart(this Bot bot, SteamGameId gameId, bool isPrivate, AddItemsToCartRequest.GiftInfoData? giftInfo)
{
var myGameId = new MyGameId
{
Id = gameId.GameId,
Type = gameId.Type switch
{
ESteamGameIdType.App => EGameIdType.AppId,
ESteamGameIdType.Sub => EGameIdType.PackageId,
ESteamGameIdType.Bundle => EGameIdType.BundleId,
_ => throw new ArgumentOutOfRangeException(nameof(gameId.Type), gameId.Type, null)
}
};

return AddItemToAccountCart(bot, myGameId, isPrivate, giftInfo);
}

/// <summary>
/// 添加购物车项目
/// </summary>
/// <param name="bot"></param>
/// <param name="gameId"></param>
/// <param name="isPrivate"></param>
/// <param name="giftInfo"></param>
/// <returns></returns>
/// <exception cref="AccessTokenNullException"></exception>
internal static async Task<AddItemsToCartResponse?> AddItemToAccountCart(this Bot bot, MyGameId gameId, bool isPrivate, AddItemsToCartRequest.GiftInfoData? giftInfo)
{
var payload = new AddItemsToCartRequest
Expand Down Expand Up @@ -80,7 +72,7 @@ internal static class WebRequest
},
};

var json = JsonConvert.SerializeObject(payload);
var json = JsonConvert.SerializeObject(payload, JsonOptions);
var token = bot.AccessToken ?? throw new AccessTokenNullException();
var request = new Uri(SteamApiURL, $"/IAccountCartService/AddItemsToCart/v1/?access_token={token}");
var data = new Dictionary<string, string>
Expand All @@ -92,6 +84,15 @@ internal static class WebRequest
return response?.Content?.Response;
}

/// <summary>
/// 添加购物车项目
/// </summary>
/// <param name="bot"></param>
/// <param name="gameIds"></param>
/// <param name="isPrivate"></param>
/// <param name="giftInfo"></param>
/// <returns></returns>
/// <exception cref="AccessTokenNullException"></exception>
internal static async Task<AddItemsToCartResponse?> AddItemToAccountsCart(this Bot bot, List<MyGameId> gameIds, bool isPrivate, AddItemsToCartRequest.GiftInfoData? giftInfo)
{
var items = gameIds.Select(x => new AddItemsToCartRequest.ItemData
Expand Down Expand Up @@ -130,61 +131,61 @@ internal static class WebRequest
},
};

var json = JsonConvert.SerializeObject(payload);
var json = JsonConvert.SerializeObject(payload, JsonOptions);
var token = bot.AccessToken ?? throw new AccessTokenNullException();
var request = new Uri(SteamApiURL, $"/IAccountCartService/AddItemsToCart/v1/?access_token={token}");
var data = new Dictionary<string, string>
{
{ "input_json", json },
};
{
{ "input_json", json },
};

var response = await bot.ArchiWebHandler.UrlPostToJsonObject<AbstractResponse<AddItemsToCartResponse>>(request, data, SteamStoreURL).ConfigureAwait(false);
return response?.Content?.Response;
}

/// <summary>
/// 添加到购物车
/// 添加购物车项目
/// </summary>
/// <param name="bot"></param>
/// <param name="gameId"></param>
/// <param name="items"></param>
/// <returns></returns>
internal static async Task<bool?> AddCart(this Bot bot, SteamGameId gameId)
/// <exception cref="AccessTokenNullException"></exception>
internal static async Task<AddItemsToCartResponse?> AddItemsToAccountCart(this Bot bot, List<AddItemsToCartRequest.ItemData> items)
{
if (gameId.Type == ESteamGameIdType.Sub || gameId.Type == ESteamGameIdType.Bundle)
{
return await AddCart(bot, gameId.GameId, gameId.Type == ESteamGameIdType.Bundle).ConfigureAwait(false);
}
else
var payload = new AddItemsToCartRequest
{
return null;
}
}


/// <summary>
/// 添加到购物车
/// </summary>
/// <param name="bot"></param>
/// <param name="subId"></param>
/// <param name="isBundle"></param>
/// <returns></returns>
internal static async Task<bool?> AddCart(this Bot bot, uint subId, bool isBundle = false)
{
string type = isBundle ? "bundle" : "sub";

var request = new Uri(SteamStoreURL, "/cart/");
var referer = new Uri(SteamStoreURL, $"/{type}/{subId}");
Items = items,
Navdata = new AddItemsToCartRequest.NavdataData
{
Domain = "store.steampowered.com",
Controller = "default",
Method = "default",
SubMethod = "",
Feature = "spotlight",
Depth = 1,
CountryCode = Langs.CountryCode,
WebKey = 0,
IsClient = false,
CuratorData = new AddItemsToCartRequest.CuratorData
{
ClanId = null,
ListId = null,
},
IsLikelyBot = false,
IsUtm = false,
},
};

var data = new Dictionary<string, string>(5, StringComparer.Ordinal)
var json = JsonConvert.SerializeObject(payload, JsonOptions);
var token = bot.AccessToken ?? throw new AccessTokenNullException();
var request = new Uri(SteamApiURL, $"/IAccountCartService/AddItemsToCart/v1/?access_token={token}");
var data = new Dictionary<string, string>
{
{ "action", "add_to_cart" },
{ type + "id", subId.ToString() },
{ "originating_snr", "1_direct-navigation__" },
{ "input_json", json },
};

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

return response != null;
var response = await bot.ArchiWebHandler.UrlPostToJsonObject<AbstractResponse<AddItemsToCartResponse>>(request, data, SteamStoreURL).ConfigureAwait(false);
return response?.Content?.Response;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class NavdataData
///
/// </summary>
[JsonProperty("controller")]
public string ?Controller { get; set; }
public string? Controller { get; set; }
/// <summary>
///
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions ASFEnhance/Data/IAccountCartService/GetCartResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public sealed record CartLineItemData
///
/// </summary>
[JsonProperty("packageid")]
public uint PackageId { get; set; }
public uint ?PackageId { get; set; }
/// <summary>
///
/// </summary>
[JsonProperty("bundleid")]
public uint BundleId { get; set; }
public uint ?BundleId { get; set; }
/// <summary>
///
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions ASFEnhance/Event/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ internal static class Command

if (intGamsIDs.Count < categories) //不足11个游戏自动补齐
{
var defaultGames = new int[] { 1086940, 1922010, 1374480, 990080, 2344520, 2254740, 2411910, 1817230, 2242710, 1868140, 2194530 };
List<int> defaultGames = [1086940, 1922010, 1374480, 990080, 2344520, 2254740, 2411910, 1817230, 2242710, 1868140, 2194530];
while (intGamsIDs.Count < categories)
{
intGamsIDs.Add(defaultGames[intGamsIDs.Count]);
Expand Down Expand Up @@ -533,7 +533,7 @@ internal static class Command

if (intGamsIDs.Count < categories) //不足11个游戏自动补齐
{
var defaultGames = new int[] { 1086940, 1957780, 548430, 990080, 1260320, 668580, 1716740, 2138710, 1817230, 2322560, 1868140 };
List<int> defaultGames = [1086940, 1957780, 548430, 990080, 1260320, 668580, 1716740, 2138710, 1817230, 2322560, 1868140];
while (intGamsIDs.Count < categories)
{
intGamsIDs.Add(defaultGames[intGamsIDs.Count]);
Expand Down

0 comments on commit 7154f4d

Please sign in to comment.