public void ItGetsTheTotalPoints()
        {
            using (IDataContext dataContext = new NemeStatsDataContext())
            {
                IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever); 
                PlayerStatistics playerStatistics = playerRetriever.GetPlayerStatistics(testPlayer1.Id);

                int totalBasePoints = 0;
                int totalGameDurationPoints = 0;
                int totalWeightBonusPoints = 0;

                foreach(PlayedGame playedGame in testPlayedGames)
                {
                    if(playedGame.PlayerGameResults.Any(result => result.PlayerId == testPlayer1.Id))
                    {
                        var playerGameResult = playedGame.PlayerGameResults.First(result => result.PlayerId == testPlayer1.Id);
                        totalBasePoints += playerGameResult.NemeStatsPointsAwarded;
                        totalGameDurationPoints += playerGameResult.GameDurationBonusPoints;
                        totalWeightBonusPoints += playerGameResult.GameWeightBonusPoints;
                    }
                }

                Assert.AreEqual(totalBasePoints, playerStatistics.NemePointsSummary.BaseNemePoints);
                Assert.AreEqual(totalGameDurationPoints, playerStatistics.NemePointsSummary.GameDurationBonusNemePoints);
                Assert.AreEqual(totalWeightBonusPoints, playerStatistics.NemePointsSummary.WeightBonusNemePoints);
            }
        }
        public void ItGetsTheGamesPlayedMetrics()
        {
            using (IDataContext dataContext = new NemeStatsDataContext())
            {
                IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
                PlayerStatistics playerStatistics = playerRetriever.GetPlayerStatistics(testPlayer1.Id);

                int totalGamesForPlayer1 = testPlayedGames
                    .Count(playedGame => playedGame.PlayerGameResults
                                                   .Any(playerGameResult => playerGameResult.PlayerId == testPlayer1.Id));
                Assert.That(playerStatistics.TotalGames, Is.EqualTo(totalGamesForPlayer1));

                int totalWinsForPlayer1 = testPlayedGames
                    .Count(playedGame => playedGame.PlayerGameResults
                                                   .Any(playerGameResult => playerGameResult.PlayerId == testPlayer1.Id && playerGameResult.GameRank == 1));
                Assert.That(playerStatistics.TotalGamesWon, Is.EqualTo(totalWinsForPlayer1));

                int totalLossesForPlayer1 = testPlayedGames
                    .Count(playedGame => playedGame.PlayerGameResults
                                                   .Any(playerGameResult => playerGameResult.PlayerId == testPlayer1.Id && playerGameResult.GameRank != 1));
                Assert.That(playerStatistics.TotalGamesLost, Is.EqualTo(totalLossesForPlayer1));

                int winPercentageForPlayer1 = (int)((decimal)totalWinsForPlayer1 / (totalGamesForPlayer1) * 100);

                Assert.That(playerStatistics.WinPercentage, Is.EqualTo(winPercentageForPlayer1));
            }

        }
        public override void FixtureSetUp()
        {
            base.FixtureSetUp();

            dataContext = new NemeStatsDataContext();
            IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
            IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
            playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
            player1Details = playerRetriever.GetPlayerDetails(testPlayer1.Id, 0);
            player5Details = playerRetriever.GetPlayerDetails(testPlayer5.Id, 0);
        }
        public void SetUp()
        {
            using(NemeStatsDataContext dataContext = new NemeStatsDataContext())
            {
                var playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                var cacheableGameDataRetriever = new BoardGameGeekGameDefinitionInfoRetriever(new DateUtilities(), new CacheService(), dataContext);

                retriever = new GameDefinitionRetriever(dataContext, playerRepository, cacheableGameDataRetriever);
                actualGameDefinitionSummaries = retriever.GetAllGameDefinitions(testUserWithDefaultGamingGroup.CurrentGamingGroupId);
            }
        }
        public override void FixtureSetUp()
        {
            base.FixtureSetUp();

            using (dbContext = new NemeStatsDbContext())
            {
                using (dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    var playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                    var cacheableGameDataRetriever = new BoardGameGeekGameDefinitionInfoRetriever(new DateUtilities(), new CacheService(), dataContext);

                    var gameDefinitionRetriever = new GameDefinitionRetriever(dataContext, playerRepository, cacheableGameDataRetriever);
                    gameDefinitionSummary = gameDefinitionRetriever.GetGameDefinitionDetails(
                        testGameDefinition.Id, 
                        numberOfGamesToRetrieve);
                }
            }
        }
        public void ItEagerlyFetchesGameDefinitions()
        {
            using (NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                using (IDataContext dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    dbContext.Configuration.LazyLoadingEnabled = false;
                    dbContext.Configuration.ProxyCreationEnabled = false;
                    INemesisHistoryRetriever nemesisHistoryRetriever = new NemesisHistoryRetriever(dataContext);
                    IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                    IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                    IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
                    PlayerDetails testPlayerDetails = playerRetriever.GetPlayerDetails(testPlayer1.Id, 1);

                    Assert.NotNull(testPlayerDetails.PlayerGameResults.First().PlayedGame.GameDefinition);
                }
            }
        }
        public override void FixtureSetUp()
        {
            base.FixtureSetUp();

            dataContext = new NemeStatsDataContext();
            var playerRepository = new EntityFrameworkPlayerRepository(dataContext);
            boardGameGeekGameDefinitionInfoRetriever = new BoardGameGeekGameDefinitionInfoRetriever(new DateUtilities(), new CacheService(), dataContext);
            gameDefinitionRetriever = new GameDefinitionRetriever(dataContext, playerRepository, boardGameGeekGameDefinitionInfoRetriever);

            gameDefinition = gameDefinitionRetriever.GetGameDefinitionDetails(testGameDefinitionWithOtherGamingGroupId.Id,
                0);
            championlessGameDefinition = gameDefinitionRetriever.GetGameDefinitionDetails(gameDefinitionWithNoChampion.Id, 0);

            // Player ID 1 has a winning percentage high enough to be considered the champion
            championPlayerIdForGameDefinition = testPlayer7WithOtherGamingGroupId.Id;

            // Player ID 9 has a higher winning percentage than player 7, but is not active
            otherChampionPlayerIdForGameDefinition = testPlayer9UndefeatedWith5Games.Id;
        }
 public HotStreakAchievement(IDataContext dataContext, EntityFrameworkPlayerRepository entityFrameworkPlayerRepository) : base(dataContext)
 {
     _entityFrameworkPlayerRepository = entityFrameworkPlayerRepository;
 }
        public void ItThrowsAnEntityDoesNotExistExceptionIfTheIdIsntValid()
        {
            using (dbContext = new NemeStatsDbContext())
            {
                using (dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    var playerRepository = new EntityFrameworkPlayerRepository(dataContext);

                    int invalidId = -1;
                    var expectedException = new EntityDoesNotExistException(typeof(GameDefinition), invalidId);
                    var cacheableGameDataRetriever = new BoardGameGeekGameDefinitionInfoRetriever(new DateUtilities(), new CacheService(), dataContext);

                    var gameDefinitionRetriever = new GameDefinitionRetriever(dataContext, playerRepository, cacheableGameDataRetriever);

                    var actualException = Assert.Throws<EntityDoesNotExistException>(() => 
                        gameDefinitionRetriever.GetGameDefinitionDetails(invalidId, 0));

                    actualException.Message.ShouldBe(expectedException.Message);
                }
            }
        }
        public void TryGamingGroup1()
        {
            using (NemeStatsDataContext dataContext = new NemeStatsDataContext())
            {
                var playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                var cacheableGameDataRetriever = new BoardGameGeekGameDefinitionInfoRetriever(new DateUtilities(), new CacheService(), dataContext);

                retriever = new GameDefinitionRetriever(dataContext, playerRepository, cacheableGameDataRetriever);
                IList<GameDefinitionSummary> gameDefinitionSummaries = retriever.GetAllGameDefinitions(1);

                foreach (GameDefinitionSummary summary in gameDefinitionSummaries)
                {
                    if (summary.ChampionId != null)
                    {
                        Assert.That(summary.Champion, Is.Not.Null);
                        Assert.That(summary.Champion.Player, Is.Not.Null);
                    }
                }
            }
        }
        public void ItSetsPlayerStatistics()
        {
            using (NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                using (IDataContext dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    INemesisHistoryRetriever nemesisHistoryRetriever = new NemesisHistoryRetriever(dataContext);
                    IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                    IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                    IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext,  playerRepository, playedGameRetriever);
                    PlayerDetails playerDetails = playerRetriever.GetPlayerDetails(testPlayer1.Id, 1);

                    Assert.NotNull(playerDetails.PlayerStats);
                }
            }
        }
        public void ItOrdersPlayerGameResultsByTheDatePlayedDescending()
        {
            using (NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                using (IDataContext dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    int numberOfGamesToRetrieve = 3;

                    INemesisHistoryRetriever nemesisHistoryRetriever = new NemesisHistoryRetriever(dataContext);
                    IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                    IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                    IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
                    PlayerDetails playerDetails = playerRetriever.GetPlayerDetails(testPlayer1.Id, numberOfGamesToRetrieve);

                    long lastTicks = long.MaxValue; ;
                    Assert.IsTrue(playerDetails.PlayerGameResults.Count == numberOfGamesToRetrieve);
                    foreach (PlayerGameResult result in playerDetails.PlayerGameResults)
                    {
                        Assert.GreaterOrEqual(lastTicks, result.PlayedGame.DatePlayed.Ticks);

                        lastTicks = result.PlayedGame.DatePlayed.Ticks;
                    }
                }
            }
        }
        public void ItOnlyGetsTheSpecifiedNumberOfRecentGames()
        {
            using (NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                using (IDataContext dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    int numberOfGamesToRetrieve = 1;

                    INemesisHistoryRetriever nemesisHistoryRetriever = new NemesisHistoryRetriever(dataContext);
                    IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                    IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                    IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
                    PlayerDetails playerDetails = playerRetriever.GetPlayerDetails(testPlayer1.Id, numberOfGamesToRetrieve);

                    Assert.AreEqual(numberOfGamesToRetrieve, playerDetails.PlayerGameResults.Count);
                }
            }
        }
        public void ItGetsTheAveragePlayersPerGame()
        {
            using (IDataContext dataContext = new NemeStatsDataContext())
            {
                IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever);
                PlayerStatistics playerStatistics = playerRetriever.GetPlayerStatistics(testPlayer1.Id);

                float averagePlayersPerGame = (float)testPlayedGames.Where(game => game.PlayerGameResults.Any(result => result.PlayerId == testPlayer1.Id))
                    .Average(game => game.NumberOfPlayers);

                Assert.AreEqual(averagePlayersPerGame, playerStatistics.AveragePlayersPerGame);
            }
        }
        private void CreatePlayedGames(NemeStatsDataContext dataContext)
        {
            var playerRepository = new EntityFrameworkPlayerRepository(dataContext);
            var nemesisRecalculator = new NemesisRecalculator(dataContext, playerRepository);
            var championRepository = new ChampionRepository(dataContext);
            var championRecalculator = new ChampionRecalculator(dataContext, championRepository);
            var securedEntityValidator = new SecuredEntityValidator(dataContext);
            var weightBonusCalculator = new WeightBonusCalculator(new WeightTierCalculator());
            var pointsCalculator = new PointsCalculator(weightBonusCalculator, new GameDurationBonusCalculator());
            var linkedPlayedGameValidator = new LinkedPlayedGameValidator(dataContext);
            var applicationLinker = new ApplicationLinker(dataContext);

            IPlayedGameSaver playedGameSaver = new PlayedGameSaver(
                dataContext, 
                playedGameTracker, 
                nemesisRecalculator, 
                championRecalculator, 
                securedEntityValidator,
                pointsCalculator,
                new FakeEventBus(),
                linkedPlayedGameValidator,
                applicationLinker);
            
            List<Player> players = new List<Player> { testPlayer1, testPlayer2 };
            List<int> playerRanks = new List<int> { 1, 1 };
            PlayedGame playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer1, testPlayer2, testPlayer3 };
            playerRanks = new List<int> { 1, 2, 3 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer1, testPlayer3, testPlayer2 };
            playerRanks = new List<int> { 1, 2, 3 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer3, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            //make player4 beat player 1 three times
            players = new List<Player> { testPlayer4, testPlayer1, testPlayer2, testPlayer3 };
            playerRanks = new List<int> { 1, 2, 3, 4 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer4, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer4, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            //--make the inactive player5 beat player1 3 times
            players = new List<Player> { testPlayer5, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer5, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer5, testPlayer1 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            //make player 2 be the only one who beat player 5
            players = new List<Player> { testPlayer2, testPlayer5 };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinition.Id, players, playerRanks, testUserWithDefaultGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            //--create games that have a different GamingGroupId and testPlayer7 being the champion
            players = new List<Player> { testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer7WithOtherGamingGroupId, testPlayer8WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer7WithOtherGamingGroupId, testPlayer8WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer7WithOtherGamingGroupId, testPlayer8WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer7WithOtherGamingGroupId, testPlayer8WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer8WithOtherGamingGroupId, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(testGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer9UndefeatedWith5Games, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(anotherTestGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer9UndefeatedWith5Games, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(anotherTestGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer9UndefeatedWith5Games, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(anotherTestGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer9UndefeatedWith5Games, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(anotherTestGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);

            players = new List<Player> { testPlayer9UndefeatedWith5Games, testPlayer7WithOtherGamingGroupId };
            playerRanks = new List<int> { 1, 2 };
            playedGame = CreateTestPlayedGame(anotherTestGameDefinitionWithOtherGamingGroupId.Id, players, playerRanks, testUserWithOtherGamingGroup, playedGameSaver);
            testPlayedGames.Add(playedGame);
        }