public async Task <StartGameResponseView> StartGame(string userId, StartGameView model) { var game = new Game() { UserId = userId, State = UserGameStateType.InGame, NumberOfPlayers = model.NumberOfBots + 1 }; await _gameRepository.CreateAsync(game); var usersPoints = new UsersPoints() { UserId = userId, GameId = game.Id, Points = Constants.GameSettings.InitionalPoints }; await _usersPointsRepository.CreateAsync(usersPoints); var bots = (await _botRepository.GetRandomBotsAsync(model.NumberOfBots)).ToList(); var botsPoints = bots.Select(x => new BotsPoints() { BotId = x.Id, GameId = game.Id, Points = Constants.GameSettings.InitionalPoints, CardsInHand = Constants.GameSettings.InitialCardsInHand }) .ToList(); await _botsPointsRepository.AddRangeAsync(botsPoints); await InitialCardsDeal(game, usersPoints, bots, botsPoints); await _gameRepository.UpdateAsync(game); var response = new StartGameResponseView() { GameId = game.Id, State = (int)game.State }; return(response); }
public async Task <StartGameResponseView> Start(int countBots, string userId) { using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var userInGame = await _unitOfWork.UserInGames.GetUnfinished(userId); var result = new StartGameResponseView(); if (userInGame != null) { result.GameId = userInGame.GameId; return(result); } var game = new Game { CountBots = countBots, WinnerName = String.Empty }; await _unitOfWork.Games.Create(game); var bots = await _unitOfWork.Bots.GetCount(countBots); var deck = await CreateDeck(game); var userSteps = new List <UserStep>(); var botSteps = new List <BotStep>(); var newUserInGame = new UserInGame { GameId = game.Id, UserId = userId, CountPoint = 0 }; var botInGames = bots.Select(x => new BotInGame { BotId = x.Id, GameId = game.Id, CountPoint = 0 }).ToList(); var cardsForRemove = new List <Card>(); DealTwoCards(game, deck, cardsForRemove, userId, userSteps, newUserInGame, bots, botSteps, botInGames); await _unitOfWork.UserSteps.CreateRange(userSteps); await _unitOfWork.BotSteps.CreateRange(botSteps); await _unitOfWork.Cards.DeleteRange(cardsForRemove); await _unitOfWork.UserInGames.Create(newUserInGame); await _unitOfWork.BotInGames.CreateRange(botInGames); result.GameId = game.Id; transactionScope.Complete(); return(result); } }