public async Task <IActionResult> CreateMovement([FromBody] MovementDTO movementDto)
        {
            if (movementDto == null)
            {
                return(BadRequest());
            }

            var movement = _mapper.Map <Movement>(movementDto);
            await _movementRepository.AddAsync(movement);

            return(CreatedAtAction(nameof(GetMovementById), new { id = movement.Id }, movement));
        }
Esempio n. 2
0
        public GameDTO MakeMove(Guid playerId, Guid gameId, short position)
        {
            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                if (!_dbContext.Players.AsQueryable().Where(p => p.Id == playerId).Any())
                {
                    transaction.Rollback();
                    throw new NotFoundException("Player");
                }

                var game = _dbContext.Games
                           .AsQueryable()
                           .Include(g => g.GamePlayers)
                           .Include(g => g.Movements)
                           .Where(g => g.Id == gameId && g.GamePlayers.Where(gp => gp.PlayerId == playerId).Any())
                           .FirstOrDefault();
                if (game == null)
                {
                    transaction.Rollback();
                    throw new NotFoundException("Game");
                }
                if (game.IsCompleted)
                {
                    transaction.Rollback();
                    throw new GameException("The game is already over.");
                }

                var gameDto        = _mapper.Map <DAL.Entities.Game, GameDTO>(game);
                var playerMovement = new MovementDTO
                {
                    PlayerId = playerId,
                    GameId   = gameId,
                    Position = position,
                    Number   = (short)game.Movements.Count()
                };
                gameDto.Movements.Add(playerMovement);

                var gameIsValid = BLL.GameUtils.Game.GameIsValid(gameDto.Movements, out string message);
                if (!gameIsValid)
                {
                    transaction.Rollback();
                    throw new GameException(message);
                }
                _dbContext.Movements.Add(_mapper.Map <MovementDTO, Movement>(playerMovement));

                var winnerId = BLL.GameUtils.Game.GetWinnerId(gameDto.Movements, out short?[] winNumbers);
                if (winnerId != Guid.Empty)
                {
                    gameDto.WinnerId   = winnerId;
                    gameDto.WinNumbers = winNumbers;

                    var winner = _dbContext.Players.Find(playerId);
                    winner.WinsCount  += 1;
                    winner.GamesCount += 1;
                    _dbContext.Players.Update(winner);

                    var loser = _dbContext.Players.Find(_gameBot.Id);
                    loser.FailuresCount += 1;
                    loser.GamesCount    += 1;
                    _dbContext.Players.Update(loser);
                }
                else if (!GameUtils.Game.MovementsIsLeft(gameDto.Movements))
                {
                    var gameBot = _dbContext.Players.Find(_gameBot.Id);
                    gameBot.GamesCount += 1;
                    _dbContext.Players.Update(gameBot);

                    var player = _dbContext.Players.Find(playerId);
                    player.GamesCount += 1;
                    _dbContext.Players.Update(player);
                }

                gameDto.IsCompleted = winnerId != Guid.Empty || !GameUtils.Game.MovementsIsLeft(gameDto.Movements);
                if (!gameDto.IsCompleted)
                {
                    var botMovement = _gameBot.MakeMove(gameDto.Movements);
                    botMovement.GameId = gameDto.Id;
                    gameDto.Movements.Add(botMovement);

                    gameIsValid = BLL.GameUtils.Game.GameIsValid(gameDto.Movements, out message);
                    if (!gameIsValid)
                    {
                        transaction.Rollback();
                        throw new GameException(message);
                    }
                    _dbContext.Movements.Add(_mapper.Map <MovementDTO, Movement>(botMovement));
                    winnerId = BLL.GameUtils.Game.GetWinnerId(gameDto.Movements, out winNumbers);
                    if (winnerId != Guid.Empty)
                    {
                        gameDto.WinnerId   = winnerId;
                        gameDto.WinNumbers = winNumbers;

                        var winner = _dbContext.Players.Find(_gameBot.Id);
                        winner.WinsCount  += 1;
                        winner.GamesCount += 1;
                        _dbContext.Players.Update(winner);

                        var loser = _dbContext.Players.Find(playerId);
                        loser.FailuresCount += 1;
                        loser.GamesCount    += 1;
                        _dbContext.Players.Update(loser);
                    }
                    else if (!GameUtils.Game.MovementsIsLeft(gameDto.Movements))
                    {
                        var gameBot = _dbContext.Players.Find(_gameBot.Id);
                        gameBot.GamesCount += 1;
                        _dbContext.Players.Update(gameBot);

                        var player = _dbContext.Players.Find(playerId);
                        player.GamesCount += 1;
                        _dbContext.Players.Update(player);
                    }
                    gameDto.IsCompleted = winnerId != Guid.Empty || !GameUtils.Game.MovementsIsLeft(gameDto.Movements);
                }

                game.IsCompleted = gameDto.IsCompleted;
                game.WinnerId    = gameDto.WinnerId;
                _dbContext.Games.Update(game);
                _dbContext.SaveChanges();
                transaction.Commit();

                return(gameDto);
            }
        }