Example #1
0
        public async Task <IActionResult> Index(int page = 1)
        {
            Domain.Model.Filter filter = new Domain.Model.Filter(page);

            var triggerList = await _triggerService.GetPaginatedListAsync(filter);

            foreach (var trigger in triggerList.Data)
            {
                trigger.AwardBadgeFilename =
                    _pathResolver.ResolveContentPath(trigger.AwardBadgeFilename);
            }

            PaginateViewModel paginateModel = new PaginateViewModel()
            {
                ItemCount    = triggerList.Count,
                CurrentPage  = page,
                ItemsPerPage = filter.Take.Value
            };

            if (paginateModel.MaxPage > 0 && paginateModel.CurrentPage > paginateModel.MaxPage)
            {
                return(RedirectToRoute(
                           new
                {
                    page = paginateModel.LastPage ?? 1
                }));
            }

            TriggersListViewModel viewModel = new TriggersListViewModel()
            {
                Triggers      = triggerList.Data,
                PaginateModel = paginateModel,
            };

            return(View(viewModel));
        }
Example #2
0
        Generate(Site site, int count, int challengePercent, int codePercent, bool quiet)
        {
            int[]   minuteCeilings            = { 60, 120, 500 };
            float[] minuteCeilingDistribution = { 0.85F, 0.1F, 0.05F };

            var userList = await _userService.GetPaginatedUserListAsync(new UserFilter());

            var codeList = await _triggerService.GetPaginatedListAsync(new TriggerFilter
            {
                SecretCodesOnly = true
            });

            var challengeList = await _challengeService
                                .GetPaginatedChallengeListAsync(new ChallengeFilter());

            var activities = new List <GeneratedActivity>();

            var rand = new Bogus.Randomizer();

            if (!quiet)
            {
                Console.Write($"Generating {count} activity items... ");
            }

            ProgressBar progress = quiet ? null : new ProgressBar();

            try
            {
                for (int i = 0; i < count; i++)
                {
                    bool addActivity = false;
                    var  randomUser  = (await _userService.GetPaginatedUserListAsync(new UserFilter
                    {
                        SiteId = site.Id,
                        Skip = rand.Int(0, userList.Count - 1),
                        Take = 1
                    })).Data.First();

                    var act = new GeneratedActivity
                    {
                        User = randomUser,
                    };
                    if (challengePercent > 0 && rand.Int(1, 100) <= challengePercent)
                    {
                        bool isValid = false;
                        int  challengeLookupCount = 0;
                        await _configureUserSite.Lookup(randomUser.Id);

                        _challengeService.ClearCachedUserContext();
                        DataWithCount <IEnumerable <Challenge> > randomChallenge = null;
                        while (!isValid)
                        {
                            challengeLookupCount++;
                            var filter = new ChallengeFilter()
                            {
                                Take = rand.Int(0, challengeList.Count - 1),
                                Skip = 1
                            };
                            randomChallenge = await _challengeService.GetPaginatedChallengeListAsync(filter);

                            if (randomChallenge.Data != null &&
                                randomChallenge.Data.FirstOrDefault() != null)
                            {
                                isValid = randomChallenge.Data.First().IsValid;
                            }
                            if (challengeLookupCount > 20)
                            {
                                _logger.LogError($"Unable to find an eligible challenge for user id {randomUser.Id} after 20 tries, giving up.");
                                randomChallenge = null;
                                addActivity     = false;
                                break;
                            }
                        }
                        if (randomChallenge != null)
                        {
                            var randomTasks = await _challengeService
                                              .GetChallengeTasksAsync(randomChallenge.Data.First().Id);

                            var randomTask = randomTasks
                                             .Skip(rand.Int(0, randomTasks.Count() - 1)).First();
                            randomTask.IsCompleted = true;
                            act.ActivityType       = ActivityType.ChallengeTasks;
                            act.ChallengeId        = randomChallenge.Data.First().Id;
                            act.ChallengeTasks     = new List <ChallengeTask> {
                                randomTask
                            };
                            addActivity = true;
                        }
                    }
                    else
                    {
                        if (codePercent > 0 && rand.Int(1, 100) <= codePercent)
                        {
                            var randomCode = (await _triggerService.GetPaginatedListAsync(new TriggerFilter
                            {
                                SiteId = site.Id,
                                Skip = rand.Int(0, codeList.Count - 1),
                                Take = 1,
                                SecretCodesOnly = true
                            })).Data.First();

                            act.ActivityType = ActivityType.SecretCode;
                            act.SecretCode   = randomCode.SecretCode;
                            addActivity      = true;
                        }
                        else
                        {
                            act.ActivityAmount = rand.Int(1, rand
                                                          .WeightedRandom <int>(minuteCeilings, minuteCeilingDistribution));
                            act.ActivityType = ActivityType.Default;
                            addActivity      = true;
                        }
                    }
                    if (addActivity)
                    {
                        activities.Add(act);
                    }

                    if (progress != null)
                    {
                        progress.Report((double)i / count);
                    }
                }
            }
            finally
            {
                if (progress != null)
                {
                    progress.Dispose();
                    Console.WriteLine();
                }
            }
            return(activities);
        }
Example #3
0
        public async Task <IActionResult> Index(string search,
                                                int?systemId, int?branchId, bool?mine, int?programId, int page = 1)
        {
            var filter = new TriggerFilter(page);

            if (!string.IsNullOrWhiteSpace(search))
            {
                filter.Search = search;
            }

            if (mine == true)
            {
                filter.UserIds = new List <int> {
                    GetId(ClaimType.UserId)
                };
            }
            else if (branchId.HasValue)
            {
                filter.BranchIds = new List <int> {
                    branchId.Value
                };
            }
            else if (systemId.HasValue)
            {
                filter.SystemIds = new List <int> {
                    systemId.Value
                };
            }

            if (programId.HasValue)
            {
                if (programId.Value > 0)
                {
                    filter.ProgramIds = new List <int?> {
                        programId.Value
                    };
                }
                else
                {
                    filter.ProgramIds = new List <int?> {
                        null
                    };
                }
            }

            var triggerList = await _triggerService.GetPaginatedListAsync(filter);

            var paginateModel = new PaginateViewModel
            {
                ItemCount    = triggerList.Count,
                CurrentPage  = page,
                ItemsPerPage = filter.Take.Value
            };

            if (paginateModel.PastMaxPage)
            {
                return(RedirectToRoute(
                           new
                {
                    page = paginateModel.LastPage ?? 1
                }));
            }

            foreach (var trigger in triggerList.Data)
            {
                trigger.AwardBadgeFilename =
                    _pathResolver.ResolveContentPath(trigger.AwardBadgeFilename);
                var graEvent = (await _eventService.GetRelatedEventsForTriggerAsync(trigger.Id))
                               .FirstOrDefault();
                if (graEvent != null)
                {
                    trigger.RelatedEventId   = graEvent.Id;
                    trigger.RelatedEventName = graEvent.Name;
                }
            }

            var systemList = (await _siteService.GetSystemList())
                             .OrderByDescending(_ => _.Id == GetId(ClaimType.SystemId)).ThenBy(_ => _.Name);

            var viewModel = new TriggersListViewModel
            {
                Triggers      = triggerList.Data,
                PaginateModel = paginateModel,
                Search        = search,
                SystemId      = systemId,
                BranchId      = branchId,
                ProgramId     = programId,
                Mine          = mine,
                SystemList    = systemList,
                ProgramList   = await _siteService.GetProgramList()
            };

            if (mine == true)
            {
                viewModel.BranchList = (await _siteService.GetBranches(GetId(ClaimType.SystemId)))
                                       .OrderByDescending(_ => _.Id == GetId(ClaimType.BranchId))
                                       .ThenBy(_ => _.Name);
                viewModel.ActiveNav = "Mine";
            }
            else if (branchId.HasValue)
            {
                var branch = await _siteService.GetBranchByIdAsync(branchId.Value);

                viewModel.BranchName = branch.Name;
                viewModel.SystemName = systemList
                                       .SingleOrDefault(_ => _.Id == branch.SystemId)?.Name;
                viewModel.BranchList = (await _siteService.GetBranches(branch.SystemId))
                                       .OrderByDescending(_ => _.Id == GetId(ClaimType.BranchId))
                                       .ThenBy(_ => _.Name);
                viewModel.ActiveNav = "Branch";
            }
            else if (systemId.HasValue)
            {
                viewModel.SystemName = systemList
                                       .SingleOrDefault(_ => _.Id == systemId.Value)?.Name;
                viewModel.BranchList = (await _siteService.GetBranches(systemId.Value))
                                       .OrderByDescending(_ => _.Id == GetId(ClaimType.BranchId))
                                       .ThenBy(_ => _.Name);
                viewModel.ActiveNav = "System";
            }
            else
            {
                viewModel.BranchList = (await _siteService.GetBranches(GetId(ClaimType.SystemId)))
                                       .OrderByDescending(_ => _.Id == GetId(ClaimType.BranchId))
                                       .ThenBy(_ => _.Name);
                viewModel.ActiveNav = "All";
            }
            if (programId.HasValue)
            {
                if (programId.Value > 0)
                {
                    viewModel.ProgramName =
                        (await _siteService.GetProgramByIdAsync(programId.Value)).Name;
                }
                else
                {
                    viewModel.ProgramName = "Not Limited";
                }
            }

            return(View(viewModel));
        }