Exemplo n.º 1
0
        public async Task <ActionResult <GenericResponse> > RenamePost(string botName, [FromBody] BotRenameRequest request)
        {
            if (string.IsNullOrEmpty(botName))
            {
                throw new ArgumentNullException(nameof(botName));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (Bot.Bots == null)
            {
                throw new InvalidOperationException(nameof(Bot.Bots));
            }

            if (string.IsNullOrEmpty(request.NewName) || !ASF.IsValidBotName(request.NewName) || Bot.Bots.ContainsKey(request.NewName))
            {
                return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(request.NewName)))));
            }

            if (!Bot.Bots.TryGetValue(botName, out Bot? bot))
            {
                return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botName))));
            }

            bool result = await bot.Rename(request.NewName).ConfigureAwait(false);

            return(Ok(new GenericResponse(result)));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <GenericResponse> > BotPost(string botNames, [FromBody] BotRequest request)
        {
            if (string.IsNullOrEmpty(botNames))
            {
                throw new ArgumentNullException(nameof(botNames));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (Bot.Bots == null)
            {
                throw new InvalidOperationException(nameof(Bot.Bots));
            }

            if (request.BotConfig == null)
            {
                return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(request.BotConfig)))));
            }

            (bool valid, string?errorMessage) = request.BotConfig.CheckValidation();

            if (!valid)
            {
                return(BadRequest(new GenericResponse(false, errorMessage)));
            }

            request.BotConfig.ShouldSerializeDefaultValues    = false;
            request.BotConfig.ShouldSerializeHelperProperties = false;
            request.BotConfig.ShouldSerializeSensitiveDetails = true;

            HashSet <string> bots = botNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet(Bot.BotsComparer);

            if (bots.Any(botName => !ASF.IsValidBotName(botName)))
            {
                return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(botNames)))));
            }

            Dictionary <string, bool> result = new Dictionary <string, bool>(bots.Count, Bot.BotsComparer);

            foreach (string botName in bots)
            {
                if (Bot.Bots.TryGetValue(botName, out Bot? bot))
                {
                    if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet)
                    {
                        request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin;
                    }

                    if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet)
                    {
                        request.BotConfig.DecryptedSteamPassword = bot.BotConfig.DecryptedSteamPassword;
                    }

                    if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet)
                    {
                        request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode;
                    }

                    if (bot.BotConfig.AdditionalProperties?.Count > 0)
                    {
                        request.BotConfig.AdditionalProperties ??= new Dictionary <string, JToken>(bot.BotConfig.AdditionalProperties.Count, bot.BotConfig.AdditionalProperties.Comparer);

                        foreach ((string key, JToken value) in bot.BotConfig.AdditionalProperties.Where(property => !request.BotConfig.AdditionalProperties.ContainsKey(property.Key)))
                        {
                            request.BotConfig.AdditionalProperties.Add(key, value);
                        }

                        request.BotConfig.AdditionalProperties.TrimExcess();
                    }
                }

                string filePath = Bot.GetFilePath(botName, Bot.EFileType.Config);

                if (string.IsNullOrEmpty(filePath))
                {
                    ASF.ArchiLogger.LogNullError(filePath);

                    return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(filePath)))));
                }

                result[botName] = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false);
            }

            return(Ok(new GenericResponse <IReadOnlyDictionary <string, bool> >(result.Values.All(value => value), result)));
        }