Beispiel #1
0
        private static async Task <int> Main()
        {
            var provider = GetServiceProvider();

            while (true)
            {
                try
                {
                    MenuLibrary.Clear();
                    MenuLibrary.WriteLineColor("The Rock Paper Scissors Game. Designed by Karyna Bilotska and Daniil Panasenko\n", ConsoleColor.DarkGreen);

                    MenuLibrary.PressAnyKey();

                    GetHttpClient();

                    var mainMemu = provider.GetRequiredService <MainMenu>();
                    await mainMemu.StartAsync();

                    return(0);
                }
                catch (Exception)
                {
                    ResponseLibrary.UnknownResponse();
                    MenuLibrary.PressAnyKey();
                }
            }
        }
        public async Task <bool> DoMoveAsync(Move move)
        {
            _logger.LogInformation("class GameMenu. DoMoveAsync()");

            var response = await _gameClient.DoMoveAsync(move);

            _logger.LogInformation("REQUEST: Sent the request to the server to do the move");

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

                _currentMove = move;
                PrintHeader();
                MenuLibrary.WriteColor($"\nYour move: ", ConsoleColor.White);
                MenuLibrary.WriteLineColor($"{_currentMove}", ConsoleColor.DarkCyan);
                return(true);
            }
            else if (response.StatusCode == HttpStatusCode.Conflict)
            {
                _logger.LogInformation("RESPONSE: Game is over (409)");

                await ResponseLibrary.GameFinishedResponseAsync(response);
            }
            else
            {
                _logger.LogInformation("RESPONSE: Unknown response");

                throw new HttpListenerException();
            }

            return(false);
        }
Beispiel #3
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 WaitPlayerAsync()
        {
            _logger.LogInformation("class GameStartMenu. WaitPlayerAsync()");

            while (true)
            {
                var response = await _gameClient.CheckSessionAsync();

                _logger.LogInformation("REQUEST: Sent the request to check the opponent");

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    _logger.LogInformation("RESPONSE: Opponent was found (200)");

                    var name = await response.Content.ReadAsStringAsync();

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

                    MenuLibrary.WriteLineColor($"\nYour opponent is {name}\n", ConsoleColor.Green);
                    Thread.Sleep(2000);

                    await _gameMenu.StartAsync();

                    return;
                }
                else if (response.StatusCode == HttpStatusCode.Conflict)
                {
                    _logger.LogInformation("RESPONSE: Game is over (409)");
                    await ResponseLibrary.GameFinishedResponseAsync(response);

                    return;
                }
                else if (response.StatusCode != HttpStatusCode.NotFound)
                {
                    _logger.LogInformation("RESPONSE: Unknown response");
                    throw new HttpListenerException();
                }
                _logger.LogInformation("RESPONSE: Opponnent was not found (404)");
                Thread.Sleep(200);
            }
        }
        public async Task <bool> CheckMoveAsync()
        {
            _logger.LogInformation("class GameMenu. CheckMoveAsync()");

            MenuLibrary.WriteLineColor("\nWait opponent...", ConsoleColor.DarkCyan);
            while (true)
            {
                var response = await _gameClient.CheckMoveAsync();

                _logger.LogInformation("REQUEST: Sent the request to check the move");

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    _logger.LogInformation("RESPONSE: Player did a move (200)");

                    var resultJson = await response.Content.ReadAsStringAsync();

                    var result = JsonSerializer.Deserialize <RoundResultDto>(resultJson);
                    PrintResult(result);
                    return(true);
                }
                else if (response.StatusCode == HttpStatusCode.Conflict)
                {
                    _logger.LogInformation("RESPONSE: Game is over (409)");

                    await ResponseLibrary.GameFinishedResponseAsync(response);

                    return(false);
                }
                else if (response.StatusCode != HttpStatusCode.NotFound)
                {
                    _logger.LogInformation("RESPONSE: Unknown response");

                    throw new HttpListenerException();
                }

                _logger.LogInformation("RESPONSE: The opponent did not do the move (404)");
                Thread.Sleep(200);
            }
        }
        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();
            }
        }