Example #1
0
        public async Task <IActionResult> AddMove(MoveForAddMoveDto dto)
        {
            if (dto.UserId != this.GetCurrentUserId())
            {
                return(Unauthorized());
            }

            Game game = await _repo.GetGameForAddMove(dto.GameId);

            if (dto.UserId != game.WhiteUserId && dto.UserId != game.BlackUserId)
            {
                return(Unauthorized());
            }

            dto.Notation = game.GetNotation(dto);
            Move       newMove = _mapper.Map <Move>(dto);
            MoveResult result  = game.TryAddMoveToGame(newMove);

            if (result != MoveResult.CanMove)
            {
                return(BadRequest(result.GetDescription()));
            }

            int code = await _repo.SaveAll();

            await _hub.Clients.GroupExcept(dto.GameId.ToString(), dto.ConnId).SendAsync("addMoveToGame", dto);

            return(Ok(dto));
        }
Example #2
0
        // public override Task OnDisconnectedAsync(System.Exception exception)
        // {
        //  if (Users.Any(x => x.ConnectionID == Context.ConnectionId))
        // {
        //     User user = Users.First(x => x.ConnectionID == Context.ConnectionId);
        //     Clients.Others.userLeft(user.Name);
        //     Users.Remove(user);
        // }
        // return base.OnDisconnectedAsync(exception);

        // }
        // public override OnDisconnectedAsync(){

        //     return base.OnDisconnectedAsync();
        // }

        public async void AddMoveToGame(MoveForAddMoveDto newMoveDto)
        {
            Game game = await _gameRepo.GetGameForAddMove(newMoveDto.GameId);



            await Clients.OthersInGroup(newMoveDto.GameId.ToString()).SendAsync("addMoveToGame", newMoveDto);

            // Clients.All.SendAsync("sendToAll", move);
        }
Example #3
0
        public static string GetNotation(this Game game, MoveForAddMoveDto dto)
        {
            string[]      letters = { "a", "b", "c", "d", "e", "f", "g", "h" };
            StringBuilder sb      = new StringBuilder();

            if (dto.IsCastle)
            {
                if (dto.EndX == 2)
                {
                    sb.Append("0-0-0");
                }
                else
                {
                    sb.Append("0-0");
                }
                return(sb.ToString());
            }
            if (dto.PieceDiscriminator == "Knight")
            {
                sb.Append("N");
            }
            else if (!String.IsNullOrEmpty(dto.PieceDiscriminator) && dto.PieceDiscriminator != "Pawn")
            {
                sb.Append(dto.PieceDiscriminator[0].ToString());
            }
            sb.Append(letters[dto.StartX] + (dto.StartY + 1).ToString());
            if (dto.IsCapture)
            {
                sb.Append("x");
            }
            else
            {
                sb.Append("-");
            }
            sb.Append(letters[dto.EndX] + (dto.EndY + 1).ToString());
            if (!String.IsNullOrEmpty(dto.PromoteTo))
            {
                if (dto.PromoteTo == "Knight")
                {
                    sb.Append("=N");
                }
                else
                {
                    sb.Append("=" + dto.PromoteTo[0].ToString());
                }
            }
            return(sb.ToString());
        }
Example #4
0
        public async Task <IActionResult> AddMove(MoveForAddMoveDto dto)
        {
            Move newMove = _mapper.Map <Move>(dto);

            _repo.Add(newMove);
            Piece movedPiece = await _repo.GetPiece(dto.PieceId);

            var moveAttempt = movedPiece.IsLegalMove(newMove, dto.isWhite);

            if (moveAttempt.Item1)
            {
                movedPiece.X = newMove.EndX;
                movedPiece.Y = newMove.EndY;
                int code = await _repo.SaveAll();

                return(Ok(code));
            }
            else
            {
                return(BadRequest(moveAttempt.Item2));
            }
        }
Example #5
0
        public async Task <IActionResult> AddMoveTwoPlayer(MoveForAddMoveDto dto)
        {
            Move newMove = _mapper.Map <Move>(dto);

            _repo.Add(newMove);
            Piece movedPiece = await _repo.GetPiece(dto.PieceId);

            if (movedPiece == null)
            {
                return(BadRequest("Piece not found"));
            }
            var moveAttempt = movedPiece.IsLegalMoveTwoPlayer(newMove, dto.isWhite);

            if (moveAttempt.Item1)
            {
                Piece pieceAtNewLocation = await _repo.GetPieceByXY(dto.GameId, dto.EndX, dto.EndY);

                if (pieceAtNewLocation != null && pieceAtNewLocation.OwnedBy.Id != dto.UserId)
                {
                    // ef needs to update the move record that the piece doens't exist anymore.
                    // await _repo.GetMovesForPiece(pieceAtNewLocation.Id);
                    _repo.Delete(pieceAtNewLocation);
                }

                movedPiece.X = newMove.EndX;
                movedPiece.Y = newMove.EndY;
                int code = await _repo.SaveAll();

                string connId = dto.connId;
                await _hub.Clients.GroupExcept(dto.GameId.ToString(), connId).SendAsync("addMoveToGame", dto);

                return(Ok(code));
            }
            else
            {
                return(BadRequest(moveAttempt.Item2));
            }
        }