public async Task <VisibleFieldType[][]> HandleAsync(GetVisibleGameTableRequest request, CancellationToken cancellationToken) { using (var session = _documentStore.OpenAsyncSession()) { // TODO: Validate user id var game = await session.LoadGameAsync(request.GameId, cancellationToken).ConfigureAwait(false); return(EnumArrayCloner.CloneAndMap(game.VisibleTable, FieldTypeConverter.ToContract)); } }
public async Task <MoveResult> HandleAsync(MakeMoveCommand command, CancellationToken cancellationToken) { if (command == null) { throw new ArgumentNullException(nameof(command)); } using (var session = _documentStore.OpenAsyncSession()) { // TODO: Concurrency protection var gameId = command.GameId; var game = await session.LoadGameAsync(gameId, cancellationToken).ConfigureAwait(false); var currentPlayerId = GetPlayerIdByPlayerOrder(game, game.NextPlayer); var currentVisibleTable = EnumArrayCloner.Clone(game.VisibleTable); var mineCountBeforeMove = GetRemainingMineCount(game); var player1PointsBeforeMove = game.Player1.Points; var player2PointsBeforeMove = game.Player2.Points; var movementResult = _gameDriver.MakeMove(game, command.PlayerId, command.Row, command.Column); if (movementResult == MoveResultType.Success || movementResult == MoveResultType.GameOver) { await session.SaveChangesAsync(cancellationToken).ConfigureAwait(false); var mineCountAfterMove = GetRemainingMineCount(game); var nextPlayerId = GetPlayerIdByPlayerOrder(game, game.NextPlayer); await PublishTableUpdatedAsync(gameId, currentVisibleTable, game.VisibleTable, cancellationToken).ConfigureAwait(false);; await PublishPlayersTurnIfChangedAsync(gameId, currentPlayerId, nextPlayerId, cancellationToken).ConfigureAwait(false); await PublishRemainingMinesIfChangedAsync(gameId, mineCountBeforeMove, mineCountAfterMove, cancellationToken).ConfigureAwait(false); await PublishPointsIfChangedAsync( gameId, game.Player1.PlayerId, game.Player2.PlayerId, player1PointsBeforeMove, game.Player1.Points, player2PointsBeforeMove, game.Player2.Points, cancellationToken).ConfigureAwait(false); if (movementResult == MoveResultType.GameOver) { await PublishWinnerAsync(gameId, game.Winner, game.Player1.PlayerId, game.Player2.PlayerId, cancellationToken).ConfigureAwait(false); } } return(new MoveResult { MoveResultType = movementResult }); } }
public async Task <MarkTypes[][]> HandleAsync(GetPlayerMarksRequest request, CancellationToken cancellationToken) { using (var session = _documentStore.OpenAsyncSession()) { var playerMarksDocument = await session.LoadPlayerMarksAsync(request.GameId, request.PlayerId, cancellationToken).ConfigureAwait(false); if (playerMarksDocument == null) { throw new ActionNotAllowedException("You are not involved in that game."); } return(EnumArrayCloner.CloneAndMap(playerMarksDocument.Marks, MarkTypeConverter.ToContract)); } }