Skip to content

Commit

Permalink
Fixed [JsonIgnore]
Browse files Browse the repository at this point in the history
Fixed migration penalty creation
Fixed on migration command execution
Moved out CreatePenalty
Removed ClientId & AdminId since handled by Penalties
  • Loading branch information
Ayymoss authored and RaidMax committed Oct 13, 2022
1 parent 9774db1 commit 998842a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 59 deletions.
53 changes: 24 additions & 29 deletions Plugins/Mute/MuteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ public async Task<MuteStateMeta> GetCurrentMuteState(EFClient client)
var muteState = await ReadPersistentDataV1(client);
clientMuteMeta = new MuteStateMeta
{
ClientId = client.ClientId,
Reason = muteState is null ? string.Empty : _translationLookup["PLUGINS_MUTE_MIGRATED"],
Expiration = muteState switch
{
null => DateTime.UtcNow,
MuteState.Muted => null,
_ => DateTime.UtcNow
},
AdminId = Utilities.IW4MAdminClient().ClientId,
MuteState = muteState ?? MuteState.Unmuted,
CommandExecuted = true
};

// Migrate old mute meta, else, client has no state, so set a generic one, but don't write it to database.
if (muteState is not null)
{
clientMuteMeta.CommandExecuted = false;
await WritePersistentData(client, clientMuteMeta);
await CreatePenalty(muteState.Value, Utilities.IW4MAdminClient(), client, clientMuteMeta.Expiration,
clientMuteMeta.Reason);
}
else
{
Expand All @@ -64,25 +65,13 @@ public async Task<bool> Mute(Server server, EFClient origin, EFClient target, Da
var clientMuteMeta = await GetCurrentMuteState(target);
if (clientMuteMeta.MuteState is MuteState.Muted && clientMuteMeta.CommandExecuted) return false;

var newPenalty = new EFPenalty
{
Type = dateTime is null ? EFPenalty.PenaltyType.Mute : EFPenalty.PenaltyType.TempMute,
Expires = dateTime,
Offender = target,
Offense = reason,
Punisher = origin,
Link = target.AliasLink
};
_logger.LogDebug("Creating new mute for {Target} with reason {Reason}", target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
await CreatePenalty(MuteState.Muted, origin, target, dateTime, reason);

clientMuteMeta = new MuteStateMeta
{
ClientId = target.ClientId,
Expiration = dateTime,
MuteState = MuteState.Muted,
Reason = reason,
AdminId = origin.ClientId,
CommandExecuted = false
};
await WritePersistentData(target, clientMuteMeta);
Expand All @@ -100,26 +89,13 @@ public async Task<bool> Unmute(Server server, EFClient origin, EFClient target,
if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false;
if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false;

var newPenalty = new EFPenalty
{
Type = EFPenalty.PenaltyType.Unmute,
Expires = DateTime.Now,
Offender = target,
Offense = reason,
Punisher = origin,
Active = true,
Link = target.AliasLink
};
_logger.LogDebug("Creating new unmute for {Target} with reason {Reason}", target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason);

clientMuteMeta = new MuteStateMeta
{
ClientId = target.ClientId,
Expiration = DateTime.UtcNow,
MuteState = target.IsIngame ? MuteState.Unmuted : MuteState.Unmuting,
Reason = reason,
AdminId = origin.ClientId,
CommandExecuted = false
};
await WritePersistentData(target, clientMuteMeta);
Expand All @@ -131,6 +107,25 @@ public async Task<bool> Unmute(Server server, EFClient origin, EFClient target,
return true;
}

private async Task CreatePenalty(MuteState muteState, EFClient origin, EFClient target, DateTime? dateTime,
string reason)
{
var newPenalty = new EFPenalty
{
Type = muteState is MuteState.Unmuted ? EFPenalty.PenaltyType.Unmute :
dateTime is null ? EFPenalty.PenaltyType.Mute : EFPenalty.PenaltyType.TempMute,
Expires = muteState is MuteState.Unmuted ? DateTime.UtcNow : dateTime,
Offender = target,
Offense = reason,
Punisher = origin,
Active = true,
Link = target.AliasLink
};
_logger.LogDebug("Creating new {MuteState} Penalty for {Target} with reason {Reason}",
nameof(muteState), target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
}

public static async Task PerformGameCommand(Server server, EFClient? client, MuteStateMeta muteStateMeta)
{
if (client is null || !client.IsIngame) return;
Expand Down
7 changes: 2 additions & 5 deletions Plugins/Mute/MuteStateMeta.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Mute;

public class MuteStateMeta
{
public int ClientId { get; set; }
public string Reason { get; set; } = string.Empty;
public DateTime? Expiration { get; set; }
public int AdminId { get; set; }
public MuteState MuteState { get; set; }
[JsonIgnore]
public bool CommandExecuted { get; set; }
[JsonIgnore] public bool CommandExecuted { get; set; }
}

public enum MuteState
Expand Down
52 changes: 27 additions & 25 deletions Plugins/Mute/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public async Task OnEventAsync(GameEvent gameEvent, Server server)
{
if (!SupportedGames.Contains(server.GameName)) return;


switch (gameEvent.Type)
{
case GameEvent.EventType.Command:
Expand Down Expand Up @@ -129,13 +128,13 @@ public Task OnLoadAsync(IManager manager)

_interactionRegistration.RegisterInteraction(MuteInteraction, async (targetClientId, game, token) =>
{
if (!targetClientId.HasValue || game.HasValue && !SupportedGames.Contains((Server.Game)game.Value))
if (!targetClientId.HasValue || game.HasValue && !SupportedGames.Contains((Server.Game) game.Value))
{
return null;
}
var clientMuteMetaState =
(await MuteManager.GetCurrentMuteState(new EFClient { ClientId = targetClientId.Value }))
(await MuteManager.GetCurrentMuteState(new EFClient {ClientId = targetClientId.Value}))
.MuteState;
var server = manager.GetServers().First();
Expand All @@ -149,33 +148,34 @@ string GetCommandName(Type commandType) =>
return Task.CompletedTask;
}

private InteractionData CreateMuteInteraction(int targetClientId, Server server, Func<Type, string> getCommandNameFunc)
private InteractionData CreateMuteInteraction(int targetClientId, Server server,
Func<Type, string> getCommandNameFunc)
{
var reasonInput = new
{
Name = "Reason",
Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_REASON"],
Type = "text",
Values = (Dictionary<string, string>?)null
Values = (Dictionary<string, string>?) null
};

var durationInput = new
{
Name = "Duration",
Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_DURATION"],
Type = "select",
Values = (Dictionary<string, string>?)new Dictionary<string, string>
Values = (Dictionary<string, string>?) new Dictionary<string, string>
{
{ "5m", TimeSpan.FromMinutes(5).HumanizeForCurrentCulture() },
{ "30m", TimeSpan.FromMinutes(30).HumanizeForCurrentCulture() },
{ "1h", TimeSpan.FromHours(1).HumanizeForCurrentCulture() },
{ "6h", TimeSpan.FromHours(6).HumanizeForCurrentCulture() },
{ "1d", TimeSpan.FromDays(1).HumanizeForCurrentCulture() },
{ "p", Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_SELECTION_PERMANENT"] }
{"5m", TimeSpan.FromMinutes(5).HumanizeForCurrentCulture()},
{"30m", TimeSpan.FromMinutes(30).HumanizeForCurrentCulture()},
{"1h", TimeSpan.FromHours(1).HumanizeForCurrentCulture()},
{"6h", TimeSpan.FromHours(6).HumanizeForCurrentCulture()},
{"1d", TimeSpan.FromDays(1).HumanizeForCurrentCulture()},
{"p", Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_SELECTION_PERMANENT"]}
}
};
var inputs = new[] { reasonInput, durationInput };

var inputs = new[] {reasonInput, durationInput};
var inputsJson = JsonSerializer.Serialize(inputs);

return new InteractionData
Expand All @@ -186,8 +186,8 @@ private InteractionData CreateMuteInteraction(int targetClientId, Server server,
ActionPath = "DynamicAction",
ActionMeta = new()
{
{ "InteractionId", MuteInteraction },
{ "Inputs", inputsJson },
{"InteractionId", MuteInteraction},
{"Inputs", inputsJson},
{
"ActionButtonLabel",
Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"]
Expand All @@ -196,7 +196,7 @@ private InteractionData CreateMuteInteraction(int targetClientId, Server server,
"Name",
Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"]
},
{ "ShouldRefresh", true.ToString() }
{"ShouldRefresh", true.ToString()}
},
MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator,
Source = Name,
Expand Down Expand Up @@ -230,7 +230,8 @@ private InteractionData CreateMuteInteraction(int targetClientId, Server server,
};
}

private InteractionData CreateUnmuteInteraction(int targetClientId, Server server, Func<Type, string> getCommandNameFunc)
private InteractionData CreateUnmuteInteraction(int targetClientId, Server server,
Func<Type, string> getCommandNameFunc)
{
var reasonInput = new
{
Expand All @@ -239,7 +240,7 @@ private InteractionData CreateUnmuteInteraction(int targetClientId, Server serve
Type = "text",
};

var inputs = new[] { reasonInput };
var inputs = new[] {reasonInput};
var inputsJson = JsonSerializer.Serialize(inputs);

return new InteractionData
Expand All @@ -251,9 +252,9 @@ private InteractionData CreateUnmuteInteraction(int targetClientId, Server serve
ActionPath = "DynamicAction",
ActionMeta = new()
{
{ "InteractionId", MuteInteraction },
{ "Outputs", reasonInput.Name },
{ "Inputs", inputsJson },
{"InteractionId", MuteInteraction},
{"Outputs", reasonInput.Name},
{"Inputs", inputsJson},
{
"ActionButtonLabel",
Utilities.CurrentLocalization.LocalizationIndex[
Expand All @@ -264,7 +265,7 @@ private InteractionData CreateUnmuteInteraction(int targetClientId, Server serve
Utilities.CurrentLocalization.LocalizationIndex[
"WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_UNMUTE"]
},
{ "ShouldRefresh", true.ToString() }
{"ShouldRefresh", true.ToString()}
},
MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator,
Source = Name,
Expand All @@ -283,7 +284,8 @@ private InteractionData CreateUnmuteInteraction(int targetClientId, Server serve
}
var commandResponse =
await _remoteCommandService.Execute(originId, targetId, getCommandNameFunc(typeof(UnmuteCommand)), args, server);
await _remoteCommandService.Execute(originId, targetId, getCommandNameFunc(typeof(UnmuteCommand)),
args, server);
return string.Join(".", commandResponse.Select(result => result.Response));
}
};
Expand Down

0 comments on commit 998842a

Please sign in to comment.