Exemple #1
0
        private async Task ScheduleSpecialMessages(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var partsOfDay = new List <PartOfDay>();

            var now       = DateTime.Now;
            var partOfDay = now.PartOfDay();

            partsOfDay.Add(partOfDay);
            partsOfDay.AddRange(partOfDay.Rest);

            var users = await _userStorage.GetAllUsersForScheduledMessagesNoTracking();

            var scheduledCount = 0;

            foreach (var user in users)
            {
                foreach (var part in partsOfDay)
                {
                    var messageTypesForPart = Const.PartOfDay.SpecialMessageTypes[part];
                    foreach (var messageType in messageTypesForPart)
                    {
                        var start = now.Date.AddSeconds(messageType.Range.Start.TotalSeconds);

                        if (now > start)
                        {
                            start = now;
                        }

                        var end = now.Date.AddSeconds(messageType.Range.End.TotalSeconds);

                        if (now > end)
                        {
                            continue;
                        }

                        var schedulingTime = _dateTimeGenerator.GenerateRandomDateTimeWithinRange(new DateTimeRange(start, end));
                        var message        = new ScheduledMessage
                        {
                            ChatId   = user.ChatId,
                            Rarity   = _rarityRoller.RollRarityForUser(user),
                            Type     = Enum.Parse <MessageType>(messageType.Type.ToUpperInvariant()),
                            Time     = schedulingTime,
                            Category = Enum.Parse <MessageCategory>(schedulingTime.PartOfDay().Name.ToUpperInvariant())
                        };

                        await _messageSchedule.AddScheduledMessage(message, cancellationToken);

                        scheduledCount++;
                    }
                }
            }

            _logger.LogInformation($"{scheduledCount} special messages were scheduled.");
        }