Пример #1
0
        public async Task MoveItem(RequestMoveItem request)
        {
            BaseResponse <DOMove> response = CreateResponse <DOMove>();

            try
            {
                BaseResponse <BaseResult> check = await CheckGamePlayRequest(request);

                if (!check.OK)
                {
                    response.Code    = "INVALID_REQUEST";
                    response.Message = check.Message;
                    NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
                    return;
                }
                Board board = GetBoard(request.BoardId);
                if (board.Mode == PlayMode.MODE_PLAYER_VS_PLAYER)
                {
                    if ((board.RedPlayer.Id == request.PlayerId && request.FromColor == Color.BLACK) ||
                        (board.BlackPlayer.Id == request.PlayerId && request.FromColor == Color.RED))
                    {
                        response.Code    = "INVALID_REQUEST_DATA";
                        response.Message = "Dữ liệu yêu cầu nước đi không hợp lệ";
                        NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
                        return;
                    }
                }
                if (board.Turn != request.FromColor)
                {
                    response.Code    = "NOT_YOUR_TURN";
                    response.Message = "Chưa đến lượt bạn đi";
                    NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
                    return;
                }
                Item item = board.PickItem(
                    request.FromColor,
                    request.FromType,
                    request.FromRow,
                    request.FromCol);

                if (item == null)
                {
                    response.Code    = "ITEM_NOT_FOUND";
                    response.Message = String.Format("Không tìm thấy quân cờ {0} {1} tại vị trí ({2},{3}",
                                                     request.FromColor,
                                                     request.FromType,
                                                     request.FromRow,
                                                     request.FromCol
                                                     );
                    NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
                    return;
                }
                //
                Position targetPosition = new Position(request.ToRow, request.ToCol);
                if (!item.Scopes.Contains(targetPosition))
                {
                    response.Code    = "INVALID_MOVE";
                    response.Message = "Nước đi không hợp lệ do nằm ngoài phạm vi cho phép";
                    NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
                    return;
                }

                response.Data.Item = new DOItem
                {
                    Alive = true,
                    Type  = request.FromType,
                    Color = request.FromColor,
                    Row   = request.FromRow,
                    Col   = request.FromCol,
                };

                Item target = board.FindItem(request.FromColor, request.ToRow, request.ToCol);
                if (target != null)
                {
                    response.Data.Kill = target.MapTo();
                }
                response.Data.Destination = targetPosition;

                board.MoveItemTo(item, targetPosition);
                board.AnalyseBoard();
                SaveBoard(board);

                response.OK                  = true;
                response.Data.Turn           = board.Turn;
                response.Data.RedPlayer      = board.RedPlayer.MapToPlayerInBoard();
                response.Data.BlackPlayer    = board.BlackPlayer.MapToPlayerInBoard();
                response.Data.IsCheckMate    = board.IsCheckMated;
                response.Data.IsGameOver     = board.IsGameOver;
                response.Data.BoardStatus    = board.Status;
                response.Data.WarningSide    = board.WarningSide;
                response.Data.WarningMessage = board.WarningMessage;
                NotifyForAll(board, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);

                if (board.IsGameOver)
                {
                    await GameOver(board);
                }
            }
            catch (Exception e)
            {
                ExceptionHandle(response, e);
                NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_PLAYER_MOVE_ITEM);
            }
        }
Пример #2
0
 public async Task RequestMoveItem(RequestMoveItem request)
 {
     request.ConnectionId = Context.ConnectionId;
     await _service.MoveItem(request);
 }