Example #1
0
        private async Task <bool> ResponseHandlerAsync(HttpResponseMessage response, string operation)
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                _logger.LogInformation("RESPONSE: Successful request (200)");

                ResponseLibrary.SuccessfullyOperation(operation);
                await _userMenu.StartAsync();

                return(true);
            }
            else if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                _logger.LogInformation("RESPONSE: Unable to Unauthorized (401)");
                await ResponseLibrary.RepeatOperationWithMessageAsync <string>(response);

                return(true);
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                _logger.LogInformation("RESPONSE: Bad request (400)");
                await ResponseLibrary.RepeatOperationWithMessageAsync <UserValidaionResponse>(response);

                return(false);
            }
            else
            {
                _logger.LogInformation("RESPONSE: Unknown response");
                throw new HttpListenerException();
            }
        }
        private async Task GameStartAsync(RoomType roomType, string roomId)
        {
            _logger.LogInformation("class GameStartMenu. GameStartAsync()");

            var response = await _gameClient.StartSessionAsync(roomType, roomId);

            _logger.LogInformation("REQUEST: Sent the request to the server to start the game");

            if (response.StatusCode == HttpStatusCode.OK)
            {
                _logger.LogInformation("RESPONSE: Game successfully started (200)");

                MenuLibrary.WriteLineColor("\nSuccessfully started game\n", ConsoleColor.Green);

                if (roomType == RoomType.Private && roomId == null)
                {
                    var roomNumber = await response.Content.ReadAsStringAsync();

                    roomNumber = JsonSerializer.Deserialize <string>(roomNumber);

                    MenuLibrary.WriteColor("Your room number: ", ConsoleColor.White);
                    MenuLibrary.WriteLineColor(roomNumber, ConsoleColor.Green);
                    MenuLibrary.WriteLineColor("Say it your friend to connection.", ConsoleColor.White);
                }
                MenuLibrary.WriteLineColor("\nWait for the opponent...\n", ConsoleColor.DarkCyan);

                await WaitPlayerAsync();
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest ||
                     response.StatusCode == HttpStatusCode.Conflict)
            {
                _logger.LogInformation("RESPONSE: Did not manage to start the game (400, 409)");
                await ResponseLibrary.RepeatOperationWithMessageAsync <string>(response);
            }
            else
            {
                _logger.LogInformation("RESPONSE: Unknown response");
                throw new HttpListenerException();
            }
        }