Exemple #1
0
        public ActionResult Challenge(ChallengeEvent model)
        {
            List <UserChallenges> userChallengesList = new List <UserChallenges>();
            List <Notifications>  notificationsList  = new List <Notifications>();

            userChallengesList = _challengeService.QueryableCustom().Where(w => w.EventId == model.EventId && w.IsActive).ToList();
            if (model.SelectedIds.Count() > 0)
            {
                foreach (var item in userChallengesList)
                {
                    item.IsActive    = false;
                    item.ObjectState = ObjectState.Modified;
                    _challengeService.InsertOrUpdateGraph(item);
                    _unitOfWork.SaveChanges();
                }
                //eventid
                for (int i = 0; i < model.SelectedIds.Length; i++)
                {
                    UserChallenges userChallenge = new UserChallenges();
                    userChallenge.EventId       = model.EventId;
                    userChallenge.IsActive      = true;
                    userChallenge.IsAccepted    = false;
                    userChallenge.UserId        = Common.CurrentUser.Id;
                    userChallenge.DateCreated   = DateTime.Now;
                    userChallenge.ToChallengeId = model.SelectedIds[i];
                    //userChallengesList.Add();
                    _challengeService.Insert(userChallenge);
                }

                saveResult = _unitOfWork.SaveChanges();
                for (int i = 0; i < model.SelectedIds.Length; i++)
                {
                    Notifications notification = new Notifications();
                    notification.ObjectState      = ObjectState.Added;
                    notification.Notification     = Common.CurrentUser.Name + " Challenged you for the event " + model.EventName;
                    notification.Link             = "/Events/Detail/" + model.EventId;
                    notification.IsRead           = false;
                    notification.Icon             = "fa fa-plus-square fa-lg";
                    notification.UserId           = model.SelectedIds[i];
                    notification.NotificationDate = DateTime.Now;
                    notification.ProfilePic       = Common.CurrentUser.ProfilePic == null ? "/assets/images/avatar-1.png" : Common.CurrentUser.ProfilePic;
                    notificationsList.Add(notification);
                }
                NotificationHub.SendNotification(model.SelectedIds.ToList(), " You are challenged for event " + model.EventName, "fa fa-plus-square fa-lg", "/Events/Info/" + model.EventId, Common.CurrentUser.ProfilePic == null ? "/assets/images/avatar-1.png" : Common.CurrentUser.ProfilePic);
                _notificationsService.InsertGraphRange(notificationsList);
                _unitOfWork.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
        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));
        }