Skip to content

Commit

Permalink
Merge pull request #13 from bobbahbrown/v1.0.6
Browse files Browse the repository at this point in the history
Version 1.0.6
  • Loading branch information
bobbah authored Aug 8, 2020
2 parents c1b88dc + b9d53ef commit a7cc043
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 182 deletions.
4 changes: 2 additions & 2 deletions CentCom.API/CentCom.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.5</Version>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<Version>1.0.6</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
3 changes: 2 additions & 1 deletion CentCom.Common/CentCom.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
105 changes: 5 additions & 100 deletions CentCom.Common/Models/Ban.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using CentCom.Common.Models.Equality;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;

namespace CentCom.Common.Models
{
public class Ban :IEquatable<Ban>
public class Ban
{
private static Regex _keyReplacePattern = new Regex(@"[^a-z0-9]");

public int Id { get; set; }
public int Source { get; set; }
[JsonIgnore]
Expand All @@ -28,101 +25,9 @@ public class Ban :IEquatable<Ban>
public string BanID { get; set; }
public HashSet<JobBan> JobBans { get; set; }

/// <summary>
/// Determines if two bans are equal, using their values
/// </summary>
/// <param name="other">The ban to compare against</param>
/// <returns>If the two bans are equal by their values</returns>
/// <remarks>
/// Note that these equals are really just used for determining if
/// the contents of two bans are the same, at which point they're equal.
/// </remarks>
public bool Equals(Ban other)
{
if (other == null) return false;
else if (BanID != null || other.BanID != null)
{
return Source == other.Source
&& BanID == other.BanID;
}
else
{
return Source == other.Source
&& BannedOn == other.BannedOn
&& BanType == other.BanType
&& CKey == other.CKey
&& BannedBy == other.BannedBy
&& Reason == other.Reason
&& Expires == other.Expires
&& UnbannedBy == other.UnbannedBy
&& (IP == null || IP.Equals(other.IP))
&& CID == other.CID
&& (BanType == BanType.Server
|| (JobBans != null && other.JobBans != null && JobBans.SetEquals(other.JobBans)));
}
}

public static bool operator ==(Ban a, Ban b)
{
return (a is null && b is null) || a.Equals(b);
}

public static bool operator !=(Ban a, Ban b)
{
return !(a == b);
}

public override bool Equals(object obj)
public Ban()
{
return (obj is Ban ban) && Equals(ban);
}

public override int GetHashCode()
{
var hash = new HashCode();
if (BanID != null)
{
hash.Add(BanID);
hash.Add(Source);
}
else
{
hash.Add(Source);
hash.Add(BannedOn);
hash.Add(BanType);
hash.Add(CKey);
hash.Add(BannedBy);
hash.Add(Reason);
hash.Add(Expires);
hash.Add(UnbannedBy);
hash.Add(IP);
hash.Add(CID);
if (JobBans != null)
{
foreach (var job in JobBans.OrderBy(x => x.Job))
{
hash.Add(job.Job, StringComparer.OrdinalIgnoreCase);
}
}
}
return hash.ToHashCode();
}

public void MakeKeysCanonical()
{
CKey = CKey == null ? null : GetCanonicalKey(CKey);
BannedBy = BannedBy == null ? null : GetCanonicalKey(BannedBy);
UnbannedBy = UnbannedBy == null ? null : GetCanonicalKey(UnbannedBy);
}

public static string GetCanonicalKey(string raw)
{
if (raw == null)
{
throw new ArgumentNullException(nameof(raw));
}

return _keyReplacePattern.Replace(raw.ToLower(), "");
JobBans = new HashSet<JobBan>(JobBanEqualityComparer.Instance);
}
}
}
68 changes: 68 additions & 0 deletions CentCom.Common/Models/Equality/BanEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CentCom.Common.Models.Equality
{
public class BanEqualityComparer : IEqualityComparer<Ban>
{
public static readonly BanEqualityComparer Instance = new BanEqualityComparer();

public bool Equals(Ban x, Ban y)
{
if (x is null || y is null) return x == y;
else if (x.BanID != null || y.BanID != null)
{
return x.Source == y.Source
&& x.BanID == y.BanID;
}
else
{
return x.Source == y.Source
&& x.BannedOn == y.BannedOn
&& x.BanType == y.BanType
&& x.CKey == y.CKey
&& x.BannedBy == y.BannedBy
&& x.Reason == y.Reason
&& x.Expires == y.Expires
&& x.UnbannedBy == y.UnbannedBy
&& (x.IP == null || x.IP.Equals(y.IP))
&& x.CID == y.CID
&& (x.BanType == BanType.Server
|| (x.JobBans != null && y.JobBans != null && x.JobBans.SetEquals(y.JobBans)));
}
}

public int GetHashCode(Ban obj)
{
var hash = new HashCode();
if (obj.BanID != null)
{
hash.Add(obj.BanID);
hash.Add(obj.Source);
}
else
{
hash.Add(obj.Source);
hash.Add(obj.BannedOn);
hash.Add(obj.BanType);
hash.Add(obj.CKey);
hash.Add(obj.BannedBy);
hash.Add(obj.Reason);
hash.Add(obj.Expires);
hash.Add(obj.UnbannedBy);
hash.Add(obj.IP);
hash.Add(obj.CID);
if (obj.JobBans != null)
{
foreach (var job in obj.JobBans.OrderBy(x => x.Job))
{
hash.Add(job.Job);
}
}
}
return hash.ToHashCode();
}
}
}
21 changes: 21 additions & 0 deletions CentCom.Common/Models/Equality/JobBanEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CentCom.Common.Models.Equality
{
public class JobBanEqualityComparer : IEqualityComparer<JobBan>
{
public static readonly JobBanEqualityComparer Instance = new JobBanEqualityComparer();

public bool Equals(JobBan x, JobBan y)
{
return (x is null || y is null) ? x == y : x.Job == y.Job;
}

public int GetHashCode(JobBan obj)
{
return HashCode.Combine(obj.Job);
}
}
}
29 changes: 1 addition & 28 deletions CentCom.Common/Models/JobBan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,11 @@

namespace CentCom.Common.Models
{
public class JobBan : IEquatable<JobBan>
public class JobBan
{
public int BanId { get; set; }
[JsonIgnore]
public virtual Ban BanNavigation { get; set; }
public string Job { get; set; }

public bool Equals(JobBan other)
{
return !(other is null) && Job == other.Job;
}

public static bool operator ==(JobBan a, JobBan b)
{
return (a is null && b is null) || a.Equals(b);
}

public static bool operator !=(JobBan a, JobBan b)
{
return !(a == b);
}

public override bool Equals(object obj)
{
return (obj is JobBan jb) && Equals(jb);
}

public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(Job, StringComparer.OrdinalIgnoreCase);
return hash.ToHashCode();
}
}
}
23 changes: 20 additions & 3 deletions CentCom.Server/BanSources/BanParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using CentCom.Common.Data;
using CentCom.Server.Exceptions;
using CentCom.Server.Extensions;
using CentCom.Common.Models.Equality;

namespace CentCom.Server.BanSources
{
Expand Down Expand Up @@ -128,19 +130,28 @@ public virtual async Task ParseBans(IJobExecutionContext context)
&& b.CKey == x.CKey
&& b.BannedBy == x.BannedBy
&& (b.BanType == BanType.Server
|| (b.JobBans != null && x.JobBans != null && b.JobBans.SetEquals(x.JobBans))));
|| b.JobBans.SetEquals(x.JobBans)));
}

// Update ban if an existing one is found
if (matchedBan != null)
{
// Check for a difference in date time, unbans, or reason
if (matchedBan.Reason != b.Reason || matchedBan.Expires != b.Expires || matchedBan.UnbannedBy != b.UnbannedBy)
{
matchedBan.Reason = b.Reason;
matchedBan.Expires = b.Expires;
matchedBan.UnbannedBy = b.UnbannedBy;
updated++;
}

// Check for a difference in recorded jobbans
if (b.BanType == BanType.Job && !b.JobBans.SetEquals(matchedBan.JobBans))
{
matchedBan.JobBans = new HashSet<JobBan>(JobBanEqualityComparer.Instance);
matchedBan.AddJobRange(b.JobBans.Select(x => x.Job));
updated++;
}
}
// Otherwise add insert a new ban
else
Expand All @@ -157,8 +168,14 @@ public virtual async Task ParseBans(IJobExecutionContext context)
// Delete any missing bans if we're doing a complete refresh
if (isCompleteRefresh)
{
var bansHashed = new HashSet<Ban>(bans);
var missingBans = storedBans.Except(bansHashed).ToHashSet();
var bansHashed = new HashSet<Ban>(bans, BanEqualityComparer.Instance);
var missingBans = storedBans.Except(bansHashed, BanEqualityComparer.Instance).ToList();

if (bansHashed.Count == 0 && missingBans.Count > 1)
{
throw new Exception("Failed to find any bans for source, aborting removal phase of ban " +
"parsing to avoid dumping entire set of bans");
}

// Apply deletions
_logger.LogInformation(missingBans.Count > 0 ? $"Removing {missingBans.Count} deleted bans..."
Expand Down
66 changes: 66 additions & 0 deletions CentCom.Server/BanSources/TGMCBanParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using CentCom.Common.Data;
using CentCom.Common.Models;
using CentCom.Server.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CentCom.Server.BanSources
{
public class TGMCBanParser : BanParser
{
public override Dictionary<string, BanSource> Sources => new Dictionary<string, BanSource>()
{
{ "tgmc", new BanSource()
{
Display = "TGMC",
Name = "tgmc",
RoleplayLevel = RoleplayLevel.Medium
} }
};
public override bool SourceSupportsBanIDs => true;
private TGMCBanService _banService;
private const int PAGES_PER_BATCH = 3;

public TGMCBanParser(DatabaseContext dbContext, TGMCBanService banService, ILogger<TGMCBanParser> logger) : base(dbContext, logger)
{
_banService = banService;
}

public override async Task<IEnumerable<Ban>> FetchAllBansAsync()
{
_logger.LogInformation("Getting all bans for TGMC...");
return await _banService.GetBansBatchedAsync();
}

public override async Task<IEnumerable<Ban>> FetchNewBansAsync()
{
_logger.LogInformation("Getting new bans for TGMC...");
var recent = await _dbContext.Bans
.Where(x => Sources.Keys.Contains(x.SourceNavigation.Name))
.OrderByDescending(x => x.BannedOn)
.Take(5)
.Include(x => x.JobBans)
.Include(x => x.SourceNavigation)
.ToListAsync();
var foundBans = new List<Ban>();
var page = 1;

while (true)
{
var batch = await _banService.GetBansBatchedAsync(page, PAGES_PER_BATCH);
foundBans.AddRange(batch);
if (batch.Count() == 0 || batch.Any(x => recent.Any(y => y.BannedOn == x.BannedOn && y.CKey == y.CKey)))
{
break;
}
}

return foundBans;
}
}
}
Loading

0 comments on commit a7cc043

Please sign in to comment.