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(); }
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(); }
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.")); } }
private bool AddShot(GameSide side, ShotCreateModel model) { ValidateExists(side, model); side.Shots.Add(ModelToShot(model)); return(AreAllShot(side)); }