예제 #1
0
        public async Task <ActionResult <string> > SetUsername([FromForm] string playerServiceId,
                                                               [FromForm] string username)
        {
            Console.WriteLine($"{nameof(playerServiceId)} {playerServiceId}");
            if (string.IsNullOrEmpty(playerServiceId))
            {
                return(BadRequest());
            }

            if (string.IsNullOrEmpty(username))
            {
                return(BadRequest());
            }

            UsernameValidationResultEnum validationResult = await usernameChangingService
                                                            .ChangeUsername(playerServiceId, username);

            UsernameValidationResult usernameValidationResult = new UsernameValidationResult()
            {
                UsernameValidationResultEnum = validationResult
            };
            string result = usernameValidationResult.SerializeToBase64String();

            if (validationResult != UsernameValidationResultEnum.Ok)
            {
                Console.WriteLine("Ник не обновлён.");
                return(BadRequest(result));
            }

            Console.WriteLine("Всё нормально");
            return(Ok(result));
        }
예제 #2
0
        public async Task <UsernameValidationResultEnum> ChangesUsernameAsync(string username)
        {
            if (!PlayerIdStorage.TryGetServiceId(out string playerServiceId))
            {
                log.Warn("Не удалось получить id игрока");
                return(UsernameValidationResultEnum.OtherError);
            }

            HttpClient          httpClient = new HttpClient();
            HttpResponseMessage response;

            using (MultipartFormDataContent formData = new MultipartFormDataContent())
            {
                formData.Add(new StringContent(playerServiceId), nameof(playerServiceId));
                formData.Add(new StringContent(username), nameof(username));
                response = await httpClient.PostAsync(NetworkGlobals.ChangeUsernameUrl, formData);
            }


            if (!response.IsSuccessStatusCode)
            {
                log.Warn("Статус ответа " + response.StatusCode);
            }

            UsernameValidationResultEnum result = UsernameValidationResultEnum.OtherError;

            try
            {
                string base64String = await response.Content.ReadAsStringAsync();

                byte[] data = Convert.FromBase64String(base64String);
                result = ZeroFormatterSerializer.Deserialize <UsernameValidationResult>(data).UsernameValidationResultEnum;
            }
            catch (Exception e)
            {
                log.Error(e.Message + " " + e.StackTrace);
            }

            return(result);
        }