Ejemplo n.º 1
0
        public async Task <IActionResult> CreateNewGameAsync(
            [FromBody] AddNewGameCommand model)
        {
            _logger.LogDebug(
                $"Action: {ControllerContext.ActionDescriptor.ActionName} " +
                $"Parameters: PlayerOne = {model.PlayerOne}, " +
                $"PlayerTwo = {model.PlayerTwo}, " +
                $"MapSize = {model.MapSize}");

            var result = await _mediator.Send(model);

            if (result.IsSuccess)
            {
                _logger.LogInformation(
                    $"Action: {ControllerContext.ActionDescriptor.ActionName} : - " +
                    $"Game was created at {DateTime.UtcNow} [{DateTime.UtcNow.Kind}]");

                return(Created("api/game", result.Value));
            }

            _logger.LogWarning(
                $"Action: {ControllerContext.ActionDescriptor.ActionName} : - " +
                "Game can't be created");

            return(BadRequest(result.Error));
        }
        public async Task <ActionResult <string> > AddNewGame([FromBody] AddNewGameCommand command)
        {
            var result = await Mediator.Send(command);

            return(new JsonResult(result));
        }
Ejemplo n.º 3
0
        public async Task <Result <Game> > Handle(AddNewGameCommand request,
                                                  CancellationToken cancellationToken)
        {
            //  Creating new clean game.
            var gameDb = new GameDb();
            await _context.Games.AddAsync(gameDb, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            // Creating fake haunters if HaunterDb is empty.
            if (!_context.Haunters.Any())
            {
                Seed(_context, 100);
            }

            // Creating two new players in the game.
            await CheckPlayerIdForExistenceAsync(_context, request.PlayerOne,
                                                 cancellationToken);

            var firstPlayerDb = new FirstPlayerDb {
                HaunterId = request.PlayerOne
            };
            await _context.FirstPlayers.AddAsync(firstPlayerDb, cancellationToken);

            await CheckPlayerIdForExistenceAsync(_context, request.PlayerTwo,
                                                 cancellationToken);

            var secondPlayerDb = new SecondPlayerDb {
                HaunterId = request.PlayerTwo
            };
            await _context.SecondPlayers.AddAsync(secondPlayerDb, cancellationToken);

            var playersDb = new PlayerDb
            {
                GameId         = gameDb.Id, FirstPlayerId = firstPlayerDb.Id,
                SecondPlayerId = secondPlayerDb.Id
            };
            await _context.Players.AddAsync(playersDb, cancellationToken);

            // Creating new StepResult with received parameters
            var stateResultDb = new StepResultDb
            {
                NextPlayerId = Convert.ToInt32(request.PlayerOne),
                Status       = Data.Models.State.Continuation,
                GameId       = gameDb.Id
            };
            await _context.StepResults.AddAsync(stateResultDb, cancellationToken);


            // Creating new map with received parameters
            var mapDb = new MapDb
            {
                Size         = request.MapSize,
                WinningChain = request.MapWinningChain,
                GameId       = gameDb.Id
            };
            await _context.Maps.AddAsync(mapDb, cancellationToken);

            //  Creating new map cells
            var cells   = CreateCellsForMap(mapDb);
            var cellDbs = cells as CellDb[] ?? cells.ToArray();
            await _context.AddRangeAsync(cellDbs, cancellationToken);

            var game = await _context.Games
                       .Where(g => g.Id == gameDb.Id)
                       .SingleOrDefaultAsync(cancellationToken);

            var cellList = cellDbs.Select(item => 0).ToList();

            var gameDto = new Game
            {
                Id         = game.Id,
                MapId      = request.MapSize,
                PlayerOne  = Convert.ToInt32(request.PlayerOne),
                PlayerTwo  = Convert.ToInt32(request.PlayerTwo),
                TicTacList = cellList
            };

            try
            {
                await _context.SaveChangesAsync(cancellationToken);

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