示例#1
0
        public async Task <IActionResult> AddNewStepAsync(
            [FromBody] AddNewStepCommand model)
        {
            _logger.LogDebug($"Action: {ControllerContext.ActionDescriptor.ActionName} " +
                             $"Parameters: GameId = {model.GameId}, PlayerId = {model.PlayerId}," +
                             $" index={model.Index}");

            var result = await _mediator.Send(model);

            if (result.IsSuccess)
            {
                _logger.LogInformation(
                    $"Action: {ControllerContext.ActionDescriptor.ActionName} : " +
                    $"step from PlayerId={model.PlayerId} GameId={model.GameId} " +
                    $"was written at [{DateTime.UtcNow}]");

                return(Ok(result.Value));
            }

            _logger.LogWarning(
                $"Action: {ControllerContext.ActionDescriptor.ActionName}: " +
                $"step from PlayerId={model.PlayerId} GameId={model.GameId} can't be written");

            return(BadRequest(result.Error));
        }
示例#2
0
        public async Task <Result <StepResult> > Handle(AddNewStepCommand request,
                                                        CancellationToken cancellationToken)
        {
            var ticTacSymbol    = 0;
            var currentPlayerId = "";
            var gameId          = request.GameId;

            var firstPlayer = await _context.FirstPlayers
                              .Where(p => p.HaunterId == request.PlayerId.ToString())
                              .Where(x => x.Player.GameId == gameId)
                              .FirstOrDefaultAsync(cancellationToken);

            if (firstPlayer != null)
            {
                ticTacSymbol    = firstPlayer.TicTac;
                currentPlayerId = firstPlayer.HaunterId;
            }

            var secondPlayer = await _context.SecondPlayers
                               .Where(p => p.HaunterId == request.PlayerId.ToString())
                               .Where(x => x.Player.GameId == gameId)
                               .FirstOrDefaultAsync(cancellationToken);

            if (secondPlayer != null)
            {
                ticTacSymbol    = secondPlayer.TicTac;
                currentPlayerId = secondPlayer.HaunterId;
            }

            if (firstPlayer == null && secondPlayer == null)
            {
                return(Result.Fail <StepResult>(
                           $"This game has no player ID: {request.PlayerId}"));
            }

            var mapDb = await _context.Maps
                        .Where(c => c.GameId == gameId)
                        .FirstOrDefaultAsync(cancellationToken);

            var cell = IndexToCell(mapDb.Size, request.Index);

            var cellDb = await _context.Cells
                         .Where(c => c.MapId == mapDb.Id).Where(x => x.X == cell.X)
                         .Where(y => y.Y == cell.Y)
                         .FirstOrDefaultAsync(cancellationToken);

            if (cellDb == null)
            {
                return(Result.Fail <StepResult>($"This Cell with X={cell.X} Y={cell.Y} " +
                                                $"in the game with ID [{gameId}], " +
                                                $"on this map with ID [{mapDb.Id}] does not exist."));
            }

            if (cellDb.TicTac != 0)
            {
                return(Result.Fail <StepResult>("The cell is already taken."));
            }

            cellDb.TicTac = ticTacSymbol;
            _context.Cells.Update(cellDb);

            try
            {
                await _context.SaveChangesAsync(cancellationToken);

                var cells = await _context.Cells
                            .Where(c => c.MapId == mapDb.Id)
                            .ToListAsync(cancellationToken);

                var cellList = new List <int>();
                foreach (var item in cells)
                {
                    cellList.Add(item.TicTac);
                }

                var stepResultDb = await _context.StepResults
                                   .Where(c => c.GameId == gameId)
                                   .FirstOrDefaultAsync(cancellationToken);

                var status = GetStatus(mapDb.WinningChain, cellList, request.Index);
                stepResultDb.Status = (Data.Models.State)status;

                //Determining the order of players for the next step
                var gamePlayers = GetPlayers(_context, gameId);
                var nextPlayer  = currentPlayerId == gamePlayers.One
                    ? gamePlayers.Two
                    : gamePlayers.One;

                stepResultDb.NextPlayerId = Convert.ToInt32(nextPlayer);
                _context.StepResults.Update(stepResultDb);

                await _context.SaveChangesAsync(cancellationToken);

                var stepResult = new StepResult
                {
                    GameId       = stepResultDb.GameId,
                    NextPlayerId = stepResultDb.NextPlayerId,
                    Status       = status
                };

                return(Result.Ok(_mapper.Map <StepResult>(stepResult)));
            }
            catch (DbUpdateException ex)
            {
                return(Result.Fail <StepResult>(ex.Message));
            }
        }