Skip to content

Commit

Permalink
Refactor MuteManager constructor and clean up code (#329)
Browse files Browse the repository at this point in the history
The MuteManager constructor within the Mute plugin has been refactored for better dependency injection. This change simplifies the class construction by directly initializing fields in the constructor parameters. Additionally, several minor code improvements have been made, including spelling corrections and replacing some conditional checks for readability. Other arrays or methods in the plugin are also revised for better maintainability and readability of the code.
  • Loading branch information
Ayymoss authored Jun 30, 2024
1 parent 40f9125 commit ba633be
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 71 deletions.
6 changes: 3 additions & 3 deletions Plugins/Mute/Commands/MuteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public MuteCommand(CommandConfiguration config, ITranslationLookup translationLo
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
Arguments =
[
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Expand All @@ -32,7 +32,7 @@ public MuteCommand(CommandConfiguration config, ITranslationLookup translationLo
Name = translationLookup["COMMANDS_ARGS_REASON"],
Required = true
}
};
];
}

public override async Task ExecuteAsync(GameEvent gameEvent)
Expand Down
6 changes: 3 additions & 3 deletions Plugins/Mute/Commands/MuteInfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public MuteInfoCommand(CommandConfiguration config, ITranslationLookup translati
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
Arguments =
[
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Required = true
}
};
];
}

public override async Task ExecuteAsync(GameEvent gameEvent)
Expand Down
16 changes: 9 additions & 7 deletions Plugins/Mute/Commands/TempMuteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace IW4MAdmin.Plugins.Mute.Commands;

public class TempMuteCommand : Command
public partial class TempMuteCommand : Command
{
private readonly MuteManager _muteManager;
private const string TempBanRegex = @"([0-9]+\w+)\ (.+)";

public TempMuteCommand(CommandConfiguration config, ITranslationLookup translationLookup, MuteManager muteManager) : base(config,
translationLookup)
Expand All @@ -22,8 +21,8 @@ public TempMuteCommand(CommandConfiguration config, ITranslationLookup translati
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
Arguments =
[
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Expand All @@ -39,7 +38,7 @@ public TempMuteCommand(CommandConfiguration config, ITranslationLookup translati
Name = translationLookup["COMMANDS_ARGS_REASON"],
Required = true
}
};
];
}

public override async Task ExecuteAsync(GameEvent gameEvent)
Expand All @@ -49,8 +48,8 @@ public override async Task ExecuteAsync(GameEvent gameEvent)
gameEvent.Origin.Tell(_translationLookup["COMMANDS_DENY_SELF_TARGET"]);
return;
}
var match = Regex.Match(gameEvent.Data, TempBanRegex);

var match = TempBanRegex().Match(gameEvent.Data);
if (match.Success)
{
var expiration = DateTime.UtcNow + match.Groups[1].ToString().ParseTimespan();
Expand All @@ -72,4 +71,7 @@ public override async Task ExecuteAsync(GameEvent gameEvent)

gameEvent.Origin.Tell(_translationLookup["PLUGINS_MUTE_COMMANDS_TEMPMUTE_BAD_FORMAT"]);
}

[GeneratedRegex(@"([0-9]+\w+)\ (.+)")]
private static partial Regex TempBanRegex();
}
6 changes: 3 additions & 3 deletions Plugins/Mute/Commands/UnmuteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public UnmuteCommand(CommandConfiguration config, ITranslationLookup translation
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
Arguments =
[
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Expand All @@ -32,7 +32,7 @@ public UnmuteCommand(CommandConfiguration config, ITranslationLookup translation
Name = translationLookup["COMMANDS_ARGS_REASON"],
Required = true
}
};
];
}

public override async Task ExecuteAsync(GameEvent gameEvent)
Expand Down
1 change: 1 addition & 0 deletions Plugins/Mute/Mute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<OutputType>Library</OutputType>
<Configurations>Debug;Release;Prerelease</Configurations>
<Platforms>AnyCPU</Platforms>
<RootNamespace>IW4MAdmin.Plugins.Mute</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
30 changes: 11 additions & 19 deletions Plugins/Mute/MuteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@

namespace IW4MAdmin.Plugins.Mute;

public class MuteManager
public class MuteManager(
ILogger<MuteManager> logger,
IDatabaseContextFactory databaseContextFactory,
IMetaServiceV2 metaService,
ITranslationLookup translationLookup)
{
private readonly IMetaServiceV2 _metaService;
private readonly ITranslationLookup _translationLookup;
private readonly ILogger _logger;
private readonly IDatabaseContextFactory _databaseContextFactory;
private readonly ILogger _logger = logger;
private readonly SemaphoreSlim _onMuteAction = new(1, 1);

public MuteManager(ILogger<MuteManager> logger, IDatabaseContextFactory databaseContextFactory,
IMetaServiceV2 metaService, ITranslationLookup translationLookup)
{
_logger = logger;
_databaseContextFactory = databaseContextFactory;
_metaService = metaService;
_translationLookup = translationLookup;
}

public static bool IsExpiredMute(MuteStateMeta muteStateMeta) =>
muteStateMeta.Expiration is not null && muteStateMeta.Expiration < DateTime.UtcNow;

Expand All @@ -42,7 +34,7 @@ public async Task<MuteStateMeta> GetCurrentMuteState(EFClient client)
var muteState = await ReadPersistentDataV1(client);
clientMuteMeta = new MuteStateMeta
{
Reason = muteState is null ? string.Empty : _translationLookup["PLUGINS_MUTE_MIGRATED"],
Reason = muteState is null ? string.Empty : translationLookup["PLUGINS_MUTE_MIGRATED"],
Expiration = muteState switch
{
null => DateTime.UtcNow,
Expand Down Expand Up @@ -149,7 +141,7 @@ private async Task CreatePenalty(MuteState muteState, EFClient origin, EFClient

private async Task ExpireMutePenalties(EFClient client)
{
await using var context = _databaseContextFactory.CreateContext();
await using var context = databaseContextFactory.CreateContext();
var mutePenalties = await context.Penalties
.Where(penalty => penalty.OffenderId == client.ClientId)
.Where(penalty => penalty.Type == EFPenalty.PenaltyType.Mute || penalty.Type == EFPenalty.PenaltyType.TempMute)
Expand Down Expand Up @@ -184,7 +176,7 @@ public static async Task PerformGameCommand(Server server, EFClient? client, Mut
}

private async Task<MuteState?> ReadPersistentDataV1(EFClient client) => TryParse<MuteState>(
(await _metaService.GetPersistentMeta(Plugin.MuteKey, client.ClientId))?.Value, out var muteState)
(await metaService.GetPersistentMeta(Plugin.MuteKey, client.ClientId))?.Value, out var muteState)
? muteState
: null;

Expand All @@ -195,7 +187,7 @@ public static async Task PerformGameCommand(Server server, EFClient? client, Mut
if (clientMuteMeta is not null) return clientMuteMeta;

// Get meta from database and store in client if exists
clientMuteMeta = await _metaService.GetPersistentMetaValue<MuteStateMeta>(Plugin.MuteKey, client.ClientId);
clientMuteMeta = await metaService.GetPersistentMetaValue<MuteStateMeta>(Plugin.MuteKey, client.ClientId);
if (clientMuteMeta is not null) client.SetAdditionalProperty(Plugin.MuteKey, clientMuteMeta);

return clientMuteMeta;
Expand All @@ -204,6 +196,6 @@ public static async Task PerformGameCommand(Server server, EFClient? client, Mut
private async Task WritePersistentData(EFClient client, MuteStateMeta clientMuteMeta)
{
client.SetAdditionalProperty(Plugin.MuteKey, clientMuteMeta);
await _metaService.SetPersistentMetaValue(Plugin.MuteKey, clientMuteMeta, client.ClientId);
await metaService.SetPersistentMetaValue(Plugin.MuteKey, clientMuteMeta, client.ClientId);
}
}
Loading

0 comments on commit ba633be

Please sign in to comment.