public async Task <ActionResult <ApiMethodResponse <bool> > > Resign(GameIdentifierModel resignationModel) { var game = _gameRepository.GetGameById(resignationModel.GameId); var userId = _claimsProvider.GetId(HttpContext); var error = GetErrorActionResult(resignationModel.GameId, game, userId); if (error != null) { return(error); } bool isWhitePlayer = userId == game.WhitePlayerId; _gameRepository.SetGameResult(game.Id, isWhitePlayer ? "black" : "white", isWhitePlayer ? game.BlackPlayerId : game.WhitePlayerId, GameConstants.RESIGNATION_TERMINATION); var gameResult = new GameResult { WinnerColor = isWhitePlayer ? Color.Black : Color.White, Termination = GameConstants.RESIGNATION_TERMINATION }; await _gameHubContext.Clients.Group($"{GameHub.GAME_GROUP_PREFIX}{game.Id}").SendAsync(GameHubOutgoingMessages.RESIGNATION, gameResult); return(Ok(new ApiMethodResponse <bool> { Data = true })); }
public async Task <ActionResult <ApiMethodResponse <bool> > > OfferDraw(GameIdentifierModel offerDrawModel) { var game = _gameRepository.GetGameById(offerDrawModel.GameId); var userId = _claimsProvider.GetId(HttpContext); var error = GetErrorActionResult(offerDrawModel.GameId, game, userId); if (error != null) { return(error); } string color = userId == game.WhitePlayerId ? GameConstants.WHITE : GameConstants.BLACK; _gameRepository.CreateDrawOffer(game.Id, color); await _gameHubContext.Clients.Group($"{GameHub.GAME_GROUP_PREFIX}{game.Id}").SendAsync(GameHubOutgoingMessages.DRAW_OFFER, color); return(Ok(new ApiMethodResponse <bool> { Data = true })); }
public async Task <ActionResult <ApiMethodResponse <bool> > > AcceptDraw(GameIdentifierModel acceptDrawModel) { var game = _gameRepository.GetGameById(acceptDrawModel.GameId); var userId = _claimsProvider.GetId(HttpContext); var error = GetErrorActionResult(acceptDrawModel.GameId, game, userId); if (error != null) { return(error); } if (game.DrawOffer == null) { return(Unauthorized(new ApiMethodResponse <bool> { Data = false, Errors = new[] { "There is no draw offer to accept!" } })); } var drawOfferRecpientColor = game.DrawOffer == GameConstants.WHITE ? Color.Black : Color.White; var drawOfferRecipientId = game.GetPlayerId(drawOfferRecpientColor); if (drawOfferRecipientId != userId) { return(Unauthorized(new ApiMethodResponse <bool> { Data = false, Errors = new[] { "You are not the recpient of the draw offer!" } })); } _gameRepository.RemoveDrawOffer(game.Id); _gameRepository.SetGameResult(game.Id, null, null, GameConstants.AGREEMENT_TERMINATION); var gameResult = new GameResult { WinnerColor = null, Termination = GameConstants.AGREEMENT_TERMINATION }; await _gameHubContext.Clients.Group($"{GameHub.GAME_GROUP_PREFIX}{game.Id}").SendAsync(GameHubOutgoingMessages.DRAW_OFFER_ACCEPTED, gameResult); return(Ok(new ApiMethodResponse <bool> { Data = true })); }