2022-12-15 18:46:37 +01:00
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
|
// |
|
|
|
|
|
// Copyright 2015-2022 Łukasz "JustArchi" Domeradzki
|
|
|
|
|
// Contact: JustArchi@JustArchi.net
|
|
|
|
|
// |
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
// |
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
// |
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ArchiSteamFarm.Core;
|
|
|
|
|
using ArchiSteamFarm.IPC.Responses;
|
2022-12-15 19:16:28 +01:00
|
|
|
using ArchiSteamFarm.OfficialPlugins.ItemsMatcher.Data;
|
2022-12-15 18:46:37 +01:00
|
|
|
using ArchiSteamFarm.Steam;
|
|
|
|
|
using ArchiSteamFarm.Steam.Data;
|
|
|
|
|
using ArchiSteamFarm.Steam.Storage;
|
|
|
|
|
using ArchiSteamFarm.Storage;
|
|
|
|
|
using ArchiSteamFarm.Web;
|
|
|
|
|
using ArchiSteamFarm.Web.Responses;
|
|
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.OfficialPlugins.ItemsMatcher;
|
|
|
|
|
|
|
|
|
|
internal static class Backend {
|
2022-12-17 17:23:20 +01:00
|
|
|
internal static async Task<BasicResponse?> AnnounceForListing(Bot bot, IReadOnlyList<Asset> inventory, IReadOnlyCollection<Asset.EType> acceptedMatchableTypes, string tradeToken, string? nickname = null, string? avatarHash = null) {
|
2022-12-15 18:46:37 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
|
|
if ((inventory == null) || (inventory.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(inventory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((acceptedMatchableTypes == null) || (acceptedMatchableTypes.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(acceptedMatchableTypes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(tradeToken)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(tradeToken));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tradeToken.Length != BotConfig.SteamTradeTokenLength) {
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(tradeToken));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri request = new(ArchiNet.URL, "/Api/Listing/Announce");
|
|
|
|
|
|
|
|
|
|
AnnouncementRequest data = new(ASF.GlobalDatabase?.Identifier ?? Guid.NewGuid(), bot.SteamID, tradeToken, inventory, acceptedMatchableTypes, bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.MatchEverything), ASF.GlobalConfig?.MaxTradeHoldDuration ?? GlobalConfig.DefaultMaxTradeHoldDuration, nickname, avatarHash);
|
|
|
|
|
|
2022-12-17 17:23:20 +01:00
|
|
|
return await bot.ArchiWebHandler.WebBrowser.UrlPost(request, data: data, requestOptions: WebBrowser.ERequestOptions.ReturnRedirections | WebBrowser.ERequestOptions.ReturnClientErrors).ConfigureAwait(false);
|
2022-12-15 18:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-23 15:31:14 +01:00
|
|
|
internal static async Task<(HttpStatusCode StatusCode, ImmutableHashSet<ListedUser> Users)?> GetListedUsersForMatching(Guid licenseID, Bot bot, IReadOnlyCollection<Asset> inventory, IReadOnlyCollection<Asset.EType> acceptedMatchableTypes) {
|
2022-12-15 18:46:37 +01:00
|
|
|
if (licenseID == Guid.Empty) {
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(licenseID));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
|
|
if ((inventory == null) || (inventory.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(inventory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((acceptedMatchableTypes == null) || (acceptedMatchableTypes.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(acceptedMatchableTypes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri request = new(ArchiNet.URL, "/Api/Listing/Inventories");
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> headers = new(1, StringComparer.Ordinal) {
|
|
|
|
|
{ "X-License-Key", licenseID.ToString("N") }
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-23 15:31:14 +01:00
|
|
|
InventoriesRequest data = new(ASF.GlobalDatabase?.Identifier ?? Guid.NewGuid(), bot.SteamID, inventory, acceptedMatchableTypes);
|
2022-12-15 18:46:37 +01:00
|
|
|
|
|
|
|
|
ObjectResponse<GenericResponse<ImmutableHashSet<ListedUser>>>? response = await bot.ArchiWebHandler.WebBrowser.UrlPostToJsonObject<GenericResponse<ImmutableHashSet<ListedUser>>, InventoriesRequest>(request, headers, data, requestOptions: WebBrowser.ERequestOptions.ReturnClientErrors | WebBrowser.ERequestOptions.AllowInvalidBodyOnErrors).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (response.StatusCode, response.Content?.Result ?? ImmutableHashSet<ListedUser>.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 16:34:42 +01:00
|
|
|
internal static async Task<BasicResponse?> HeartBeatForListing(Bot bot) {
|
2022-12-15 18:46:37 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
|
|
Uri request = new(ArchiNet.URL, "/Api/Listing/HeartBeat");
|
|
|
|
|
|
|
|
|
|
HeartBeatRequest data = new(ASF.GlobalDatabase?.Identifier ?? Guid.NewGuid(), bot.SteamID);
|
|
|
|
|
|
2022-12-23 16:34:42 +01:00
|
|
|
return await bot.ArchiWebHandler.WebBrowser.UrlPost(request, data: data, requestOptions: WebBrowser.ERequestOptions.ReturnRedirections | WebBrowser.ERequestOptions.ReturnClientErrors).ConfigureAwait(false);
|
2022-12-15 18:46:37 +01:00
|
|
|
}
|
|
|
|
|
}
|