Example #1
0
        private async Task AwardTriggersAsync(int userId)
        {
            // load the initial list of triggers that might have been achieved
            var triggers = await _triggerRepository.GetTriggersAsync(userId);

            do
            {
                if (_queuedTriggerIds == null || _queuedTriggerIds.Count() == 0)
                {
                    // this is our first check, we're not nested
                    _queuedTriggerIds = triggers.Select(_ => _.Id).ToList();
                }
                else
                {
                    // we've already checked triggers so don't double-award any that are queued
                    triggers = triggers
                               .Where(_ => !_queuedTriggerIds.Contains(_.Id))
                               .ToList();

                    // update the queue with everything we're working on
                    _queuedTriggerIds = _queuedTriggerIds
                                        .Union(triggers.Select(_ => _.Id))
                                        .ToList();
                }

                // if any triggers came back let's check them
                while (triggers.Count() > 0)
                {
                    // pull the first trigger off the list and remove it from the list
                    var trigger = triggers.First();
                    triggers.Remove(trigger);

                    // add that we've processed this trigger for this user
                    await _triggerRepository.AddTriggerActivationAsync(userId, trigger.Id);

                    // if there are points to be awarded, do that now
                    if (trigger.AwardPoints > 0)
                    {
                        // this call will recursively call this method in case any additional
                        // point-based triggers are fired by this action
                        await AddPointsSaveAsync(GetClaimId(ClaimType.UserId),
                                                 GetActiveUserId(),
                                                 userId,
                                                 trigger.AwardPoints);
                    }

                    // every trigger awards a badge
                    var badge = await AwardBadgeAsync(userId, trigger.AwardBadgeId);

                    // log the notification
                    await _notificationRepository.AddSaveAsync(userId, new Notification
                    {
                        PointsEarned  = trigger.AwardPoints,
                        UserId        = userId,
                        Text          = trigger.AwardMessage,
                        BadgeId       = trigger.AwardBadgeId,
                        BadgeFilename = badge.Filename
                    });

                    // add the award to the user's history
                    await _userLogRepository.AddSaveAsync(userId, new UserLog
                    {
                        UserId       = userId,
                        PointsEarned = trigger.AwardPoints,
                        IsDeleted    = false,
                        BadgeId      = trigger.AwardBadgeId,
                        Description  = trigger.AwardMessage
                    });

                    // award any vendor code that is necessary
                    await AwardVendorCodeAsync(userId, trigger.AwardVendorCodeTypeId);

                    // remove this item from the queued list of triggers
                    _queuedTriggerIds.Remove(trigger.Id);
                }

                // reload the list in case a trigger triggered another trigger :rage4:
                triggers = await _triggerRepository.GetTriggersAsync(userId);
            } while (triggers.Count() > 0);
        }
Example #2
0
        private async Task AwardTriggersAsync(int userId, bool logPoints = true, int?siteId = null,
                                              bool userIdIsCurrentUser   = false)
        {
            // load the initial list of triggers that might have been achieved
            var triggers = await _triggerRepository.GetTriggersAsync(userId);

            // if three are no triggers in the current query or in the queue then we are done
            if (triggers == null || triggers.Count() == 0)
            {
                return;
            }

            // if any triggers came back let's check them
            while (triggers.Count() > 0)
            {
                // pull the first trigger off the list and remove it from the list
                var trigger = triggers.First();
                triggers.Remove(trigger);

                // add that we've processed this trigger for this user
                await _triggerRepository.AddTriggerActivationAsync(userId, trigger.Id);

                // if there are points to be awarded, do that now
                if (trigger.AwardPoints > 0 && logPoints)
                {
                    await AddPointsSaveAsync(GetClaimId(ClaimType.UserId),
                                             GetActiveUserId(),
                                             userId,
                                             trigger.AwardPoints,
                                             checkTriggers : false);
                }

                // every trigger awards a badge
                var badge = await AwardBadgeAsync(userId, trigger.AwardBadgeId);

                // log the notification
                await _notificationRepository.AddSaveAsync(userId, new Notification
                {
                    PointsEarned  = trigger.AwardPoints,
                    UserId        = userId,
                    Text          = trigger.AwardMessage,
                    BadgeId       = trigger.AwardBadgeId,
                    BadgeFilename = badge.Filename
                });

                // add the award to the user's history
                await _userLogRepository.AddSaveAsync(userId, new UserLog
                {
                    UserId       = userId,
                    PointsEarned = trigger.AwardPoints,
                    IsDeleted    = false,
                    BadgeId      = trigger.AwardBadgeId,
                    Description  = trigger.AwardMessage
                });

                // award any vendor code that is necessary
                await AwardVendorCodeAsync(userId, trigger.AwardVendorCodeTypeId, siteId);

                // send mail if applicable
                int?mailId = await SendMailAsync(userId, trigger, siteId);

                // award prize if applicable
                await AwardPrizeAsync(userId, trigger, mailId, userIdIsCurrentUser);
            }
            // this call will recursively call this method in case any additional
            // triggers are fired by this action
            await AwardTriggersAsync(userId, logPoints);
        }