public async Task HandleDrinkEventsAsync(Activity activity, User currentUser)
        {
            if (activity.Drink.DrinkType == DrinkType.Anti)
            {
                return;
            }
            var drinkEvent = await drinkEventRepository.FindCurrentDrinkEventAsync();

            if (drinkEvent == null)
            {
                return;
            }

            if (!drinkEvent.AddScoringUserId(currentUser.Id))
            {
                return;
            }

            await drinkEventRepository.UpdateDrinkEventAsync(drinkEvent);

            await rankingService.IncreaseScoreAsync(currentUser.Id, Constants.Scores.StandardDrinkAction);

            string message = await translationService.GetTranslationAsync(currentUser.Language, "DrinkEventActivityWinMessage", Constants.Scores.StandardDrinkAction);

            var drinkEventNotificationActivity = Activity.CreateNotificationActivity(currentUser.Id, currentUser.Name, message);
            await activityRepository.AddActivityAsync(drinkEventNotificationActivity.ToEntity());

            var notifications = new[] { new DrinkEventCongratulationNotification(currentUser.Id) };
            await pushNotificationService.NotifyAsync(notifications);
        }
Example #2
0
        public async Task RunAsync(ActivityAddedMessage message, IDurableOrchestrationClient durableClient)
        {
            var activity = await activityRepository.GetActivityAsync(message.ActivityId);

            logger.LogInformation($"Handling added activity [{activity}] ...");

            var currentUser = await userRepository.GetUserAsync(activity.UserId);

            try
            {
                // Notifying user makes only sense when he provided push infos
                if (activity.ActivityType == ActivityType.Drink && currentUser.PushInfo != null)
                {
                    await HandleMonitoringAsync(durableClient, currentUser.Id);
                }

                bool           shouldUpdate = false;
                UserStatistics userStats    = null;
                try
                {
                    // Immediately update Stats for current user
                    userStats = await statisticUpdateService.UpdateStatisticsAsync(currentUser.Id, currentUser.Gender, currentUser.Weight);

                    if (activity.ActivityType == ActivityType.Drink)
                    {
                        await statisticUpdateService.UpdateRankingAsync(currentUser.Id);

                        activity.UpdateStats(userStats.CurrentNightDrinks, userStats.CurrentAlcoholization);

                        shouldUpdate = true;
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"Failed to update stats for user [{currentUser}]");
                }

                if (activity.Location != null && activity.Location.IsValid())
                {
                    await HandleLocationUpdateAsync(activity);

                    shouldUpdate = true;
                }

                var entity = activity.ToEntity();

                if (shouldUpdate)
                {
                    await activityRepository.UpdateActivityAsync(entity);
                }

                await this.activityDistributionService.DistributeActivitiesAsync(currentUser, entity);

                await SendActivityUpdateAsync(currentUser, activity, userStats);

                // check for drink events
                try
                {
                    if (activity.ActivityType == ActivityType.Drink)
                    {
                        await this.drinkEventHandlingService.HandleDrinkEventsAsync(activity, currentUser);
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Error handling drink events!");
                }

                // send out push notifications
                var notifications = BuildNotifications(currentUser, activity, userStats);
                if (notifications.Any())
                {
                    await pushNotificationService.NotifyAsync(notifications);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Processing failed unexpectedly!");
            }
        }