Ejemplo n.º 1
0
        public void CountParrot(string userId, string channelId, DateTime currentTime)
        {
            var coffeeBreak = _coffeeRepository.GetCoffeeBreak();

            var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones().Any(x => x.Id == "Eastern Standard Time") ?
                               TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time") :
                               TimeZoneInfo.FindSystemTimeZoneById("America/New_York");

            var timezoneOffset = timeZoneInfo.GetUtcOffset(currentTime);
            var morningBreak   = TimeSpan.FromHours(MorningBreak) - timezoneOffset;
            var afternoonBreak = TimeSpan.FromHours(AfternoonBreak) - timezoneOffset;

            var morningBreakIsPossible   = IsInTimespan(currentTime, morningBreak, Tolerance);
            var afternoonBreakIsPossible = IsInTimespan(currentTime, afternoonBreak, Tolerance);
            var isTooCloseFromLastBreak  = currentTime < coffeeBreak.LastCoffee + TimeSpan.FromHours(1);

            if (!morningBreakIsPossible && !afternoonBreakIsPossible)
            {
                return;
            }

            if (isTooCloseFromLastBreak || coffeeBreak.IsCoffeeBreak)
            {
                return;
            }

            _coffeeRepository.ConfirmUserReady(userId);

            if (_coffeeRepository.GetMissingParticipants().Count == 0)
            {
                LaunchCoffeeBreak(channelId);
                return;
            }

            var remindJob = _coffeeRepository.GetRemindJob();

            if (!string.IsNullOrEmpty(remindJob))
            {
                _backgroundJobClient.Delete(remindJob);
            }

            var newRemindJob = _backgroundJobClient.Schedule(() => CoffeeRemind(channelId), TimeSpan.FromSeconds(CoffeeRemindDelaySeconds));

            _coffeeRepository.SetRemindJob(newRemindJob);
        }
Ejemplo n.º 2
0
        public void CountParrot(string userId, string channelId, DateTime currentTime)
        {
            var coffeeBreak = _coffeeRepository.GetCoffeeBreak();

            var morningBreakIsPossible = IsInTimespan(currentTime, TimeSpan.FromHours(MorningBreak), Tolerance) &&
                                         !coffeeBreak.MorningBreakCompleted;

            var afternoonBreakIsPossible = IsInTimespan(currentTime, TimeSpan.FromHours(AfternoonBreak), Tolerance) &&
                                           !coffeeBreak.AfternoonBreakCompleted;

            if (!morningBreakIsPossible && !afternoonBreakIsPossible)
            {
                return;
            }

            if (coffeeBreak.IsCoffeeBreak)
            {
                return;
            }

            _coffeeRepository.ConfirmUserReady(userId);

            if (_coffeeRepository.GetMissingParticipants().Count == 0)
            {
                LaunchCoffeeBreak(channelId);
                return;
            }

            var remindJob = _coffeeRepository.GetRemindJob();

            if (!string.IsNullOrEmpty(remindJob))
            {
                _backgroundJobClient.Delete(remindJob);
            }

            var newRemindJob = _backgroundJobClient.Schedule(() => CoffeeRemind(channelId), TimeSpan.FromSeconds(CoffeeRemindDelaySeconds));

            _coffeeRepository.SetRemindJob(newRemindJob);
        }