Example #1
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();
        }
Example #2
0
        public async Task ValidateForCreate(IPrincipal currentPrincipal, int gameId, ShotCreateModel model)
        {
            await ValidateCurrentUser(currentPrincipal, model.PlayerId);

            Game game = await context.Games
                        .Include("NextShotSide")
                        .FirstOrDefaultAsync(x => x.Id == gameId);

            ValidateGameExists(game);
            ValidateNextShot(game.NextShotSide, model.PlayerId);

            ValidateBounds(model);

            HandleErrors();
        }
Example #3
0
        public async Task <IHttpActionResult> AddShot(int gameId, ShotCreateModel model)
        {
            IPrincipal principal = RequestContext.Principal;
            await validationService.ValidateForCreate(principal, gameId, model);

            try
            {
                await shotService.CreateAsync(gameId, model);

                return(NoContent());
            }
            catch (ShotAlreadyExistsException)
            {
                return(Conflict("Shot already exists."));
            }
        }
Example #4
0
 private bool AddShot(GameSide side, ShotCreateModel model)
 {
     ValidateExists(side, model);
     side.Shots.Add(ModelToShot(model));
     return(AreAllShot(side));
 }