private List <UserDashboardEventViewModel> CreateRecentEventsList(IEnumerable <Playthrough> recentlyStartedPlaythroughs, IEnumerable <Playthrough> recentlyFinishedPlaythroughs,
                                                                          IEnumerable <JournalEntry> recentJournalEntries)
        {
            var recentEvents = new List <UserDashboardEventViewModel>();

            foreach (var pt in recentlyStartedPlaythroughs)
            {
                recentEvents.Add(UserDashboardMapper.StartedPlaythroughToRecentEvent(pt));
            }
            foreach (var pt in recentlyFinishedPlaythroughs)
            {
                recentEvents.Add(UserDashboardMapper.FinishedPlaythroughToRecentEvent(pt));
            }
            foreach (var je in recentJournalEntries)
            {
                recentEvents.Add(UserDashboardMapper.JournalEntryToRecentEvent(je));
            }

            recentEvents.Sort((a, b) => a.Date.CompareTo(b.Date));
            recentEvents.Reverse();

            if (recentEvents.Count > 15)
            {
                recentEvents.RemoveRange(15, recentEvents.Count - 15);
            }

            return(recentEvents);
        }
Example #2
0
        public async Task <ActionResult <UserDashboardDTO> > Dashboard()
        {
            var userDashboard = await _bll.AppService.GetUserDashboard(User.GetUserId());

            if (userDashboard == null)
            {
                return(BadRequest("Unknown error occurred"));
            }

            return(UserDashboardMapper.FromBLL(userDashboard));
        }
        public async Task <UserDashboardViewModel> GetUserDashboardAsync(int hoarderID)
        {
            // TODO: Split into separate methods for each section of the dashboard?

            var recentlyStartedPlaythroughs = await playthroughDbService.GetUserRecentlyStartedPlaythroughsAsync(hoarderID);

            var recentlyFinishedPlaythroughs = await playthroughDbService.GetUserRecentlyFinishedPlaythroughsAsync(hoarderID);

            var recentJournalEntries = await journalDbService.GetRecentJournalEntriesAsync(hoarderID);

            var recentEvents = CreateRecentEventsList(recentlyStartedPlaythroughs, recentlyFinishedPlaythroughs, recentJournalEntries);

            var totalPlayTime = await playDataDbService.CountUserTotalPlaytimeAsync(hoarderID);

            var numberOfOwnedGames = await playDataDbService.CountUserOwnedGamesAsync(hoarderID);

            var numberOfDroppedGames = await playDataDbService.CountUserDroppedGamesAsync(hoarderID);

            var numberOfWishlistItems = await wishlistDbService.CountUserWishlistItemsAsync(hoarderID);

            var userDashboardVM = new UserDashboardViewModel
            {
                TotalPlaytime         = PlaytimeHelper.GetLongPlaytimeString(totalPlayTime),
                NumberOfOwnedGames    = numberOfOwnedGames.ToString(),
                NumberOfDroppedGames  = numberOfDroppedGames.ToString(),
                NumberOfWishlistItems = numberOfWishlistItems.ToString(),
                RecentEvents          = recentEvents
            };

            var currentPlaythroughs = await playthroughDbService.GetUserCurrentPlaythroughsAsync(hoarderID);

            foreach (var pt in currentPlaythroughs)
            {
                userDashboardVM.CurrentGames.Add(UserDashboardMapper.ToGameViewModel(pt));
            }

            int finishedGamesTotal = 0;
            int playedGamesTotal   = 0;
            int unplayedGamesTotal = 0;
            int droppedGamesTotal  = 0;

            var platforms = await platformDbService.GetAllAsync();

            foreach (var platform in platforms)
            {
                int platformID           = platform.ID;
                var platformStatisticsVM = UserDashboardMapper.ToPlatformStatisticsViewModel(platform);

                int finishedGames = await playDataDbService.CountUserFinishedGamesByPlatformAsync(hoarderID, platformID);

                int playedGames = await playDataDbService.CountUserPlayedGamesByPlatformAsync(hoarderID, platformID);

                int unplayedGames = await playDataDbService.CountUserUnplayedGamesByPlatformAsync(hoarderID, platformID);

                int droppedGames = await playDataDbService.CountUserDroppedGamesByPlatformAsync(hoarderID, platformID);

                int totalGames = finishedGames + playedGames + unplayedGames + droppedGames;

                if (totalGames > 0)
                {
                    int finishedGamesPercentage = CalculatePercentage(totalGames, finishedGames);
                    int playedGamesPercentage   = CalculatePercentage(totalGames, playedGames);
                    int unplayedGamesPercentage = CalculatePercentage(totalGames, unplayedGames);
                    int droppedGamesPercentage  = CalculatePercentage(totalGames, droppedGames);

                    platformStatisticsVM.TotalGamesOwned         = totalGames.ToString();
                    platformStatisticsVM.FinishedGames           = finishedGames.ToString();
                    platformStatisticsVM.FinishedGamesPercentage = finishedGamesPercentage;
                    platformStatisticsVM.PlayedGames             = playedGames.ToString();
                    platformStatisticsVM.PlayedGamesPercentage   = playedGamesPercentage;
                    platformStatisticsVM.UnplayedGames           = unplayedGames.ToString();
                    platformStatisticsVM.UnplayedGamesPercentage = unplayedGamesPercentage;
                    platformStatisticsVM.DroppedGames            = droppedGames.ToString();
                    platformStatisticsVM.DroppedGamesPercentage  = droppedGamesPercentage;

                    userDashboardVM.PlatformStatistics.Add(platformStatisticsVM);

                    finishedGamesTotal += finishedGames;
                    playedGamesTotal   += playedGames;
                    unplayedGamesTotal += unplayedGames;
                    droppedGamesTotal  += droppedGames;
                }
            }

            int totalFinishedGamesPercentage = CalculatePercentage(numberOfOwnedGames, finishedGamesTotal);
            int totalPlayedGamesPercentage   = CalculatePercentage(numberOfOwnedGames, playedGamesTotal);
            int totalUnplayedGamesPercentage = CalculatePercentage(numberOfOwnedGames, unplayedGamesTotal);
            int totalDroppedGamesPercentage  = CalculatePercentage(numberOfOwnedGames, droppedGamesTotal);

            userDashboardVM.TotalGamesFinished           = finishedGamesTotal.ToString();
            userDashboardVM.TotalGamesFinishedPercentage = totalFinishedGamesPercentage;
            userDashboardVM.TotalGamesPlayed             = playedGamesTotal.ToString();
            userDashboardVM.TotalGamesPlayedPercentage   = totalPlayedGamesPercentage;
            userDashboardVM.TotalGamesUnplayed           = unplayedGamesTotal.ToString();
            userDashboardVM.TotalGamesUnplayedPercentage = totalUnplayedGamesPercentage;
            userDashboardVM.TotalGamesDropped            = droppedGamesTotal.ToString();
            userDashboardVM.TotalGamesDroppedPercentage  = totalDroppedGamesPercentage;

            return(userDashboardVM);
        }