Exemplo n.º 1
0
        private bool _changePlayer; // To display other screen before other player makes move

        public async Task <IActionResult> OnGetAsync(int id, int?shipId, int?x, int?y, string dir, bool horizontal, string move)
        {
            IsShipHorizontal = horizontal;


            Game = await _context.Games
                   .Where(g => g.GameId == id)
                   .Include(g => g.GameOption)
                   .Include(g => g.GameOption.GameOptionShips)
                   .Include(g => g.PlayerA)
                   .Include(g => g.PlayerA.GameShips)
                   .Include(g => g.PlayerA.PlayerBoardStates)
                   .Include(g => g.PlayerB)
                   .Include(g => g.PlayerB.GameShips)
                   .Include(g => g.PlayerB.PlayerBoardStates).FirstOrDefaultAsync();

            BattleShips = new BattleShips(Game);


            if (BattleShips.GetUnaddedGameShips().Count > 0)
            {
                CurrentGameShip = shipId != null?BattleShips.GetUnaddedGameShips().FirstOrDefault(s => s.GameShipId == shipId) : BattleShips.GetUnaddedGameShips().First();
            }

            IsGameOver = BattleShips.IsGameOver();

            if (x != null && y != null && CurrentGameShip != null)
            {
                var(isSuccess, playerBoardState) = BattleShips.AddShips(CurrentGameShip, x.Value, y.Value, IsShipHorizontal);
                if (isSuccess && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    if (BattleShips.GetUnaddedGameShips().Count > 0)
                    {
                        CurrentGameShip = BattleShips.GetUnaddedGameShips().First();
                    }
                    else
                    {
                        CurrentGameShip = null;
                        _changePlayer   = true;
                    }
                }
            }
            else if (x != null && y != null && move == "shoot")
            {
                var(success, playerBoardState) = BattleShips.MakeMove(x.Value, y.Value);

                if (success[0] && !success[1] && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    _changePlayer = true;
                }
                else if (success[1] && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    IsGameOver = true;
                }
            }

            CurrentPlayerBoard = BattleShips.GetBoards().First();
            if (BattleShips.GetUnaddedGameShips().Count == 0 && !IsGameOver)
            {
                OtherPlayerBoard = BattleShips.GetBoards(true)[1];
            }
            else if (IsGameOver)
            {
                OtherPlayerBoard = BattleShips.GetBoards(false, true)[1];
            }

            if (CurrentGameShip != null)
            {
                HandlePosition(dir);
            }


            await _context.SaveChangesAsync();

            if (_changePlayer)
            {
                return(RedirectToPage("../NextTurn/Index", new { id = Game !.GameId }));