public void EnsureContextIsValid(ApplicationUser user)
        {
            if (user.CurrentGamingGroupId.HasValue)
            {
                var currentGamingGroupIsValid = _dataContext
                                                .GetQueryable <UserGamingGroup>()
                                                .Any(x => x.GamingGroupId == user.CurrentGamingGroupId.Value &&
                                                     x.ApplicationUserId == user.Id &&
                                                     x.GamingGroup.Active);
                if (currentGamingGroupIsValid)
                {
                    return;
                }
            }

            var activeGamingGroup = _dataContext
                                    .GetQueryable <UserGamingGroup>()
                                    .OrderBy(x => x.GamingGroup.DateCreated)
                                    .FirstOrDefault(x => x.ApplicationUserId == user.Id && x.GamingGroup.Active);

            if (activeGamingGroup == null)
            {
                user.CurrentGamingGroupId = null;
            }
            else
            {
                user.CurrentGamingGroupId = activeGamingGroup.GamingGroupId;
            }
            _dataContext.AdminSave(user);
        }
Esempio n. 2
0
        private void CreateOrUpdateAchievement(Player player, IAchievement achievement, PlayerAchievement currentPlayerAchievement,
                                               AchievementAwarded achievementAwarded)
        {
            if (currentPlayerAchievement == null)
            {
                var playerAchievement = new PlayerAchievement
                {
                    DateCreated      = DateTime.UtcNow,
                    LastUpdatedDate  = DateTime.UtcNow,
                    PlayerId         = player.Id,
                    AchievementId    = achievement.Id,
                    AchievementLevel = achievementAwarded.LevelAwarded.Value,
                    RelatedEntities  = achievementAwarded.RelatedEntities
                };

                _dataContext.Save(playerAchievement, new AnonymousApplicationUser());
                _dataContext.CommitAllChanges();

                NotifyPlayer(player, achievement, achievementAwarded.LevelAwarded);
            }
            else
            {
                currentPlayerAchievement.RelatedEntities = achievementAwarded.RelatedEntities;

                if ((int)achievementAwarded.LevelAwarded.Value > (int)currentPlayerAchievement.AchievementLevel)
                {
                    currentPlayerAchievement.AchievementLevel = achievementAwarded.LevelAwarded.Value;
                    currentPlayerAchievement.LastUpdatedDate  = DateTime.UtcNow;
                    _dataContext.AdminSave(currentPlayerAchievement);
                    _dataContext.CommitAllChanges();

                    NotifyPlayer(player, achievement, achievementAwarded.LevelAwarded);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// LinkedPlayedGameValidator should be called before calling this method since validation can occur before saving the PlayedGame and this
        /// method requires the PlayedGame to be saved first.
        /// </summary>
        /// <param name="playedGameId"></param>
        /// <param name="applicationName"></param>
        /// <param name="entityId"></param>
        /// <param name="dataContext"></param>
        public void LinkApplication(int playedGameId, string applicationName, string entityId, IDataContext dataContext)
        {
            var applicationLinkage = new PlayedGameApplicationLinkage
            {
                ApplicationName = applicationName,
                EntityId        = entityId,
                PlayedGameId    = playedGameId
            };

            dataContext.AdminSave(applicationLinkage);
        }
        private int UpdateBoardGameGeekDefinitions(List <BoardGameGeekGameDefinition> boardGameGeekGameDefinitions)
        {
            int totalGamesUpdated = 0;

            foreach (var existingBoardGameGeekGameDefinition in boardGameGeekGameDefinitions)
            {
                //delay between BGG calls to decrease likelyhood of getting blocked by BGG
                Thread.Sleep(400);
                var gameDetails = _boardGameGeekApiClient.GetGameDetails(existingBoardGameGeekGameDefinition.Id);
                if (gameDetails != null)
                {
                    UpdateBGGDefScalars(existingBoardGameGeekGameDefinition, gameDetails);
                    foreach (var gameCategory in gameDetails.Categories)
                    {
                        if (CategoryDoesntExistInGamesCategoryList(existingBoardGameGeekGameDefinition, gameCategory.Category))
                        {
                            var category = GetOrCreateGameCategory(gameCategory);
                            existingBoardGameGeekGameDefinition.Categories.Add(category);
                        }
                    }
                    foreach (var gameMechanic in gameDetails.Mechanics)
                    {
                        if (MechanicDoesntExistInGamesMechanicsList(existingBoardGameGeekGameDefinition, gameMechanic.Mechanic))
                        {
                            var mechanic = GetOrCreateMechanic(gameMechanic);
                            existingBoardGameGeekGameDefinition.Mechanics.Add(mechanic);
                        }
                    }
                    _dataContext.AdminSave(existingBoardGameGeekGameDefinition);
                    if (totalGamesUpdated++ % 10 == 0)
                    {
                        _dataContext.CommitAllChanges();
                        Debug.WriteLine($@"Last Id Updated was '{existingBoardGameGeekGameDefinition.Id}'. {totalGamesUpdated} BoardGameGeekGameDefinitions updated so far...");
                    }
                }
            }
            _dataContext.CommitAllChanges();
            Debug.WriteLine($@"Done. Updated a total of {totalGamesUpdated} BoardGameGeekGameDefinitions.");
            return(totalGamesUpdated);
        }
        public void RecalculateGamingGroupChampionUsingGamingGroupId(int gamingGroupId)
        {
            var topPlayer = (from playerGameResult in _dataContext.GetQueryable <PlayerGameResult>()
                             where playerGameResult.PlayedGame.GamingGroupId == gamingGroupId
                             group playerGameResult by playerGameResult.PlayerId
                             into groupedResults
                             select
                             new
            {
                TotalPoints = groupedResults.Sum(x => x.TotalPoints),
                PlayerId = groupedResults.Key
            })
                            .Where(x => x.TotalPoints >= MINIMUM_POINTS_TO_BE_GAMING_GROUP_CHAMPION)
                            .OrderByDescending(r => r.TotalPoints)
                            .ThenBy(x => x.PlayerId)
                            .FirstOrDefault();

            if (topPlayer != null)
            {
                var gamingGroup = _dataContext.GetQueryable <GamingGroup>().First(x => x.Id == gamingGroupId);
                gamingGroup.GamingGroupChampionPlayerId = topPlayer.PlayerId;
                _dataContext.AdminSave(gamingGroup);
            }
        }
        public BoardGameGeekGameDefinition CreateBoardGameGeekGameDefinition(int boardGameGeekGameDefinitionId)
        {
            try
            {
                var existingRecord = _dataContext.FindById <BoardGameGeekGameDefinition>(boardGameGeekGameDefinitionId);
                if (existingRecord != null)
                {
                    return(existingRecord);
                }
            }
            catch (EntityDoesNotExistException)
            {
                //this is OK, just means we need to create a new one
            }

            var gameDetails = _boardGameGeekApiClient.GetGameDetails(boardGameGeekGameDefinitionId);

            if (gameDetails == null)
            {
                return(null);
            }

            var newRecord = new BoardGameGeekGameDefinition
            {
                Id            = boardGameGeekGameDefinitionId,
                Name          = gameDetails.Name,
                Thumbnail     = gameDetails.Thumbnail,
                Image         = gameDetails.Image,
                MaxPlayers    = gameDetails.MaxPlayers,
                MinPlayers    = gameDetails.MinPlayers,
                MaxPlayTime   = gameDetails.MaxPlayTime,
                MinPlayTime   = gameDetails.MinPlayTime,
                AverageWeight = gameDetails.AverageWeight,
                Description   = gameDetails.Description,
                YearPublished = gameDetails.YearPublished,
                IsExpansion   = gameDetails.IsExpansion,
                Rank          = gameDetails.Rank
            };

            _dataContext.AdminSave(newRecord);

            // Save categories to BGG definition
            foreach (var category in gameDetails.Categories)
            {
                var existentCategory = _dataContext.GetQueryable <BoardGameGeekGameCategory>().FirstOrDefault(c => c.BoardGameGeekGameCategoryId == category.Id);

                if (existentCategory == null)
                {
                    existentCategory = new BoardGameGeekGameCategory()
                    {
                        BoardGameGeekGameCategoryId = category.Id,
                        CategoryName = category.Category
                    };
                }

                newRecord.Categories.Add(existentCategory);
            }

            foreach (var mechanic in gameDetails.Mechanics)
            {
                var existentMechanic = _dataContext.GetQueryable <BoardGameGeekGameMechanic>().FirstOrDefault(c => c.BoardGameGeekGameMechanicId == mechanic.Id);

                if (existentMechanic == null)
                {
                    existentMechanic = new BoardGameGeekGameMechanic()
                    {
                        BoardGameGeekGameMechanicId = mechanic.Id,
                        MechanicName = mechanic.Mechanic
                    };
                }

                newRecord.Mechanics.Add(existentMechanic);
            }

            return(newRecord);
        }
        private int UpdateBoardGameGeekDefinitions(List <BoardGameGeekGameDefinition> boardGameGeekGameDefinitions)
        {
            int totalGamesUpdated = 0;

            foreach (var existingBoardGameGeekGameDefinition in boardGameGeekGameDefinitions)
            {
                //delay between BGG calls to decrease likelyhood of getting blocked by BGG
                Thread.Sleep(400);
                var gameDetails = _boardGameGeekApiClient.GetGameDetails(existingBoardGameGeekGameDefinition.Id);

                if (gameDetails != null)
                {
                    existingBoardGameGeekGameDefinition.DateUpdated = DateTime.UtcNow;

                    existingBoardGameGeekGameDefinition.AverageWeight = gameDetails.AverageWeight;
                    existingBoardGameGeekGameDefinition.Description   = gameDetails.Description;
                    existingBoardGameGeekGameDefinition.MaxPlayTime   = gameDetails.MaxPlayTime;
                    existingBoardGameGeekGameDefinition.MinPlayTime   = gameDetails.MinPlayTime;
                    existingBoardGameGeekGameDefinition.MaxPlayers    = gameDetails.MaxPlayers;
                    existingBoardGameGeekGameDefinition.MinPlayers    = gameDetails.MinPlayers;
                    existingBoardGameGeekGameDefinition.Name          = gameDetails.Name;
                    existingBoardGameGeekGameDefinition.Thumbnail     = gameDetails.Thumbnail;
                    existingBoardGameGeekGameDefinition.Image         = gameDetails.Image;
                    existingBoardGameGeekGameDefinition.YearPublished = gameDetails.YearPublished;

                    foreach (var gameCategory in gameDetails.Categories)
                    {
                        if (
                            existingBoardGameGeekGameDefinition.Categories.All(
                                c => !c.CategoryName.Equals(gameCategory.Category, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var existentCategory =
                                _dataContext.GetQueryable <BoardGameGeekGameCategory>()
                                .FirstOrDefault(
                                    c =>
                                    c.CategoryName.Equals(gameCategory.Category,
                                                          StringComparison.InvariantCultureIgnoreCase));
                            if (existentCategory == null)
                            {
                                existentCategory = new BoardGameGeekGameCategory()
                                {
                                    BoardGameGeekGameCategoryId = gameCategory.Id,
                                    CategoryName = gameCategory.Category
                                };
                            }

                            existingBoardGameGeekGameDefinition.Categories.Add(existentCategory);
                        }
                    }

                    foreach (var gameMechanic in gameDetails.Mechanics)
                    {
                        if (
                            existingBoardGameGeekGameDefinition.Mechanics.All(
                                c => !c.MechanicName.Equals(gameMechanic.Mechanic, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var existentMechanic =
                                _dataContext.GetQueryable <BoardGameGeekGameMechanic>()
                                .FirstOrDefault(
                                    c =>
                                    c.MechanicName.Equals(gameMechanic.Mechanic,
                                                          StringComparison.InvariantCultureIgnoreCase));
                            if (existentMechanic == null)
                            {
                                existentMechanic = new BoardGameGeekGameMechanic()
                                {
                                    BoardGameGeekGameMechanicId = gameMechanic.Id,
                                    MechanicName = gameMechanic.Mechanic
                                };
                            }

                            existingBoardGameGeekGameDefinition.Mechanics.Add(existentMechanic);
                        }
                    }

                    _dataContext.AdminSave(existingBoardGameGeekGameDefinition);

                    if (totalGamesUpdated++ % 10 == 0)
                    {
                        _dataContext.CommitAllChanges();
                        Console.WriteLine("{0} BoardGameGeekGameDefinitions updated so far...", totalGamesUpdated);
                    }
                }
            }
            _dataContext.CommitAllChanges();

            return(totalGamesUpdated);
        }