public async Task <bool> StartGameAsync(string gameId) { if (await GameAlreadyExistsAsync(gameId).ConfigureAwait(false)) { return(false); } //Get game var game = await _GameDataAccess.GetGameAsync(gameId).ConfigureAwait(false); if (game == null) { return(false); } if (game.State == GameStatusEnum.Finished || game.State == GameStatusEnum.Cancelled) { // Game already finished or cancelled return(false); } //Get game variant var gameVariant = await _GameVariantDataAccess.GetGameVariantAsync(game.GameVariantId).ConfigureAwait(false); if (gameVariant == null) { return(false); } if (game.PlayerIds.Count != gameVariant.PlayerCount) { // Not all players available return(false); } if (game.State == GameStatusEnum.WaitingForPlayers) { if (game.PlayerRoles.Count != game.PlayerIds.Count) { // clear roles in DB var clearRoles = await _GameDataAccess.ClearRolesAsync(gameId).ConfigureAwait(false); if (!clearRoles) { // Roles couldn't be cleared return(false); } // Assign random roles var random = new Random(); int index = random.Next(game.PlayerIds.Count); var productOwner = game.PlayerIds[index]; game.PlayerRoles.Add(productOwner, GameRole.ProductOwner); var addProductOwner = await _GameDataAccess.AssignRoleToPlayerAsync(gameId, productOwner, GameRole.ProductOwner).ConfigureAwait(false); if (!addProductOwner) { // Product owner wasnt added to db return(false); } var clonedPlayers = game.PlayerIds.ToList(); clonedPlayers.Remove(productOwner); foreach (var stakeholder in clonedPlayers) { game.PlayerRoles.Add(stakeholder, GameRole.Stakeholder); var addStakeholder = await _GameDataAccess.AssignRoleToPlayerAsync(gameId, stakeholder, GameRole.Stakeholder).ConfigureAwait(false); if (!addStakeholder) { // Stakeholder wasnt added to db return(false); } } } } // Get players var playerDictionary = new Dictionary <string, GameRole>(); using (var scope = _ServiceProvider.CreateScope()) { var userLogic = scope.ServiceProvider.GetService(typeof(IUserLogic)) as IUserLogic; foreach (var player in game.PlayerRoles) { var userId = player.Key; var role = player.Value; var user = await userLogic.GetUserByIdAsync(userId).ConfigureAwait(false); playerDictionary.Add(user.UserId, role); } } if (playerDictionary.Count != game.PlayerRoles.Count) { // Not all players exist return(false); } if (playerDictionary.Values.Where(a => a == GameRole.ProductOwner).Count() > 1) { // More than one product Owner return(false); } // Add GameProcessor for game IGameProcessor gameProcessor = new GameProcessor(game, gameVariant, playerDictionary, _Mapper); // Subscribe to GameProcessor events gameProcessor.GameFinishedAsyncCallback = OnGameFinishedAsync; gameProcessor.SaveGameStatusAsyncCallback = OnSaveGameStatusAsyncCallback; gameProcessor.SaveGamePhaseAsyncCallback = OnSaveGamePhaseAsyncCallback; gameProcessor.SaveDecisionAsyncCallback = OnSaveDecisionAsyncCallback; gameProcessor.GameViewUpdatedAsyncCallback = OnGameViewUpdatedAsyncCallback; await AddGameAsync(game.GameId, gameProcessor).ConfigureAwait(false); await gameProcessor.StartAsync().ConfigureAwait(false); return(true); }