예제 #1
0
        public async Task <IActionResult> Index(
            int page      = 1,
            int pageSize  = 10,
            string mode   = "",
            string region = ""
            )
        {
            // Create the view model with initial values we already know.
            var vm = new LeaderboardViewModel
            {
                Page           = page,
                PageSize       = pageSize,
                SelectedMode   = mode,
                SelectedRegion = region,

                GameModes = new List <string>()
                {
                    "Solo",
                    "Duo",
                    "Trio"
                },

                GameRegions = new List <string>()
                {
                    "Milky Way",
                    "Andromeda",
                    "Pinwheel",
                    "NGC 1300",
                    "Messier 82",
                }
            };

            try
            {
                // Fetch the total number of results in the background.
                var countItemsTask = _dbRespository.CountScoresAsync(mode, region);

                // Fetch the scores that match the current filter.
                IEnumerable <Score> scores = await _dbRespository.GetScoresAsync(mode, region, page, pageSize);

                // Wait for the total count.
                vm.TotalResults = await countItemsTask;

                // Fetch the user profile for each score.
                // This creates a list that's parallel with the scores collection.
                var profiles = new List <Task <Profile> >();
                foreach (var score in scores)
                {
                    profiles.Add(_dbRespository.GetProfileAsync(score.ProfileId));
                }
                Task <Profile> .WaitAll(profiles.ToArray());

                // Combine each score with its profile.
                vm.Scores = scores.Zip(profiles, (score, profile) => new ScoreProfile {
                    Score = score, Profile = profile.Result
                });

                return(View(vm));
            }
            catch (Exception ex)
            {
                return(View(vm));
            }
        }