public List<PlayedGame> GetRecentGames(int numberOfGames, int gamingGroupId, IDateRangeFilter dateRangeFilter = null) { if(dateRangeFilter == null) { dateRangeFilter = new BasicDateRangeFilter(); } List<PlayedGame> playedGames = dataContext.GetQueryable<PlayedGame>() .Where(game => game.GamingGroupId == gamingGroupId && game.DatePlayed >= dateRangeFilter.FromDate && game.DatePlayed <= dateRangeFilter.ToDate) .Include(playedGame => playedGame.GameDefinition.BoardGameGeekGameDefinition) .Include(playedGame => playedGame.GamingGroup) .Include(playedGame => playedGame.PlayerGameResults .Select(playerGameResult => playerGameResult.Player)) .OrderByDescending(orderBy => orderBy.DatePlayed) .ThenByDescending(orderBy => orderBy.DateCreated) .Take(numberOfGames) .ToList(); //TODO this seems ridiculous but I can't see how to order a related entity in Entity Framework :( foreach (PlayedGame playedGame in playedGames) { playedGame.PlayerGameResults = playedGame.PlayerGameResults.OrderBy(orderBy => orderBy.GameRank).ToList(); } return playedGames; }
public virtual List<GameDefinitionSummary> GetGameDefinitionSummaries(List<int> gameDefinitionIds) { var dateRangeFilter = new BasicDateRangeFilter(); var gameDefinitionSummaries = _dataContext.GetQueryable<GameDefinition>() .Where(gameDefinition => gameDefinitionIds.Contains(gameDefinition.Id)) .Select(gameDefinition => new GameDefinitionSummary { Active = gameDefinition.Active, BoardGameGeekGameDefinitionId = gameDefinition.BoardGameGeekGameDefinitionId, Name = gameDefinition.Name, Description = gameDefinition.Description, GamingGroupId = gameDefinition.GamingGroupId, Id = gameDefinition.Id, PlayedGames = gameDefinition.PlayedGames.Where( playedGame => playedGame.DatePlayed >= ((IDateRangeFilter)dateRangeFilter).FromDate && playedGame.DatePlayed <= ((IDateRangeFilter)dateRangeFilter).ToDate) .ToList(), Champion = gameDefinition.Champion, ChampionId = gameDefinition.ChampionId, PreviousChampion = gameDefinition.PreviousChampion, PreviousChampionId = gameDefinition.PreviousChampionId, DateCreated = gameDefinition.DateCreated, BoardGameGeekGameDefinition = gameDefinition.BoardGameGeekGameDefinition }) .ToList(); gameDefinitionSummaries.ForEach(x => x.BoardGameGeekInfo = x.BoardGameGeekGameDefinitionId.HasValue ? _boardGameGeekGameDefinitionInfoRetriever.GetResults(x.BoardGameGeekGameDefinitionId.Value) : null); return gameDefinitionSummaries; }
public void ItOnlyReturnsGamesPlayedWithinTheSpecifiedDateRangeFilter() { var expectedDateRangeFilter = new BasicDateRangeFilter { FromDate = new System.DateTime(2015, 1, 1), ToDate = new System.DateTime(2015, 1, 1), }; AssertResultsFilteredUsingThisDateRangeFilter(expectedDateRangeFilter, expectedDateRangeFilter); }
public GamingGroupFilter(IDateRangeFilter dateRangeFilter = null) { if(dateRangeFilter == null) { DateRangeFilter = new BasicDateRangeFilter(); }else { DateRangeFilter = dateRangeFilter; } }
public List<PlayerWithNemesis> GetAllPlayersWithNemesisInfo(int gamingGroupId, IDateRangeFilter dateRangeFilter = null) { if (dateRangeFilter == null) { dateRangeFilter = new BasicDateRangeFilter(); } var playersWithNemesis = (from Player player in GetAllPlayersInGamingGroupQueryable(gamingGroupId) .Include(player => player.PlayerGameResults) select new PlayerWithNemesis { PlayerId = player.Id, ApplicationUserId = player.ApplicationUserId, PlayerName = player.Name, PlayerActive = player.Active, PlayerRegistered = !string.IsNullOrEmpty(player.ApplicationUserId), NemesisPlayerId = player.Nemesis == null ? (int?)null : player.Nemesis.NemesisPlayerId, NemesisPlayerName = player.Nemesis != null && player.Nemesis.NemesisPlayer != null ? player.Nemesis.NemesisPlayer.Name : null, PreviousNemesisPlayerId = player.PreviousNemesis == null ? (int?)null : player.PreviousNemesis.NemesisPlayerId, PreviousNemesisPlayerName = player.PreviousNemesis != null && player.PreviousNemesis.NemesisPlayer != null ? player.PreviousNemesis.NemesisPlayer.Name : null, GamingGroupId = player.GamingGroupId, GamesWon = player.PlayerGameResults.Where( x => x.PlayedGame.DatePlayed >= dateRangeFilter.FromDate && x.PlayedGame.DatePlayed <= dateRangeFilter.ToDate).Count(x => x.GameRank == 1), GamesLost = player.PlayerGameResults.Where( x => x.PlayedGame.DatePlayed >= dateRangeFilter.FromDate && x.PlayedGame.DatePlayed <= dateRangeFilter.ToDate).Count(x => x.GameRank > 1), //only get championed games where this player is the current champion TotalChampionedGames = player.ChampionedGames.Count( champion => champion.GameDefinition.ChampionId != null && champion.GameDefinition.ChampionId.Value == champion.Id) } ).ToList(); PopulateNemePointsSummary(gamingGroupId, playersWithNemesis, dateRangeFilter); PopulateAchivements(playersWithNemesis); return playersWithNemesis//--deliberately ToList() first since Linq To Entities cannot support ordering by NemePointsSummary.TotalPoints .OrderByDescending(x => x.PlayerActive) .ThenByDescending(pwn => pwn.NemePointsSummary?.TotalPoints ?? 0) .ThenByDescending(pwn => pwn.GamesWon) .ThenBy(pwn => pwn.PlayerName) .ToList(); }
public void ItUsesTheDefaultDateRangeIfNoneIsSpecified() { var expectedDateRange = new BasicDateRangeFilter(); autoMocker.ClassUnderTest.GetGamingGroupSummary(0); autoMocker.Get<IGamingGroupRetriever>().AssertWasCalled( mock => mock.GetGamingGroupDetails( Arg<GamingGroupFilter>.Matches(x => x.DateRangeFilter.FromDate == expectedDateRange.FromDate && x.DateRangeFilter.ToDate == expectedDateRange.ToDate))); }
public override void SetUp() { base.SetUp(); gamingGroupSummary = new GamingGroupSummary() { PlayedGames = new List<PlayedGame>() }; _gamingGroupViewModel = new GamingGroupViewModel(); dateRangeFilter = new BasicDateRangeFilter(); autoMocker.ClassUnderTest.Expect(mock => mock.GetGamingGroupSummary( Arg<int>.Is.Anything, Arg<IDateRangeFilter>.Is.Anything)) .Repeat.Once() .Return(gamingGroupSummary); autoMocker.Get<IGamingGroupViewModelBuilder>().Expect(mock => mock.Build(gamingGroupSummary, currentUser)) .Return(_gamingGroupViewModel); }
private void AssertResultsFilteredUsingThisDateRangeFilter(BasicDateRangeFilter expectedDateRangeFilter, BasicDateRangeFilter actualFilterPassed = null) { int playedGameIdForTooOldGame = 1; int expectedPlayedGameId = 2; int playedGameIdForTooNewGame = 3; var queryable = new List<GameDefinition> { new GameDefinition { GamingGroupId = gamingGroupId, PlayedGames = new List<PlayedGame> { new PlayedGame { Id = playedGameIdForTooOldGame, DatePlayed = expectedDateRangeFilter.FromDate.AddDays(-1) }, new PlayedGame { Id = expectedPlayedGameId, DatePlayed = expectedDateRangeFilter.FromDate }, new PlayedGame { Id = playedGameIdForTooNewGame, DatePlayed = expectedDateRangeFilter.ToDate.AddDays(1) } } } }.AsQueryable(); autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<GameDefinition>()) .Repeat.Once() .Return(queryable); var result = autoMocker.ClassUnderTest.GetAllGameDefinitions(currentUser.CurrentGamingGroupId, actualFilterPassed)[0]; Assert.True(result.PlayedGames.All(playedGame => playedGame.Id == expectedPlayedGameId)); }
public virtual ActionResult Details(int id, ApplicationUser currentUser, [System.Web.Http.FromUri]BasicDateRangeFilter dateRangeFilter = null) { if (dateRangeFilter == null) { dateRangeFilter = new BasicDateRangeFilter(); } string errorMessage; if (!dateRangeFilter.IsValid(out errorMessage)) { ModelState.AddModelError("dateRangeFilter", errorMessage); } var gamingGroupSummary = GetGamingGroupSummary(id, dateRangeFilter); var viewModel = gamingGroupViewModelBuilder.Build(gamingGroupSummary, currentUser); viewModel.PlayedGames.ShowSearchLinkInResultsHeader = true; viewModel.DateRangeFilter = dateRangeFilter; viewModel.UserCanEdit = currentUser.CurrentGamingGroupId == id; ViewBag.RecentGamesSectionAnchorText = SECTION_ANCHOR_RECENT_GAMES; ViewBag.PlayerSectionAnchorText = SECTION_ANCHOR_PLAYERS; ViewBag.GameDefinitionSectionAnchorText = SECTION_ANCHOR_GAMEDEFINITIONS; return View(MVC.GamingGroup.Views.Details, viewModel); }
public void ItOnlyReturnsGamesPlayedWithinTheDefaultBasicDateRangeFilterIfNoDateRangeFilterIsSpecified() { var expectedDateRangeFilter = new BasicDateRangeFilter(); AssertResultsFilteredUsingThisDateRangeFilter(expectedDateRangeFilter); }
public void ItFiltersPlayedGamesThatHappenedBeforeTheFromDate() { var autoMocker = new RhinoAutoMocker<PlayerRetriever>(); autoMocker.PartialMockTheClassUnderTest(); var gamingGroupId = 1; var fromDate = new DateTime(2015, 6, 1); var expectedNemePointsAwardedForEachGame = 50; var queryable = new List<Player> { new Player { GamingGroupId = gamingGroupId, PlayerGameResults = new List<PlayerGameResult> { new PlayerGameResult { GameRank = 2, PlayedGame = new PlayedGame { DatePlayed = fromDate.AddDays(-1) }, NemeStatsPointsAwarded = expectedNemePointsAwardedForEachGame }, new PlayerGameResult { GameRank = 1, PlayedGame = new PlayedGame { DatePlayed = fromDate.AddDays(1) }, NemeStatsPointsAwarded = expectedNemePointsAwardedForEachGame }, new PlayerGameResult { GameRank = 1, PlayedGame = new PlayedGame { DatePlayed = fromDate.AddDays(-1) }, NemeStatsPointsAwarded = expectedNemePointsAwardedForEachGame } }, ChampionedGames = new List<Champion>() } }.AsQueryable(); autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<Player>()) .Return(queryable); autoMocker.ClassUnderTest.Expect(mock => mock.PopulateNemePointsSummary(Arg<int>.Is.Anything, Arg<List<PlayerWithNemesis>>.Is.Anything, Arg<IDateRangeFilter>.Is.Anything)); var dateRangeFilter = new BasicDateRangeFilter { FromDate = fromDate }; var players = autoMocker.ClassUnderTest.GetAllPlayersWithNemesisInfo(gamingGroupId, dateRangeFilter); Assert.That(players.Count, Is.EqualTo(1)); var player = players.First(); Assert.That(player.GamesLost, Is.EqualTo(0)); Assert.That(player.GamesWon, Is.EqualTo(1)); }
public void ItOnlySumsUpPointsFromGamesThatHappenedBeforeTheEndDate() { //--arrange var playersWithNemeses = new List<PlayerWithNemesis> { new PlayerWithNemesis { PlayerId = _playerOneId } }; _playerGameResults.Add(MakePlayerGameResult(_playerOneId)); _autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<PlayerGameResult>()).Return(_playerGameResults.AsQueryable()); var dateRangeFilter = new BasicDateRangeFilter { ToDate = DateTime.Now.AddDays(-2) }; //--act _autoMocker.ClassUnderTest.PopulateNemePointsSummary(_expectedGamingGroupId, playersWithNemeses, dateRangeFilter); //--assert Assert.That(playersWithNemeses[0].NemePointsSummary, Is.Not.Null); var actualNemePointsSummary = playersWithNemeses[0].NemePointsSummary; Assert.That(actualNemePointsSummary.GameDurationBonusNemePoints, Is.EqualTo(0)); Assert.That(actualNemePointsSummary.BaseNemePoints, Is.EqualTo(0)); Assert.That(actualNemePointsSummary.WeightBonusNemePoints, Is.EqualTo(0)); }
internal virtual GamingGroupSummary GetGamingGroupSummary(int gamingGroupId, IDateRangeFilter dateRangeFilter = null) { if (dateRangeFilter == null) { dateRangeFilter = new BasicDateRangeFilter(); } else { dateRangeFilter.FromDate = dateRangeFilter.FromDate; dateRangeFilter.ToDate = dateRangeFilter.ToDate; } var filter = new GamingGroupFilter(dateRangeFilter) { NumberOfRecentGamesToShow = MAX_NUMBER_OF_RECENT_GAMES, GamingGroupId = gamingGroupId }; return gamingGroupRetriever.GetGamingGroupDetails(filter); }
public void SetUp() { filter = new BasicDateRangeFilter(); }
public virtual IList<GameDefinitionSummary> GetAllGameDefinitions(int gamingGroupId, IDateRangeFilter dateRangeFilter = null) { if (dateRangeFilter == null) { dateRangeFilter = new BasicDateRangeFilter(); } var returnValue = _dataContext.GetQueryable<GameDefinition>() .Where(gameDefinition => gameDefinition.GamingGroupId == gamingGroupId && gameDefinition.Active) .Select(gameDefinition => new GameDefinitionSummary { Active = gameDefinition.Active, BoardGameGeekGameDefinitionId = gameDefinition.BoardGameGeekGameDefinitionId, Name = gameDefinition.Name, Description = gameDefinition.Description, GamingGroupId = gameDefinition.GamingGroupId, Id = gameDefinition.Id, PlayedGames = gameDefinition.PlayedGames.Where( playedGame => playedGame.DatePlayed >= dateRangeFilter.FromDate && playedGame.DatePlayed <= dateRangeFilter.ToDate) .ToList(), Champion = gameDefinition.Champion, ChampionId = gameDefinition.ChampionId, PreviousChampion = gameDefinition.PreviousChampion, PreviousChampionId = gameDefinition.PreviousChampionId, DateCreated = gameDefinition.DateCreated }) .OrderBy(game => game.Name) .ToList(); AddPlayersToChampionData(returnValue); returnValue.ForEach(summary => { summary.Champion = summary.Champion ?? new NullChampion(); summary.PreviousChampion = summary.PreviousChampion ?? new NullChampion(); summary.TotalNumberOfGamesPlayed = summary.PlayedGames.Count; if (summary.BoardGameGeekGameDefinitionId.HasValue) { summary.BoardGameGeekInfo = _boardGameGeekGameDefinitionInfoRetriever.GetResults(summary.BoardGameGeekGameDefinitionId.Value); } }); return returnValue; }