Beispiel #1
0
        private Duration measureDifference()
        {
            Instant now       = clock.GetCurrentInstant();
            Instant nowServer = calculateServerInstant();

            return(nowServer.Minus(now));
        }
Beispiel #2
0
        public static Duration GetPoTime(
            int offsetMinutes,
            ArenaType arenaType,
            Instant?utcNow = null)
        {
            Instant  other       = utcNow.HasValue ? utcNow.Value : SystemClock.Instance.GetCurrentInstant();
            DateTime dateTimeUtc = other.ToDateTimeUtc();
            int      hourOfDay   = arenaType == ArenaType.Squad ? 18 : 19;
            Instant  instant     = Instant.FromUtc(dateTimeUtc.Year, dateTimeUtc.Month, dateTimeUtc.Day, hourOfDay, 0, 0).Minus(Duration.FromMinutes((long)offsetMinutes));

            return(instant >= other?instant.Minus(other) : instant.Plus(Duration.FromHours(24)).Minus(other));
        }
Beispiel #3
0
        public async Task <bool> IsUserBlockedAsync(AppUser user)
        {
            Duration duration    = Duration.FromMinutes(10);
            Instant  currentTime = SystemClock.Instance.GetCurrentInstant();

            int attemptCount = await _authDbContext.InvalidLoginAttempts
                               .AsNoTracking()
                               .Where(l => l.TargetUser == user)
                               .Where(l => currentTime.Minus(duration) <= l.AttemptTime)
                               .CountAsync();

            return(attemptCount >= 10);
        }
Beispiel #4
0
        // FIXME: This currently does not consider IPv6. Current logic allows 10 login attempts in the last 10 minutes.
        public async Task <bool> IsIpBlockedAsync(IPAddress ipAddress)
        {
            Duration duration    = Duration.FromMinutes(10);
            Instant  currentTime = SystemClock.Instance.GetCurrentInstant();

            int attemptCount = await _authDbContext.InvalidLoginAttempts
                               .AsNoTracking()
                               .Where(l => l.IpAddress == ipAddress)
                               .Where(l => currentTime.Minus(duration) <= l.AttemptTime)
                               .CountAsync();

            return(attemptCount >= 10);
        }
Beispiel #5
0
        private async Task WaitTill(Instant instant)
        {
            while (_stoppingTask == null)
            {
                UpdateLiveliness();

                var remaining = (int)instant.Minus(InstantUtils.Now).TotalMilliseconds;
                if (remaining <= 0)
                {
                    return;
                }

                await Task.Delay(Math.Min(1000, remaining));
            }

            _context.Logger.Debug("Suspending wait because of stop request");
        }
Beispiel #6
0
        public void Can_Query_By_MoreThan_Instant_Stored_As_Int64()
        {
            Instant startInstant  = TestClock.Now.Plus(Duration.FromHours(24));
            Instant finishInstant = startInstant.Plus(Duration.FromHours(1));
            var     testEvent     = new InstantTestEntity
            {
                Description   = " Can_Query_By_MoreThan_Instant_Stored_As_Int64",
                StartInstant  = startInstant,
                FinishInstant = finishInstant
            };

            using (ISession session = SessionFactory.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(testEvent);
                    transaction.Commit();
                }

            Instant beforeInstant = startInstant.Minus(Duration.FromSeconds(1));

            using (ISession session = SessionFactory.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var query = session.Query <InstantTestEntity>()
                                .Where(x => x.Id == testEvent.Id && x.StartInstant > beforeInstant);
                    var retrievedEvent = query.SingleOrDefault();
                    transaction.Commit();
                    Assert.That(testEvent, Is.Not.Null);
                    Assert.That(testEvent, Is.EqualTo(retrievedEvent));
                }

            using (ISession session = SessionFactory.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var query = session.Query <InstantTestEntity>()
                                .Where(x => x.Id == testEvent.Id && x.StartInstant > beforeInstant && x.FinishInstant >= finishInstant);
                    var retrievedEvent = query.SingleOrDefault();
                    transaction.Commit();
                    Assert.That(testEvent, Is.Not.Null);
                    Assert.That(testEvent, Is.EqualTo(retrievedEvent));
                }
        }
Beispiel #7
0
        public async Task <Embed> GetMaintenance()
        {
            List <NewsItem> items = await NewsAPI.Latest(Categories.Maintenance);

            Instant  now       = TimeUtils.Now;
            NewsItem?nextMaint = null;
            Instant? bestStart = null;

            foreach (NewsItem item in items)
            {
                Instant?start = item.GetStart();
                Instant?end   = item.GetEnd();

                if (start == null || end == null)
                {
                    continue;
                }

                if (!item.Title.Contains("All Worlds"))
                {
                    continue;
                }

                if (start < bestStart)
                {
                    continue;
                }

                if (start < now.Minus(Duration.FromDays(14)))
                {
                    continue;
                }

                bestStart = start;
                nextMaint = item;
            }

            if (nextMaint != null)
            {
                EmbedBuilder builder = new EmbedBuilder();
                builder.ThumbnailUrl = "https://img.finalfantasyxiv.com/lds/h/F/DlQYVw2bqzA5ZOCfXKZ-Qe1IZU.svg";
                builder.Title        = nextMaint.Title;

                Instant?start = nextMaint.GetStart();
                Instant?end   = nextMaint.GetEnd();

                if (start == null || end == null)
                {
                    throw new Exception();
                }

                Duration timeUntilStart = (Duration)(start - now);
                Duration timeUntilEnd   = (Duration)(end - now);

                if (timeUntilStart.TotalMinutes > 0)
                {
                    builder.Description = "Starts In: " + TimeUtils.GetDurationString(start - now);
                }
                else if (timeUntilEnd.TotalMinutes > 0)
                {
                    builder.Description = "Ends In: " + TimeUtils.GetDurationString(end - now);
                }
                else
                {
                    builder.Description = "Completed: " + TimeUtils.GetDurationString(now - end) + " ago.";
                }

                builder.AddField("Starts", TimeUtils.GetDateTimeString(start));
                builder.AddField("Ends", TimeUtils.GetDateTimeString(end));
                builder.AddField("Duration", TimeUtils.GetDurationString(end - start));
                return(builder.Build());
            }

            throw new UserException("I couldn't find any maintenance.");
        }
Beispiel #8
0
 internal static Instant MinusFromDateTimeOffset(this Instant instance, Duration duration) => instance.Minus(duration);
Beispiel #9
0
        public async Task GetMaintenance(CommandMessage message)
        {
            List <NewsItem> items = await NewsAPI.Latest(Categories.Maintenance);

            Instant  now       = TimeUtils.Now;
            NewsItem?nextMaint = null;
            Instant? bestStart = null;

            foreach (NewsItem item in items)
            {
                Instant?start = item.GetStart();
                Instant?end   = item.GetEnd();

                if (start == null || end == null)
                {
                    continue;
                }

                if (!item.Title.Contains("All Worlds"))
                {
                    continue;
                }

                if (start < bestStart)
                {
                    continue;
                }

                if (start < now.Minus(Duration.FromDays(14)))
                {
                    continue;
                }

                bestStart = start;
                nextMaint = item;
            }

            if (nextMaint != null)
            {
                EmbedBuilder builder = new EmbedBuilder
                {
                    ThumbnailUrl = "http://na.lodestonenews.com/images/maintenance.png",
                    ////ThumbnailUrl = "https://img.finalfantasyxiv.com/lds/h/F/DlQYVw2bqzA5ZOCfXKZ-Qe1IZU.svg",
                    Title = nextMaint.Title,
                    Url   = nextMaint.Url,
                };

                Instant?start = nextMaint.GetStart();
                Instant?end   = nextMaint.GetEnd();

                if (start == null || end == null)
                {
                    throw new Exception();
                }

                Duration timeUntilStart = (Duration)(start - now);
                Duration timeUntilEnd   = (Duration)(end - now);

                if (timeUntilStart.TotalMinutes > 0)
                {
                    builder.Description = "Starts In: " + TimeUtils.GetDurationString(start - now);
                }
                else if (timeUntilEnd.TotalMinutes > 0)
                {
                    builder.Description = "Ends In: " + TimeUtils.GetDurationString(end - now);
                }
                else
                {
                    builder.Description = "Completed: " + TimeUtils.GetDurationString(now - end) + " ago.";
                }

                ////builder.AddField("Starts", TimeUtils.GetDateTimeString(start));
                ////builder.AddField("Ends", TimeUtils.GetDateTimeString(end));
                builder.AddField("Starts", await TimeUtils.GetTimeList(message.Guild.Id, start));
                builder.AddField("Ends", await TimeUtils.GetTimeList(message.Guild.Id, end));
                builder.AddField("Duration", TimeUtils.GetDurationString(end - start) ?? "Unknown");

                await message.Channel.SendMessageAsync(embed : builder.Build(), messageReference : message.MessageReference);

                return;
            }

            throw new UserException("I couldn't find any maintenance.");
        }