Exemplo n.º 1
0
 public static void AddJobRange(this Ban ban, IEnumerable <string> jobs)
 {
     foreach (var job in jobs)
     {
         ban.AddJob(job);
     }
 }
Exemplo n.º 2
0
        public async Task <IEnumerable <Ban> > GetBansAsync(int page = 1)
        {
            var request  = new RestRequest($"bans/{page}", Method.GET, DataFormat.Json).AddQueryParameter("limit", RecordsPerPage.ToString());
            var response = await Client.ExecuteAsync(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                FailedRequest(response);
            }

            var toReturn  = new List <Ban>();
            var dirtyBans = new List <Ban>();
            var content   = JsonSerializer.Deserialize <Dictionary <string, JsonElement> >(response.Content);

            foreach (var bh in content["bans"].EnumerateObject())
            {
                var ban = bh.Value;

                // Ban expiration could be based on the expiration time field or the existance of the unbanned datetime
                // field, so we have to check both.
                var expiration = ban.GetProperty("unbanned_datetime").GetString() == null ? (DateTime?)null
                        : DateTime.Parse(ban.GetProperty("unbanned_datetime").GetString());
                if (!expiration.HasValue)
                {
                    expiration = ban.GetProperty("expiration_time").GetString() == null ? (DateTime?)null
                        : DateTime.Parse(ban.GetProperty("expiration_time").GetString());
                }

                // Get ban
                var toAdd = new Ban()
                {
                    BanID            = bh.Name,
                    BannedBy         = ban.GetProperty("admin").GetString(),
                    BannedOn         = DateTime.Parse(ban.GetProperty("bantime").ToString()),
                    CKey             = ban.GetProperty("ckey").GetString(),
                    Expires          = expiration,
                    Reason           = ban.GetProperty("reason").ToString(),
                    BanType          = ban.GetProperty("role").GetString().ToLower() == "server" ? BanType.Server : BanType.Job,
                    SourceNavigation = BanSource
                };

                // Specify UTC
                toAdd.BannedOn = DateTime.SpecifyKind(toAdd.BannedOn, DateTimeKind.Utc);
                if (toAdd.Expires.HasValue)
                {
                    toAdd.Expires = DateTime.SpecifyKind(toAdd.Expires.Value, DateTimeKind.Utc);
                }

                // Add jobban if relevant
                if (toAdd.BanType == BanType.Job)
                {
                    toAdd.AddJob(ban.GetProperty("role").GetString());
                    dirtyBans.Add(toAdd);
                }
                else
                {
                    toReturn.Add(toAdd);
                }
            }

            // Group jobbans
            foreach (var group in dirtyBans.GroupBy(x => new { x.CKey, x.BannedOn }))
            {
                var firstBan = group.OrderBy(x => x.BanID).First();
                firstBan.AddJobRange(group.SelectMany(x => x.JobBans).Select(x => x.Job));
                toReturn.Add(firstBan);
            }

            return(toReturn);
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <Ban> > GetBansAsync(int page = 1)
        {
            var request  = new RestRequest($"bans/{page}", Method.GET, DataFormat.Json).AddQueryParameter("limit", RECORDS_PER_PAGE.ToString());
            var response = await _client.ExecuteAsync(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                _logger.LogError($"TGMC website returned a non-200 HTTP response code: {response.StatusCode}, aborting parse.");
                throw new BanSourceUnavailableException($"TGMC website returned a non-200 HTTP response code: {response.StatusCode}, aborting parse.");
            }

            var toReturn  = new List <Ban>();
            var dirtyBans = new List <Ban>();
            var content   = JsonSerializer.Deserialize <Dictionary <string, JsonElement> >(response.Content);

            foreach (var bh in content["bans"].EnumerateObject())
            {
                var ban = bh.Value;

                // Get ban
                var toAdd = new Ban()
                {
                    BanID    = bh.Name,
                    BannedBy = ban.GetProperty("admin").GetString(),
                    BannedOn = DateTime.Parse(ban.GetProperty("bantime").ToString()),
                    CKey     = ban.GetProperty("ckey").GetString(),
                    Expires  = ban.GetProperty("expiration_time").GetString() == null ? (DateTime?)null
                        : DateTime.Parse(ban.GetProperty("expiration_time").GetString()),
                    Reason           = ban.GetProperty("reason").ToString(),
                    BanType          = ban.GetProperty("role").GetString().ToLower() == "server" ? BanType.Server : BanType.Job,
                    SourceNavigation = _banSource
                };

                // Specify UTC
                toAdd.BannedOn = DateTime.SpecifyKind(toAdd.BannedOn, DateTimeKind.Utc);
                if (toAdd.Expires.HasValue)
                {
                    toAdd.Expires = DateTime.SpecifyKind(toAdd.Expires.Value, DateTimeKind.Utc);
                }

                // Add jobban if relevant
                if (toAdd.BanType == BanType.Job)
                {
                    toAdd.AddJob(ban.GetProperty("role").GetString());
                    dirtyBans.Add(toAdd);
                }
                else
                {
                    toReturn.Add(toAdd);
                }
            }

            // Group jobbans
            foreach (var group in dirtyBans.GroupBy(x => new { x.BannedOn, x.CKey, x.Reason }))
            {
                var firstBan = group.OrderBy(x => x.BanID).First();
                firstBan.AddJobRange(group.SelectMany(x => x.JobBans).Select(x => x.Job));
                toReturn.Add(firstBan);
            }

            return(toReturn);
        }