示例#1
0
        public async Task <IActionResult> Analytics(string challengeId)
        {
            var aggregatesReponse = await aggregateProvider.GetItemAsync(challengeId);

            var model = new AnalyticsChallengeViewModel()
            {
                ChallengeId = challengeId
            };

            if (aggregatesReponse.Item1.Success)
            {
                var agg =
                    aggregatesReponse.Item2 ??
                    new ACMA.Aggregate()
                {
                    Id             = challengeId,
                    ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                    {
                        Finished = 0, Started = 0, ChallengeProgress = new List <string>()
                    }
                };

                if (aggregatesReponse.Item2 == null)
                {
                    await aggregateProvider.AddItemAsync(agg);
                }
                else
                {
                    model.Finished          = aggregatesReponse.Item2.ChallengeUsers.Finished;
                    model.Started           = aggregatesReponse.Item2.ChallengeUsers.Started;
                    model.ChallengeProgress = aggregatesReponse.Item2.ChallengeUsers.ChallengeProgress;
                }
            }
            else
            {
                // No aggregate defined
                var agg = new ACMA.Aggregate
                {
                    Id             = challengeId,
                    ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                    {
                        Finished = 0, Started = 0, ChallengeProgress = new List <string>()
                    }
                };


                await aggregateProvider.AddItemAsync(agg);
            }

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> UpdateChallenge(EditChallengeViewModel inputModel)
        {
            // We only care about updating the challenge values
            var challenge = new ACMT.ChallengeDetails
            {
                Description = inputModel.Description,
                Id          = inputModel.Id,
                Name        = inputModel.Name,
                IsPublic    = inputModel.IsPublic,
                Questions   = inputModel.ChallengeQuestions == null ? new List <ACMQ.QuestionLite>() :
                              inputModel.ChallengeQuestions?
                              .Select(p => new ACMQ.QuestionLite()
                {
                    AssociatedQuestionId = p.AssociatedQuestionId,
                    Description          = p.Description,
                    Difficulty           = p.Difficulty,
                    Id             = p.Id,
                    Index          = p.Index,
                    Name           = p.Name,
                    NextQuestionId = p.NextQuestionId
                }).ToList(),
                AzureServiceCategory = inputModel.AzureServiceCategory,
                WelcomeMessage       = inputModel.WelcomeMessage,
                Duration             = inputModel.Duration,
                PrereqLinks          = inputModel.PrereqLinks
            };

            var updateResult = await challengeProvider.AddItemAsync(challenge);

            // Check the IsPublic property. Depending on the change, we need to add or remove the challenge from the aggregate
            if (inputModel.IsPublic != inputModel.OldIsPublic)
            {
                // We only have one, so just get via the Partition search
                var aggregatesReponse = await aggregateProvider.GetItemAsync("00000000-0000-0000-0000-000000000000");

                if (aggregatesReponse.Item1.Success)
                {
                    var agg =
                        aggregatesReponse.Item2 ??
                        new ACMA.Aggregate()
                    {
                        Id = "00000000-0000-0000-0000-000000000000",
                        ChallengeTotals = new ACMA.ChallengeAggregateTotals()
                        {
                            TotalPublic = 0
                        }
                    };

                    agg.ChallengeTotals.TotalPublic += inputModel.IsPublic ? 1 : agg.ChallengeTotals.TotalPublic > 0 ? -1 : 0;
                    await aggregateProvider.AddItemAsync(agg);
                }
                else
                {
                    // Doesn't exists
                    var agg = new ACMA.Aggregate()
                    {
                        Id = "00000000-0000-0000-0000-000000000000",
                        ChallengeTotals = new ACMA.ChallengeAggregateTotals()
                        {
                            TotalPublic = 0
                        }
                    };

                    agg.ChallengeTotals.TotalPublic += inputModel.IsPublic ? 1 : agg.ChallengeTotals.TotalPublic > 0 ? -1 : 0;
                    await aggregateProvider.AddItemAsync(agg);
                }
            }

            if (updateResult.Success)
            {
                return(Ok());
            }
            else
            {
                return(StatusCode(500));
            }
        }
        public async Task <IActionResult> StartChallenge(string challengeId, string questionId)
        {
            var user = await _userManager.GetUserAsync(User);

            var userChallengeResponse = await userChallengesProvider.GetItemAsync(user.Id);

            var challengeResponse = await challengesProvider.GetItemAsync(challengeId);

            var aggregateReponse = await aggregateProvider.GetItemAsync(challengeId);

            if (!userChallengeResponse.Item1.IsError)
            {
                // If the challenge is not locked, lock it since the first user has started it
                if (!challengeResponse.Item2.IsLocked)
                {
                    challengeResponse.Item2.IsLocked = true;
                    await challengesProvider.AddItemAsync(challengeResponse.Item2);
                }

                // If this is a new challenge, we first need to show the introduction
                var showIntroduction = false;

                // New tournament
                if (!userChallengeResponse.Item1.Success)
                {
                    var userChallenge = new UserChallenges {
                        Id = user.Id, Challenges = new List <UserChallengeItem>()
                    };
                    userChallenge.Challenges
                    .Add(new UserChallengeItem()
                    {
                        AccumulatedXP   = 0,
                        ChallengeId     = challengeId,
                        Completed       = false,
                        CurrentQuestion = questionId,
                        StartTimeUTC    = DateTime.Now.ToUniversalTime(),
                        CurrentIndex    = 0,
                        NumOfQuestions  = challengeResponse.Item2.Questions.Count()
                    });

                    await userChallengesProvider.AddItemAsync(userChallenge);

                    if (aggregateReponse.Item1.Success)
                    {
                        var agg =
                            aggregateReponse.Item2 ??
                            new ACMA.Aggregate()
                        {
                            Id             = challengeId,
                            ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                            {
                                Finished = 0, Started = 0
                            }
                        };

                        agg.ChallengeUsers.Started += 1;
                        await aggregateProvider.AddItemAsync(agg);
                    }
                    else
                    {
                        // Doesn't exist
                        var agg = new ACMA.Aggregate()
                        {
                            Id             = challengeId,
                            ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                            {
                                Finished = 0, Started = 0
                            }
                        };

                        agg.ChallengeUsers.Started += 1;
                        await aggregateProvider.AddItemAsync(agg);
                    }

                    showIntroduction = true;
                }
                else
                {
                    // The user already has done some challenges
                    // If this is new, then add the userChallenge and update the Aggregate
                    if (!userChallengeResponse.Item2.Challenges.Any(p => p.ChallengeId == challengeId))
                    {
                        userChallengeResponse.Item2.Challenges
                        .Add(new UserChallengeItem()
                        {
                            AccumulatedXP   = 0,
                            ChallengeId     = challengeId,
                            Completed       = false,
                            CurrentQuestion = questionId,
                            StartTimeUTC    = DateTime.Now.ToUniversalTime(),
                            CurrentIndex    = 0,
                            NumOfQuestions  = challengeResponse.Item2.Questions.Count()
                        });
                        await userChallengesProvider.AddItemAsync(userChallengeResponse.Item2);

                        if (aggregateReponse.Item1.Success)
                        {
                            var agg =
                                aggregateReponse.Item2 ??
                                new ACMA.Aggregate()
                            {
                                Id             = challengeId,
                                ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                                {
                                    Finished = 0, Started = 0
                                }
                            };

                            agg.ChallengeUsers.Started += 1;
                            await aggregateProvider.AddItemAsync(agg);
                        }
                        else
                        {
                            // Doesn't exist
                            var agg = new ACMA.Aggregate()
                            {
                                Id             = challengeId,
                                ChallengeUsers = new ACMA.ChallengeAggregateUsers()
                                {
                                    Finished = 0, Started = 0
                                }
                            };

                            agg.ChallengeUsers.Started += 1;
                            await aggregateProvider.AddItemAsync(agg);
                        }

                        showIntroduction = true;
                    }
                    else if (userChallengeResponse.Item2.Challenges.Where(p => p.ChallengeId == challengeId).FirstOrDefault()?.CurrentIndex == 0)
                    {
                        // If the user already registered for the challenge but never started it
                        showIntroduction = true;
                    }
                }

                if (showIntroduction)
                {
                    return(RedirectToAction("Introduction", new { challengeId = challengeId, questionId = questionId }));
                }
                else
                {
                    return(RedirectToAction("ShowQuestion", new { challengeId = challengeId, questionId = questionId }));
                }
            }


            return(StatusCode(500));
        }