public async Task <IActionResult> StartNewGame([FromBody] DtoNewGameInfo newGameInfo, CancellationToken cancellationToken = default(CancellationToken)) { var result = await this.gamesService.StartNewGame(newGameInfo, cancellationToken).ConfigureAwait(false); return(utility.GetActionResult(result)); }
public async Task <VoidAppResult> StartNewGame(DtoNewGameInfo newGameInfo, CancellationToken cancellationToken = default(CancellationToken)) { var opponent = await this.dbContext.Users.SingleOrDefaultAsync(_ => _.Mail == newGameInfo.OpponentMail.ToLower()); if (opponent == null) { var createOpponentResult = await this.loginManager.CreateUser(newGameInfo.OpponentMail, cancellationToken); if (createOpponentResult.IsErrorResult) { return(createOpponentResult.GetVoidAppResult()); } opponent = createOpponentResult.SuccessReturnValue; } var choices = ClassChoice.ChoicesFromClassTypes(newGameInfo.ClassTypes); var newGame = new Game { BeginDate = DateTime.Now, Status = GameStatus.InvitePending, UserParticipations = new List <UserParticipation> { new UserParticipation { User = this.loginManager.LoggedInUser, ClassChoices = choices }, new UserParticipation { User = opponent } }, Entities = new List <Entity> { new Player { Id = Guid.NewGuid(), Health = 20, User = this.loginManager.LoggedInUser }, new Player { Id = Guid.NewGuid(), Health = 20, User = opponent } } }; await this.cardService.InitializeGame(newGame); await this.dbContext.Games.AddAsync(newGame, cancellationToken); await this.dbContext.SaveChangesAsync(cancellationToken); return(VoidAppResult.Success()); }