private async Task GetResults()
        {
            _logger.LogInformation("class PlayerStatisticsMenu. GetResults()");

            var response = await _statisticClient.GetUserResultsAsync();

            _logger.LogInformation("REQUEST: Sent the request to get the statistics");

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

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

                var results = JsonSerializer.Deserialize <ResultsDto>(content);

                MenuLibrary.WriteLineColor("\nYour results:\n", ConsoleColor.Yellow);

                MenuLibrary.WriteColor("Wins: ", ConsoleColor.Yellow);
                MenuLibrary.WriteLineColor(results.WinCount.ToString(), ConsoleColor.White);

                MenuLibrary.WriteColor("Draws: ", ConsoleColor.Yellow);
                MenuLibrary.WriteLineColor(results.DrawCount.ToString(), ConsoleColor.White);

                MenuLibrary.WriteColor("Losses: ", ConsoleColor.Yellow);
                MenuLibrary.WriteLineColor(results.LossCount.ToString(), ConsoleColor.White);

                MenuLibrary.PressAnyKey();
            }
            else
            {
                _logger.LogInformation("RESPONSE: Unknown response");
                throw new HttpListenerException();
            }
        }
        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);
        }
        private async Task GetTotalTime()
        {
            _logger.LogInformation("class PlayerStatisticsMenu. GetTotalTime()");

            var response = await _statisticClient.GetUserGameTimeAsync();

            _logger.LogInformation("REQUEST: Sent the request to get the statistics");

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

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

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

                MenuLibrary.WriteColor("\nTotal time in the game: ", ConsoleColor.Yellow);
                MenuLibrary.WriteLineColor(time, ConsoleColor.White);

                MenuLibrary.PressAnyKey();
            }
            else
            {
                _logger.LogInformation("RESPONSE: Unknown response");
                throw new HttpListenerException();
            }
        }
Ejemplo n.º 4
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 void PrintHeader()
 {
     MenuLibrary.Clear();
     MenuLibrary.WriteLineColor("\nGame\n", ConsoleColor.Yellow);
     MenuLibrary.WriteColor("Results: ", ConsoleColor.White);
     MenuLibrary.WriteColor($"{_sessionResults.WinCount} : ", ConsoleColor.Green);
     MenuLibrary.WriteColor($"{_sessionResults.DrawCount} : ", ConsoleColor.Yellow);
     MenuLibrary.WriteLineColor($"{_sessionResults.LossesCount}", ConsoleColor.Red);
 }
        private void ShowRate(List <UserResultDto> results)
        {
            MenuLibrary.WriteLineColor("", ConsoleColor.White);
            int place = 1;

            foreach (var result in results)
            {
                MenuLibrary.WriteColor($"{place}. {result.Login}: ", ConsoleColor.White);
                MenuLibrary.WriteLineColor(result.Result, ConsoleColor.Yellow);
                place++;
            }
            MenuLibrary.PressAnyKey();
        }
Ejemplo n.º 7
0
        private void ShowRate(List <ResultByTimeDto> results)
        {
            MenuLibrary.WriteLineColor("", ConsoleColor.White);
            int place = 1;

            foreach (var result in results)
            {
                MenuLibrary.WriteColor($"{place}. {result.Time} -> ", ConsoleColor.White);
                MenuLibrary.WriteColor(result.WinCount.ToString(), ConsoleColor.Green);
                MenuLibrary.WriteColor(" : ", ConsoleColor.White);
                MenuLibrary.WriteLineColor(result.LossCount.ToString(), ConsoleColor.Red);
                place++;
            }
            MenuLibrary.PressAnyKey();
        }
        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();
            }
        }
        public void PrintResult(RoundResultDto result)
        {
            string       resultString = "";
            ConsoleColor color        = ConsoleColor.White;

            switch (result.Result)
            {
            case 1:
                _sessionResults.WinCount++;
                resultString = "Win";
                color        = ConsoleColor.Green;
                break;

            case 0:
                _sessionResults.DrawCount++;
                resultString = "Draw";
                color        = ConsoleColor.Yellow;
                break;

            case -1:
                _sessionResults.LossesCount++;
                resultString = "Lose";
                color        = ConsoleColor.Red;
                break;
            }

            PrintHeader();

            MenuLibrary.WriteColor($"\nYour move: ", ConsoleColor.White);
            MenuLibrary.WriteLineColor($"{_currentMove}", ConsoleColor.DarkCyan);

            MenuLibrary.WriteColor($"Opponent move: ", ConsoleColor.White);
            MenuLibrary.WriteLineColor($"{result.OpponentMove}", ConsoleColor.DarkCyan);

            MenuLibrary.WriteColor($"\nResult: ", ConsoleColor.White);
            MenuLibrary.WriteLineColor(resultString, color);

            MenuLibrary.PressAnyKey();
        }
Ejemplo n.º 12
0
        private async Task ExecuteRegistrationAsync()
        {
            _logger.LogInformation("class AuthorizationMenu. ExecuteRegistrationAsync()");

            MenuLibrary.Clear();
            while (true)
            {
                MenuLibrary.WriteLineColor("\nRegistration\n", ConsoleColor.Yellow);

                string login    = MenuLibrary.InputStringValue("login");
                string password = MenuLibrary.InputStringValue("password");

                _logger.LogInformation("Entered the login and the password");

                var response = await _userClient.RegistrationAsync(login, password);

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

                if (await ResponseHandlerAsync(response, "registration"))
                {
                    return;
                }
            }
        }