Fix Threading Duplicate for Mute Penalty & Added !MuteInfo & Fix PM (#269)

* Resolve duplicate migration
Resolve unmuting state double penalties

* Change order of operation

* Added MuteInfoCommand.cs

* Resolve !pm and @broadcast permanently being disabled
This commit is contained in:
Amos 2022-10-14 14:47:01 +01:00 committed by GitHub
parent 76925a78d4
commit cf51b83cdd
3 changed files with 102 additions and 34 deletions

View File

@ -0,0 +1,50 @@
using Data.Models.Client;
using Humanizer;
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace Mute.Commands;
public class MuteInfoCommand : Command
{
public MuteInfoCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config,
translationLookup)
{
Name = "muteinfo";
Description = translationLookup["PLUGINS_MUTE_COMMANDS_MUTEINFO_DESC"];
Alias = "mi";
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Required = true
}
};
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
var currentMuteMeta = await Plugin.MuteManager.GetCurrentMuteState(gameEvent.Target);
switch (currentMuteMeta.MuteState)
{
case MuteState.Muted when currentMuteMeta.Expiration is null:
gameEvent.Origin.Tell(_translationLookup["PLUGINS_MUTE_COMMANDS_MUTEINFO_SUCCESS"]
.FormatExt(gameEvent.Target.Name, currentMuteMeta.Reason));
return;
case MuteState.Muted when currentMuteMeta.Expiration.HasValue && currentMuteMeta.Expiration.Value > DateTime.UtcNow:
var remainingTime = (currentMuteMeta.Expiration.Value - DateTime.UtcNow).HumanizeForCurrentCulture();
gameEvent.Origin.Tell(_translationLookup["PLUGINS_MUTE_COMMANDS_MUTEINFO_TM_SUCCESS"]
.FormatExt(gameEvent.Target.Name, currentMuteMeta.Reason, remainingTime));
return;
default:
gameEvent.Origin.Tell(_translationLookup["PLUGINS_MUTE_COMMANDS_MUTEINFO_NONE"]);
break;
}
}
}

View File

@ -13,6 +13,7 @@ public class MuteManager
private readonly IMetaServiceV2 _metaService; private readonly IMetaServiceV2 _metaService;
private readonly ITranslationLookup _translationLookup; private readonly ITranslationLookup _translationLookup;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly SemaphoreSlim _onMuteAction = new(1, 1);
public MuteManager(IMetaServiceV2 metaService, ITranslationLookup translationLookup, ILogger logger) public MuteManager(IMetaServiceV2 metaService, ITranslationLookup translationLookup, ILogger logger)
{ {
@ -26,6 +27,9 @@ public class MuteManager
public async Task<MuteStateMeta> GetCurrentMuteState(EFClient client) public async Task<MuteStateMeta> GetCurrentMuteState(EFClient client)
{ {
try
{
await _onMuteAction.WaitAsync();
var clientMuteMeta = await ReadPersistentDataV2(client); var clientMuteMeta = await ReadPersistentDataV2(client);
if (clientMuteMeta is not null) return clientMuteMeta; if (clientMuteMeta is not null) return clientMuteMeta;
@ -59,14 +63,17 @@ public class MuteManager
return clientMuteMeta; return clientMuteMeta;
} }
finally
{
if (_onMuteAction.CurrentCount == 0) _onMuteAction.Release();
}
}
public async Task<bool> Mute(Server server, EFClient origin, EFClient target, DateTime? dateTime, string reason) public async Task<bool> Mute(Server server, EFClient origin, EFClient target, DateTime? dateTime, string reason)
{ {
var clientMuteMeta = await GetCurrentMuteState(target); var clientMuteMeta = await GetCurrentMuteState(target);
if (clientMuteMeta.MuteState is MuteState.Muted && clientMuteMeta.CommandExecuted) return false; if (clientMuteMeta.MuteState is MuteState.Muted && clientMuteMeta.CommandExecuted) return false;
await CreatePenalty(MuteState.Muted, origin, target, dateTime, reason);
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {
Expiration = dateTime, Expiration = dateTime,
@ -76,6 +83,8 @@ public class MuteManager
}; };
await WritePersistentData(target, clientMuteMeta); await WritePersistentData(target, clientMuteMeta);
await CreatePenalty(MuteState.Muted, origin, target, dateTime, reason);
// Handle game command // Handle game command
var client = server.GetClientsAsList().FirstOrDefault(client => client.NetworkId == target.NetworkId); var client = server.GetClientsAsList().FirstOrDefault(client => client.NetworkId == target.NetworkId);
await PerformGameCommand(server, client, clientMuteMeta); await PerformGameCommand(server, client, clientMuteMeta);
@ -89,7 +98,10 @@ public class MuteManager
if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false; if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false;
if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false; if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false;
if (clientMuteMeta.MuteState is not MuteState.Unmuting)
{
await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason); await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason);
}
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {

View File

@ -38,9 +38,6 @@ public class Plugin : IPlugin
switch (gameEvent.Type) switch (gameEvent.Type)
{ {
case GameEvent.EventType.Command:
break;
case GameEvent.EventType.Join: case GameEvent.EventType.Join:
// Check if user has any meta set, else ignore (unmuted) // Check if user has any meta set, else ignore (unmuted)
var muteMetaJoin = await MuteManager.GetCurrentMuteState(gameEvent.Origin); var muteMetaJoin = await MuteManager.GetCurrentMuteState(gameEvent.Origin);
@ -122,7 +119,16 @@ public class Plugin : IPlugin
manager.CommandInterceptors.Add(gameEvent => manager.CommandInterceptors.Add(gameEvent =>
{ {
if (gameEvent.Extra is not Command command) if (gameEvent.Extra is not Command command)
{
return true; return true;
}
var muteMeta = MuteManager.GetCurrentMuteState(gameEvent.Origin).GetAwaiter().GetResult();
if (muteMeta.MuteState is not MuteState.Muted)
{
return true;
}
return !DisabledCommands.Contains(command.GetType().Name) && !command.IsBroadcast; return !DisabledCommands.Contains(command.GetType().Name) && !command.IsBroadcast;
}); });