Пример #1
0
        public async void CreateGames()
        {
            Random r           = new Random();
            var    numberGames = 0;

            Console.WriteLine("Creating 100 games");
            Console.WriteLine();
            Console.WriteLine();
            while (numberGames < 1)
            {
                numberGames++;

                Console.WriteLine($"Start the {numberGames} game ...");
                Console.WriteLine();

                await CreatePlayer(1);
                await CreatePlayer(2);

                Game game;
                try
                {
                    game = await _gameService.AddAsync(new Game
                    {
                        EndGame = false,
                        PlayerGameWinnerName = ""
                    });


                    Console.WriteLine($"Game {game.Id} Created Succefully");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex}");
                    continue;
                }


                var numberRound = 1;
                var player1     = await _playerService.GetPlayerByNumber(1);

                var player2 = await _playerService.GetPlayerByNumber(2);

                while (true)
                {
                    Console.WriteLine($"Round {numberRound}");
                    numberRound++;
                    Round round;
                    try
                    {
                        var player1Move = (Move)r.Next(1, 4);
                        round = await _roundService.AddAsync(new Round
                        {
                            Player1Move = player1Move,
                            GameId      = game.Id
                        });

                        Console.WriteLine($"{player1.PlayerName} played {player1Move}");
                        Console.WriteLine();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error with Player 1 move: {ex}");
                        break;
                    }

                    try
                    {
                        Console.WriteLine(round.Id);
                        var player2Move = (Move)r.Next(1, 4);

                        var rnd = await _roundService.SingleOrDefaultAsync(round.Id);

                        if (rnd != null)
                        {
                            game = await _roundService.CompleteRound(rnd, player2Move);
                        }

                        Console.WriteLine($"{player2.PlayerName} played {player2Move}");
                        Console.WriteLine();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error with Player 2 move: {ex}");
                        break;
                    }

                    if (game.EndGame)
                    {
                        Console.WriteLine($"Game {numberGames} winner is {game.PlayerGameWinnerName}");
                        Console.WriteLine();
                        break;
                    }
                }
            }
            Console.WriteLine("Statics for players");
            foreach (var name in PlayerNames)
            {
                try
                {
                    var result = await _gameService.Statistics(name);

                    Console.WriteLine($"{name} won {result.Count} games");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex}");
                    continue;
                }
            }
            Console.WriteLine("Test Finish");
        }