diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 2656383c2..9dc79e881 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -796,7 +796,15 @@ namespace ArchiSteamFarm { } return await ResponseAddLicense(steamID, args[1]).ConfigureAwait(false); - case "!API": + case "!TRANSFER": + if (args.Length > 3) { + return await ResponseTransfer(steamID, args[1], args[2], args[3]).ConfigureAwait(false); + } + if (args.Length > 2) { + return await ResponseTransfer(steamID, args[1], args[2]).ConfigureAwait(false); + } + return await ResponseTransfer(steamID).ConfigureAwait(false); + case "!API": return ResponseAPI(steamID, args[1]); case "!BL": return await ResponseBlacklist(steamID, args[1]).ConfigureAwait(false); @@ -2293,7 +2301,7 @@ namespace ArchiSteamFarm { return responses.Count > 0 ? string.Join("", responses) : null; } - private async Task ResponseAdvancedRedeem(ulong steamID, string options, string keys) { + private async Task ResponseAdvancedRedeem(ulong steamID, string options, string keys) { if ((steamID == 0) || string.IsNullOrEmpty(options) || string.IsNullOrEmpty(keys)) { ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(options) + " || " + nameof(keys)); return null; @@ -3912,7 +3920,156 @@ namespace ArchiSteamFarm { return responses.Count > 0 ? string.Join("", responses) : null; } - private string ResponseUnknown(ulong steamID) { + private static async Task ResponseTransfer(ulong steamID) { + // modifie message / remove? + return "To less arguments"; + } + + private async Task ResponseTransfer(ulong steamID, string mode, string botNameTo) { + if ((steamID == 0) || string.IsNullOrEmpty(botNameTo) || string.IsNullOrEmpty(mode)) { + ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(mode) + " || " + nameof(botNameTo)); + return null; + } + + if (!IsMaster(steamID)) { + return null; + } + + if (!IsConnectedAndLoggedOn) { + return FormatBotResponse(Strings.BotNotConnected); + } + + // Not sure if I need to keep it, guess it's better. + if (!LootingAllowed) { + return FormatBotResponse(Strings.BotLootingTemporarilyDisabled); + } + + HashSet botsT = GetBots(botNameTo); + if ((botsT == null) || (botsT.Count == 0)) { + return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNameTo)) : null; + } + Bot botT = botsT.First(); + ulong targetSteamMasterID = botT.SteamID; + + if (targetSteamMasterID == SteamID) { + return FormatBotResponse(Strings.BotLootingYourself); + } + + if (!LootingSemaphore.Wait(0)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + + try { + HashSet tmp = new HashSet(); + string[] modes = mode.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + foreach (string singleMode in modes) { + switch (singleMode.ToUpper()) { + case "C": // Not sure about shortcuts. + case "CARD": + tmp.Add(Steam.Item.EType.TradingCard); + break; + case "F": + case "FOIL": + tmp.Add(Steam.Item.EType.FoilTradingCard); + break; + case "B": + case "BOOSTER": + tmp.Add(Steam.Item.EType.BoosterPack); + break; + case "E": + case "EMOTICON": + tmp.Add(Steam.Item.EType.Emoticon); + break; + case "BA": + case "BACKGROUND": + tmp.Add(Steam.Item.EType.ProfileBackground); + break; + case "U": + case "UNKNOWN": + tmp.Add(Steam.Item.EType.Unknown); + break; + case "G": + case "GEMS": + tmp.Add(Steam.Item.EType.SteamGems); + break; + case "EV": + case "EVERYTHING": + // Needs to be kept up to date, or is there an easy way for all types? + tmp.Add(Steam.Item.EType.TradingCard); + tmp.Add(Steam.Item.EType.FoilTradingCard); + tmp.Add(Steam.Item.EType.BoosterPack); + tmp.Add(Steam.Item.EType.Emoticon); + tmp.Add(Steam.Item.EType.ProfileBackground); + tmp.Add(Steam.Item.EType.Unknown); + tmp.Add(Steam.Item.EType.SteamGems); + break; + default: + // adjust error message + return "Unknown mode " + singleMode + "!"; + } + } + + HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, tmp).ConfigureAwait(false); + if ((inventory == null) || (inventory.Count == 0)) { + return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); + } + + if (!await ArchiWebHandler.MarkSentTrades().ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + + if (!await ArchiWebHandler.SendTradeOffer(inventory, targetSteamMasterID, BotConfig.SteamTradeToken).ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + + if (HasMobileAuthenticator) { + // Give Steam network some time to generate confirmations + await Task.Delay(3000).ConfigureAwait(false); + if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetSteamMasterID).ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + } + } finally { + LootingSemaphore.Release(); + } + + return FormatBotResponse(Strings.BotLootingSuccess); + } + + + private static async Task ResponseTransfer(ulong steamID, string botNameFrom, string mode, string botNameTo) { + //standard procedure adapted from loot + if ((steamID == 0) || string.IsNullOrEmpty(botNameFrom) || string.IsNullOrEmpty(botNameTo) || string.IsNullOrEmpty(mode)) { + ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNameFrom) + " || " + nameof(mode) + " || " + nameof(botNameTo)); + return null; + } + + HashSet botsF = GetBots(botNameFrom); + if ((botsF == null) || (botsF.Count == 0)) { + return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNameFrom)) : null; + } + + IEnumerable> tasks = botsF.Select(bot => bot.ResponseTransfer(steamID, mode, botNameTo)); + ICollection results; + + switch (Program.GlobalConfig.OptimizationMode) { + case GlobalConfig.EOptimizationMode.MinMemoryUsage: + results = new List(botsF.Count); + foreach (Task task in tasks) { + results.Add(await task.ConfigureAwait(false)); + } + + break; + default: + results = await Task.WhenAll(tasks).ConfigureAwait(false); + break; + } + + List responses = new List(results.Where(result => !string.IsNullOrEmpty(result))); + return responses.Count > 0 ? string.Join("", responses) : null; + } + + private string ResponseUnknown(ulong steamID) { if (steamID != 0) { return IsOperator(steamID) ? FormatBotResponse(Strings.UnknownCommand) : null; }