public async Task RequestBoard(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; BaseResponse <DOBoard> response = await _service.GetBoardInfo(request); _ = Clients.Clients(request.ConnectionId).ReplyBoardInfo(response); }
public void RequestBoardSnapshot(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; BaseResponse <DOBoardSnapshot> response = _service.GetBoardSnapshot(request); _ = Clients.Clients(request.ConnectionId).ReplyBoardSnapshot(response); }
public async Task RequestPlayGame(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; BaseResponse <DOBoard> response = await _service.PlayGame(request); _ = Clients.Clients(request.ConnectionId).ReceivePlayGame(response); }
public async Task <BaseResponse <DOBoard> > PlayGame(RequestGamePlay request) { BaseResponse <DOBoard> response = CreateResponse <DOBoard>(); try { Player player = await _playerService.GetPlayer(request.PlayerId); if (player == null) { response.Message = "Người chơi không tồn tại"; return(response); } Board board = FindBoardByPlayerId(request.PlayerId); if (board != null) { response = await RejoinGame(board, player); return(response); } // get list board if (_dicNewBoards.Count == 0) { board = CreateBoard(player); if (board == null) { response.Message = "Lỗi: không tạo được bàn chơi mới"; return(response); } AddToNewBoardList(board); response.Data = board.MapTo(); } else { board = _dicNewBoards.Values.FirstOrDefault(); BaseResponse <DOBoard> result = AssignBoard(board, player); if (!result.OK) { response.Message = result.Message; } else { if (result.Data.RedPlayer != null && result.Data.BlackPlayer != null) { RemoveFromNewBoardList(board); AddToBoardList(board); } response.Data = board.MapTo(); response.OK = true; } } response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
public async Task DrawOffer(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); try { BaseResponse <BaseResult> check = await CheckGamePlayRequest(request); if (!check.OK) { return; } Board board = GetBoard(request.BoardId); response.OK = true; if (board.RedPlayer != null && request.PlayerId == board.RedPlayer.Id) { string connectionId = board.ConnectionByPlayer.GetValueOrDefault(board.BlackPlayer.Id); NotifyForOne(connectionId, response, BoardHub.RECEIVE_DRAW_OFFER); } else if (board.BlackPlayer != null && request.PlayerId == board.BlackPlayer.Id) { string connectionId = board.ConnectionByPlayer.GetValueOrDefault(board.RedPlayer.Id); NotifyForOne(connectionId, response, BoardHub.RECEIVE_DRAW_OFFER); } response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } }
public async Task <BaseResponse <DOBoard> > CancelReadyPlay(RequestGamePlay request) { BaseResponse <DOBoard> response = CreateResponse <DOBoard>(); try { BaseResponse <BaseResult> check = await CheckGamePlayRequest(request); if (!check.OK) { response.Message = check.Message; return(response); } Board board = GetBoard(request.BoardId); if (board.RedPlayer.Id == request.PlayerId) { board.RedPlayer.ReadyToPlay = false; } else if (board.BlackPlayer.Id == request.PlayerId) { board.BlackPlayer.ReadyToPlay = false; } SaveBoard(board); response.Data = board.MapTo(); response.OK = true; NotifyForAll(board, response, "ReceiveCancelReadyPlay"); } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
public async Task Resign(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); try { BaseResponse <BaseResult> check = await CheckGamePlayRequest(request); if (!check.OK) { response.Message = check.Message; NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_RESIGN); return; } Board board = GetBoard(request.BoardId); if (board.RedPlayer != null && board.RedPlayer.Id == request.PlayerId) { await BlackWin(board, "Bên ĐỎ chấp nhận đầu hàng"); } else if (board.BlackPlayer != null && board.BlackPlayer.Id == request.PlayerId) { await RedWin(board, "Bên ĐEN chấp nhận đầu hàng"); } } catch (Exception e) { ExceptionHandle(response, e); } }
public async Task SwitchSide(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); try { var check = await CheckGamePlayRequest(request); if (!check.OK) { response.Message = check.Message; NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_SWITCH_SIDE); return; } Board board = GetBoard(request.BoardId); if (board.RedPlayer == null || board.RedPlayer.Id != request.PlayerId) { response.Message = "Chỉ người cầm bên Đỏ mới được phép chấp tiên"; NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_SWITCH_SIDE); return; } board.SwitchSide(); SaveBoard(board); response.OK = true; BaseResponse <DOBoard> res = CreateResponse <DOBoard>(); res.OK = true; res.Data = board.MapTo(); NotifyForAll(board, res, BoardHub.RECEIVE_BOARD_INFO); } catch (Exception e) { ExceptionHandle(response, e); } }
public async Task <BaseResponse <DOBoard> > GetBoardInfo(RequestGamePlay request) { BaseResponse <DOBoard> response = CreateResponse <DOBoard>(); try { Player player = await _playerService.GetPlayer(request.PlayerId); if (player == null) { response.Message = "Không tìm thấy người chơi"; return(response); } Board board = GetBoard(request.BoardId); if (board == null) { response.Message = "Không tìm thấy thông tin bàn chơi"; return(response); } board.SaveConnection(request.PlayerId, request.ConnectionId); response.Data = board.MapTo(); response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
public async Task RequestCreateGame(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; BaseResponse <BaseResult> response = await _service.CreateGame(request); _ = Clients.Clients(request.ConnectionId).ReceiveGame(response); }
public async Task <BaseResponse <BaseResult> > FindGame(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); try { Player player = await _playerService.GetPlayer(request.PlayerId); if (player == null) { response.Message = "Người chơi không tồn tại"; return(response); } if (_dicNewBoards.Count > 0) { // Board board = } Board board = CreateBoard(player); if (board == null) { response.Message = "Lỗi: không tạo được bàn chơi mới"; return(response); } AddToNewBoardList(board); response.Data.Id = board.Id; response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
public async Task <BaseResponse <DOBoard> > LeaveBoard(RequestGamePlay request) { BaseResponse <DOBoard> response = CreateResponse <DOBoard>(); try { BaseResponse <BaseResult> check = await CheckGamePlayRequest(request); if (!check.OK) { response.Message = check.Message; return(response); } Player player = await _playerService.GetPlayer(request.PlayerId); Board board = GetBoard(request.BoardId); if ((board.RedPlayer != null && board.RedPlayer.Id == player.Id) || (board.BlackPlayer != null && board.BlackPlayer.Id == player.Id)) { if (board.Status == BoardStatus.PLAYING) { response.Message = "Bạn không thể rời bàn trong lúc đang chơi"; return(response); } } // leave if player is observer board.Observers.Remove(player.Id); if (board.RedPlayer.Id == player.Id) { board.SetPlayer(Color.RED, null); } else if (board.BlackPlayer.Id == player.Id) { board.SetPlayer(Color.BLACK, null); } SaveBoard(board); _playerService.SavePlayer(player); board.RemoveConnection(request.PlayerId); response.Data = board.MapTo(); response.OK = true; NotifyForAll(board, response, "ReceivePlayerLeave"); } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
public async Task <BaseResponse <BaseResult> > LeaveGame(RequestGamePlay request) { BaseResponse <BaseResult> response = await CheckGamePlayRequest(request); try { if (!response.OK) { return(response); } Board board = GetBoard(request.BoardId); if (board.Status == BoardStatus.PLAYING) { if (board.IsPlayer(request.PlayerId)) { if (board.IsRedPlayer(request.PlayerId)) { await BlackWin(board, "Bên ĐỎ đã rời bàn chơi"); } else if (board.IsBlackPlayer(request.PlayerId)) { await RedWin(board, "Bên ĐEN đã rời bàn chơi"); } } } board.UnAssigned(request.PlayerId); RemovePlayerInBoard(request.PlayerId); if (board.RedPlayer == null && board.BlackPlayer == null && board.Observers.Count == 0) { RemoveBoard(board.Id); } else if (!board.IsGameOver) { BaseResponse <DOBoard> res = CreateResponse <DOBoard>(); res.OK = true; res.Data = board.MapTo(); NotifyForAll(board, res, BoardHub.RECEIVE_BOARD_INFO); AddToNewBoardList(board); } } catch (Exception e) { response.OK = false; response.Message = e.Message; } return(response); }
public async Task ReadyToPlay(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); try { BaseResponse <BaseResult> check = await CheckGamePlayRequest(request); if (!check.OK) { response.Message = check.Message; NotifyForOne(request.ConnectionId, response, BoardHub.RECEIVE_READY_TO_PLAY); return; } Board board = GetBoard(request.BoardId); if (board.RedPlayer.Id == request.PlayerId) { board.RedPlayer.ReadyToPlay = true; } else if (board.BlackPlayer.Id == request.PlayerId) { board.BlackPlayer.ReadyToPlay = true; } if (board.RedPlayer != null && board.RedPlayer.ReadyToPlay && board.BlackPlayer != null && board.BlackPlayer.ReadyToPlay ) { board.Status = BoardStatus.START_GAME; board.Reset(); SaveBoard(board); SendBoardToAll(board, BoardHub.RECEIVE_START_GAME); SendBoardToAll(board, BoardHub.RECEIVE_BOARD_INFO); } else { SaveBoard(board); SendBoardToAll(board, BoardHub.RECEIVE_BOARD_INFO); } response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } }
public BaseResponse <DOBoardSnapshot> GetBoardSnapshot(RequestGamePlay request) { BaseResponse <DOBoardSnapshot> response = CreateResponse <DOBoardSnapshot>(); try { Board board = GetBoard(request.BoardId); response.Data = board.MapToSnapshot(); response.OK = true; } catch (Exception e) { ExceptionHandle(response, e); } return(response); }
private async Task <BaseResponse <BaseResult> > CheckGamePlayRequest(RequestGamePlay request) { BaseResponse <BaseResult> response = CreateResponse <BaseResult>(); Player player = await _playerService.GetPlayer(request.PlayerId); if (player == null) { response.Message = "Người chơi không tồn tại"; return(response); } Board board = GetBoard(request.BoardId); if (board == null) { response.Message = "Bàn chơi không tồn tại"; return(response); } response.OK = true; return(response); }
public async Task RequestDraw(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; await _service.DrawOffer(request); }
public async Task RequestReadyToPlay(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; await _service.ReadyToPlay(request); }
public async Task RequestSwitchSide(RequestGamePlay request) { request.ConnectionId = Context.ConnectionId; await _service.SwitchSide(request); }