Skip to content

Commit

Permalink
Merge pull request #51 from AffectedArc07/para-stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbah authored Apr 9, 2023
2 parents da3f64a + ee83105 commit 626e323
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 52 deletions.
5 changes: 5 additions & 0 deletions CentCom.Common/Abstract/IRestBan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public interface IRestBan
/// The list of job bans, if present
/// </summary>
public IReadOnlyList<IRestJobBan> JobBans { get; }

/// <summary>
/// The optional Round ID of the ban, if present
/// </summary>
public int? RoundId { get; }
}
3 changes: 2 additions & 1 deletion CentCom.Common/Models/Rest/RestBan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public record RestBan
string Reason,
DateTimeOffset? Expires,
ICKey UnbannedBy,
IReadOnlyList<IRestJobBan> JobBans
IReadOnlyList<IRestJobBan> JobBans,
int? RoundId
) : IRestBan;
8 changes: 4 additions & 4 deletions CentCom.Exporter/CentCom.Exporter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1"/>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CentCom.Common\CentCom.Common.csproj"/>
<ProjectReference Include="..\CentCom.Common\CentCom.Common.csproj" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion CentCom.Exporter/Configuration/BanProviderKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ public enum BanProviderKind
/// <summary>
/// Specifies using a <see cref="CentCom.Exporter.Data.Providers.TgBanProvider" />
/// </summary>
Tgstation
Tgstation,
/// <summary>
/// Specifies using a <see cref="CentCom.Exporter.Data.Providers.ParadiseBanProvider" />
/// </summary>
ParadiseSS13,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
using CentCom.Common.Models.Byond;
using CentCom.Common.Models.Rest;

namespace CentCom.Exporter.Data;
namespace CentCom.Exporter.Data.Ban;

/// <summary>
/// Standard exported ban from a IBanProvider
/// </summary>
/// <remarks>
/// This class may require modification should another IBanProvider implementation necessitate it
/// </remarks>
public class ExportedBan
{
public class ExportedBan {
/// <summary>
/// The database ID of the ban
/// </summary>
Expand Down Expand Up @@ -66,6 +65,11 @@ public class ExportedBan
/// </summary>
public CKey UnbannedBy { get; set; }

/// <summary>
/// The Round ID of the ban, if any
/// </summary>
public int? RoundId { get; set; }

public static implicit operator RestBan(ExportedBan ban) => new RestBan
(
ban.Id,
Expand All @@ -76,6 +80,7 @@ public class ExportedBan
ban.Reason,
ban.Expiration,
ban.UnbannedBy,
ban.BanType == BanType.Job ? new[] { new RestJobBan(ban.Role) } : null
ban.BanType == BanType.Job ? new[] { new RestJobBan(ban.Role) } : null,
ban.RoundId
);
}
29 changes: 29 additions & 0 deletions CentCom.Exporter/Data/Ban/ParadiseExportedBan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using CentCom.Common.Models;
using CentCom.Common.Models.Byond;
using CentCom.Common.Models.Rest;

namespace CentCom.Exporter.Data.Ban;
public class ParadiseExportedBan : ExportedBan {
public new BanType BanType => ((InternalBanType.Equals("PERMABAN") || InternalBanType.Equals("TEMPBAN")) ? BanType.Server : BanType.Job);

/// <summary>
/// The internal type for this ban.
/// </summary>
public string InternalBanType { get; set; }

// If you dont override this, stuff breaks
public static implicit operator RestBan(ParadiseExportedBan ban) => new RestBan
(
ban.Id,
ban.BanType,
ban.CKey,
ban.BannedAt,
ban.BannedBy,
ban.Reason,
ban.Expiration,
ban.UnbannedBy,
ban.BanType == BanType.Job ? new[] { new RestJobBan(ban.Role) } : null,
ban.RoundId
);
}
39 changes: 0 additions & 39 deletions CentCom.Exporter/Data/BanClusterer.cs

This file was deleted.

19 changes: 19 additions & 0 deletions CentCom.Exporter/Data/Clustering/IBanClusterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using CentCom.Common.Abstract;
using CentCom.Common.Models;
using CentCom.Common.Models.Rest;

namespace CentCom.Exporter.Data.Clustering;

/// <summary>
/// Provides utilities for clustering bans such that job bans are correctly grouped into one ban
/// </summary>
public interface IBanClusterer {
/// <summary>
/// Clusters a collection of IRestBans to ensure that all job bans are grouped appropriately
/// </summary>
/// <param name="bans">The bans to cluster</param>
/// <returns>The collection of bans with all job bans clustered appropriately</returns>
public IEnumerable<IRestBan> ClusterBans(IEnumerable<IRestBan> bans);
}
30 changes: 30 additions & 0 deletions CentCom.Exporter/Data/Clustering/ParadiseBanClusterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Linq;
using CentCom.Common.Abstract;
using CentCom.Common.Models;
using CentCom.Common.Models.Rest;

namespace CentCom.Exporter.Data.Clustering;

public class ParadiseBanClusterer : IBanClusterer {
/// <inheritdoc/>
/// <remarks>
/// Job bans are considered to be in a group if the ckey, banning ckey, reason,
/// expiration timestamp, and unbanned ckey are the same.
/// </remarks>
public IEnumerable<IRestBan> ClusterBans(IEnumerable<IRestBan> bans) {
var clusteredBans = bans.Where(x => x.BanType == BanType.Server).ToList();
clusteredBans.AddRange(bans.Where(x => x.BanType == BanType.Job).GroupBy(x =>
new { x.CKey, x.BannedBy, x.Reason, x.UnbannedBy, x.RoundId }).Select(group => {
var ban = group.Last();
return new RestBan(ban.Id, ban.BanType, ban.CKey, ban.BannedOn, ban.BannedBy, ban.Reason, ban.Expires,
ban.UnbannedBy,
group.SelectMany(j => j.JobBans)
.Select(j => j.Job)
.Distinct()
.Select(j => (IRestJobBan)new RestJobBan(j)).ToList(),
ban.RoundId);
}));
return clusteredBans.OrderByDescending(x => x.Id);
}
}
29 changes: 29 additions & 0 deletions CentCom.Exporter/Data/Clustering/TgBanClusterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using CentCom.Common.Abstract;
using CentCom.Common.Models;
using CentCom.Common.Models.Rest;

namespace CentCom.Exporter.Data.Clustering;

public class TgBanClusterer : IBanClusterer {
/// <inheritdoc/>
/// <remarks>
/// Job bans are considered to be in a group if the ckey, banning ckey, reason,
/// banned timestamp, expiration timestamp, and unbanned ckey are the same.
/// </remarks>
public IEnumerable<IRestBan> ClusterBans(IEnumerable<IRestBan> bans) {
var clusteredBans = bans.Where(x => x.BanType == BanType.Server).ToList();
clusteredBans.AddRange(bans.Where(x => x.BanType == BanType.Job).GroupBy(x =>
new { x.CKey, x.BannedBy, x.Reason, x.BannedOn, x.Expires, x.UnbannedBy }).Select(group => {
var ban = group.Last();
return new RestBan(ban.Id, ban.BanType, ban.CKey, ban.BannedOn, ban.BannedBy, ban.Reason, ban.Expires,
ban.UnbannedBy,
group.SelectMany(j => j.JobBans)
.Select(j => j.Job)
.Distinct()
.Select(j => (IRestJobBan)new RestJobBan(j)).ToList(), null);
}));
return clusteredBans.OrderByDescending(x => x.Id);
}
}
Loading

0 comments on commit 626e323

Please sign in to comment.