Пример #1
0
        public async Task <int> CreateAsync(GameCreateModel model, IPrincipal currentPrincipal)
        {
            IdentityUser user = await context.Users.SingleAsync(x => x.UserName == currentPrincipal.Identity.Name);

            Game game = ModelToGame(model.FirstPlayerId, user.Id);

            context.Games.Add(game);
            await context.SaveChangesAsync();

            return(game.Id);
        }
Пример #2
0
        public async Task CreateAsync(int gameId, ShotCreateModel model)
        {
            IdentityUser user = context.Users.Find(model.PlayerId);

            Game game = await context.Games
                        .Include("FirstSide.Player")
                        .Include("FirstSide.Ships.Positions")
                        .Include("FirstSide.Shots")
                        .Include("SecondSide.Player")
                        .Include("SecondSide.Ships.Positions")
                        .Include("SecondSide.Shots")
                        .Include("WinnerSide")
                        .Include("NextShotSide")
                        .FirstOrDefaultAsync(x => x.Id == gameId);


            if (game.FirstSide.PlayerFK.Equals(user.Id, StringComparison.InvariantCultureIgnoreCase))
            {
                bool isWinner = AddShot(game.SecondSide, model);

                if (isWinner)
                {
                    game.WinnerSide = game.FirstSide;
                    game.EndedAt    = DateTime.UtcNow;
                }

                if (!IsHit(game.SecondSide, model))
                {
                    game.NextShotSide = game.SecondSide;
                }
            }
            else
            {
                bool isWinner = AddShot(game.FirstSide, model);

                if (isWinner)
                {
                    game.WinnerSide = game.SecondSide;
                    game.EndedAt    = DateTime.UtcNow;
                }

                if (!IsHit(game.FirstSide, model))
                {
                    game.NextShotSide = game.FirstSide;
                }
            }

            await context.SaveChangesAsync();
        }
Пример #3
0
        public async Task CreateAsync(int gameId, GamePositionCreateModel model)
        {
            Game game = (await context.Games.Where(x => x.Id == gameId)
                         .Include("FirstSide")
                         .Include("FirstSide.Ships")
                         .Include("SecondSide")
                         .Include("SecondSide.Ships")
                         .ToListAsync()).FirstOrDefault();

            if (game.FirstSide.PlayerFK == model.PlayerId)
            {
                HandleAddingShips(game, game.FirstSide, game.SecondSide, model.Ships);
            }
            else
            {
                HandleAddingShips(game, game.SecondSide, game.FirstSide, model.Ships);
            }

            await context.SaveChangesAsync();
        }