Пример #1
0
        public async Task <IActionResult> Create([Bind("ID,Wins,Losses,Draws,Name,CreatedOn,UpdatedOn")] Player player)
        {
            if (ModelState.IsValid)
            {
                _context.Add(player);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(player));
        }
Пример #2
0
        public async Task <IActionResult> Index(string userName, string userSelection, int p1win, int p1lose, int p1draw, int p2win, int p2lose, int p2draw)
        {
            Player    aiPlayer    = _context.Player.Where(s => s.ID == 1).FirstOrDefault();
            Player    humanPlayer = new Player();
            Game      thisGame    = new Game();
            GameRound gameRound   = new GameRound();

            if (!String.IsNullOrEmpty(userName))
            {
                humanPlayer = _context.Player.Where(s => s.Name.Equals(userName)).FirstOrDefault();
                if (humanPlayer == null)
                {
                    humanPlayer = new Player
                    {
                        Name   = userName,
                        Wins   = 0,
                        Losses = 0,
                        Draws  = 0
                    };
                    _context.Add(humanPlayer);
                    await _context.SaveChangesAsync();
                }

                gameRound.Players.Add(aiPlayer);
                gameRound.Players.Add(humanPlayer);
                gameRound.PlayerWins    = p1win;
                gameRound.PlayerLosses  = p1lose;
                gameRound.PlayerDraws   = p1draw;
                gameRound.AiWins        = p2win;
                gameRound.AiLosses      = p2lose;
                gameRound.AiDraws       = p2draw;
                gameRound.HumanPlayer   = humanPlayer;
                gameRound.UserSelection = userSelection;
                gameRound.AiSelection   = thisGame.GetAiSelection();
                if (!String.IsNullOrEmpty(userSelection))
                {
                    gameRound.AiImageUrl    = thisGame.GetImage(gameRound.AiSelection);
                    gameRound.HumanImageUrl = thisGame.GetImage(userSelection);
                }
            }



            //int test = id;
            String[] weapons = new string[] { "Scissors", "Paper", "Rock", "Lizard", "Spock" };



            if (!String.IsNullOrEmpty(userSelection))
            {
                gameRound.PlayerSelectionString = "The user has selected: " + gameRound.UserSelection;
                gameRound.AiSelectionString     = "Sheldon has selected: " + gameRound.AiSelection;
            }



            if (!String.IsNullOrEmpty(userSelection))
            {
                int result = thisGame.GetWinner(weapons, gameRound.UserSelection, gameRound.AiSelection);

                switch (result)
                {
                case 1:
                    gameRound.Verb   = thisGame.GetVerb(gameRound.UserSelection, gameRound.AiSelection);
                    gameRound.Winner = gameRound.UserSelection + " " + gameRound.Verb + " " + gameRound.AiSelection + " you win!";
                    gameRound.PlayerWins++;
                    gameRound.AiLosses++;
                    humanPlayer.Wins++;
                    aiPlayer.Losses++;
                    _context.UpdateRange(gameRound.Players);
                    await _context.SaveChangesAsync();

                    break;

                case 0:
                    gameRound.Draw = "Draw";
                    gameRound.AiDraws++;
                    gameRound.PlayerDraws++;
                    humanPlayer.Draws++;
                    aiPlayer.Draws++;
                    _context.UpdateRange(gameRound.Players);
                    await _context.SaveChangesAsync();

                    break;

                case -1:
                    gameRound.Verb  = thisGame.GetVerb(gameRound.AiSelection, gameRound.UserSelection);
                    gameRound.Loser = gameRound.AiSelection + " " + gameRound.Verb + " " + gameRound.UserSelection + " you lose!";
                    gameRound.PlayerLosses++;
                    gameRound.AiWins++;
                    humanPlayer.Losses++;
                    aiPlayer.Wins++;
                    _context.UpdateRange(gameRound.Players);
                    await _context.SaveChangesAsync();

                    break;
                }
            }



            return(View(gameRound));
        }