2022-12-17 17:27:41 +00: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.ComponentModel;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using ArchiSteamFarm.Core;
|
|
|
|
using ArchiSteamFarm.Localization;
|
|
|
|
using ArchiSteamFarm.Steam;
|
|
|
|
using ArchiSteamFarm.Steam.Storage;
|
|
|
|
using SteamKit2;
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.OfficialPlugins.ItemsMatcher;
|
|
|
|
|
|
|
|
internal static class Commands {
|
|
|
|
internal static async Task<string?> OnBotCommand(Bot bot, EAccess access, string[] args, ulong steamID = 0) {
|
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((args == null) || (args.Length == 0)) {
|
|
|
|
throw new ArgumentNullException(nameof(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (args.Length) {
|
|
|
|
case 1:
|
|
|
|
switch (args[0].ToUpperInvariant()) {
|
|
|
|
case "MATCH":
|
|
|
|
return ResponseMatch(access, bot);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (args[0].ToUpperInvariant()) {
|
|
|
|
case "MATCH":
|
|
|
|
return await ResponseMatch(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string? ResponseMatch(EAccess access, Bot bot) {
|
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
if (access < EAccess.Master) {
|
2022-12-17 19:36:58 +00:00
|
|
|
return access > EAccess.None ? bot.Commands.FormatBotResponse(Strings.ErrorAccessDenied) : null;
|
2022-12-17 17:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ASF.GlobalConfig?.LicenseID == null) || (ASF.GlobalConfig.LicenseID == Guid.Empty)) {
|
2022-12-17 19:36:58 +00:00
|
|
|
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(ASF.GlobalConfig.LicenseID)));
|
2022-12-17 17:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.MatchActively) || !ItemsMatcherPlugin.RemoteCommunications.TryGetValue(bot, out RemoteCommunication? remoteCommunication)) {
|
2022-12-17 19:36:58 +00:00
|
|
|
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(BotConfig.ETradingPreferences.MatchActively)));
|
2022-12-17 17:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteCommunication.TriggerMatchActivelyEarlier();
|
|
|
|
|
2022-12-17 19:36:58 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.Done);
|
2022-12-17 17:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task<string?> ResponseMatch(EAccess access, string botNames, ulong steamID = 0) {
|
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(botNames)) {
|
|
|
|
throw new ArgumentNullException(nameof(botNames));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
HashSet<Bot>? bots = Bot.GetBots(botNames);
|
|
|
|
|
|
|
|
if ((bots == null) || (bots.Count == 0)) {
|
|
|
|
return access >= EAccess.Owner ? Steam.Interaction.Commands.FormatStaticResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames)) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
IList<string?> results = await Utilities.InParallel(bots.Select(bot => Task.Run(() => ResponseMatch(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot)))).ConfigureAwait(false);
|
|
|
|
|
|
|
|
List<string> responses = new(results.Where(static result => !string.IsNullOrEmpty(result))!);
|
|
|
|
|
|
|
|
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
|
|
|
|
}
|
|
|
|
}
|