From 18f23fd07d7928baceda284b5889431f5dbc1cd7 Mon Sep 17 00:00:00 2001 From: Amos Date: Fri, 26 Aug 2022 18:09:33 +0100 Subject: [PATCH] Adding Mute for IW4x (#257) * Adding Mute for IW4x --- IW4MAdmin.sln | 27 +++++++++++++ Plugins/Mute/Commands/MuteCommand.cs | 42 +++++++++++++++++++ Plugins/Mute/DataManager.cs | 38 ++++++++++++++++++ Plugins/Mute/Mute.csproj | 20 ++++++++++ Plugins/Mute/MuteManager.cs | 21 ++++++++++ Plugins/Mute/Plugin.cs | 60 ++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+) create mode 100644 Plugins/Mute/Commands/MuteCommand.cs create mode 100644 Plugins/Mute/DataManager.cs create mode 100644 Plugins/Mute/Mute.csproj create mode 100644 Plugins/Mute/MuteManager.cs create mode 100644 Plugins/Mute/Plugin.cs diff --git a/IW4MAdmin.sln b/IW4MAdmin.sln index cc4007448..e72313ca6 100644 --- a/IW4MAdmin.sln +++ b/IW4MAdmin.sln @@ -69,6 +69,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integrations.Cod", "Integra EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integrations.Source", "Integrations\Source\Integrations.Source.csproj", "{9512295B-3045-40E0-9B7E-2409F2173E9D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mute", "Plugins\Mute\Mute.csproj", "{259824F3-D860-4233-91D6-FF73D4DD8B18}" +EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GameFiles", "GameFiles", "{6CBF412C-EFEE-45F7-80FD-AC402C22CDB9}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GameInterface", "GameInterface", "{5C2BE2A8-EA1D-424F-88E1-7FC33EEC2E55}" @@ -401,6 +403,30 @@ Global {9512295B-3045-40E0-9B7E-2409F2173E9D}.Release|x86.Build.0 = Release|Any CPU {9512295B-3045-40E0-9B7E-2409F2173E9D}.Prerelease|Any CPU.ActiveCfg = Prerelease|Any CPU {9512295B-3045-40E0-9B7E-2409F2173E9D}.Prerelease|Any CPU.Build.0 = Prerelease|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|x64.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|x64.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|x86.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Debug|x86.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|Mixed Platforms.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|Mixed Platforms.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|x64.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|x64.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|x86.ActiveCfg = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|x86.Build.0 = Debug|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|Any CPU.Build.0 = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|x64.ActiveCfg = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|x64.Build.0 = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|x86.ActiveCfg = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Release|x86.Build.0 = Release|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|Any CPU.ActiveCfg = Prerelease|Any CPU + {259824F3-D860-4233-91D6-FF73D4DD8B18}.Prerelease|Any CPU.Build.0 = Prerelease|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -415,6 +441,7 @@ Global {00A1FED2-2254-4AF7-A5DB-2357FA7C88CD} = {26E8B310-269E-46D4-A612-24601F16065F} {A9348433-58C1-4B9C-8BB7-088B02529D9D} = {A2AE33B4-0830-426A-9E11-951DAB12BE5B} {9512295B-3045-40E0-9B7E-2409F2173E9D} = {A2AE33B4-0830-426A-9E11-951DAB12BE5B} + {259824F3-D860-4233-91D6-FF73D4DD8B18} = {26E8B310-269E-46D4-A612-24601F16065F} {6CBF412C-EFEE-45F7-80FD-AC402C22CDB9} = {8C8F3945-0AEF-4949-A1F7-B18E952E50BC} {5C2BE2A8-EA1D-424F-88E1-7FC33EEC2E55} = {6CBF412C-EFEE-45F7-80FD-AC402C22CDB9} {AB83BAC0-C539-424A-BF00-78487C10753C} = {6CBF412C-EFEE-45F7-80FD-AC402C22CDB9} diff --git a/Plugins/Mute/Commands/MuteCommand.cs b/Plugins/Mute/Commands/MuteCommand.cs new file mode 100644 index 000000000..04047317a --- /dev/null +++ b/Plugins/Mute/Commands/MuteCommand.cs @@ -0,0 +1,42 @@ +using Data.Models.Client; +using SharedLibraryCore; +using SharedLibraryCore.Commands; +using SharedLibraryCore.Configuration; +using SharedLibraryCore.Interfaces; + +namespace Mute.Commands; + +public class MuteCommand : Command +{ + public MuteCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, + translationLookup) + { + Name = "mute"; + Description = translationLookup["PLUGINS_MUTE_COMMANDS_MUTE_DESC"]; + Alias = "mu"; + Permission = EFClient.Permission.Moderator; + RequiresTarget = true; + SupportedGames = Plugin.SupportedGames; + Arguments = new[] + { + new CommandArgument + { + Name = translationLookup["COMMANDS_ARGS_PLAYER"], + Required = true + } + }; + } + + private readonly MuteManager _muteManager = new(); + + public override async Task ExecuteAsync(GameEvent gameEvent) + { + if (await _muteManager.Mute(gameEvent)) + { + gameEvent.Origin.Tell($"{_translationLookup["PLUGINS_MUTE_MUTED"]} {gameEvent.Target.Name}"); + return; + } + + gameEvent.Origin.Tell($"{_translationLookup["PLUGINS_MUTE_UNMUTED"]} {gameEvent.Target.Name}"); + } +} diff --git a/Plugins/Mute/DataManager.cs b/Plugins/Mute/DataManager.cs new file mode 100644 index 000000000..593707d4e --- /dev/null +++ b/Plugins/Mute/DataManager.cs @@ -0,0 +1,38 @@ +using Data.Models.Client; +using SharedLibraryCore.Interfaces; +using static System.Enum; + +namespace Mute; + +public class DataManager +{ + public DataManager(IMetaServiceV2 metaService) + { + _metaService = metaService; + } + + private readonly IMetaServiceV2 _metaService; + + public async Task ReadPersistentData(EFClient client) + { + var clientMuteState = client.GetAdditionalProperty(Plugin.MuteKey) ?? + Parse((await _metaService.GetPersistentMeta(Plugin.MuteKey, client.ClientId))? + .Value ?? nameof(MuteState.Unmuted)); + + client.SetAdditionalProperty(Plugin.MuteKey, clientMuteState); + return clientMuteState; + } + + public async Task WritePersistentData(EFClient client, MuteState state) + { + await _metaService.SetPersistentMeta(Plugin.MuteKey, state.ToString(), client.ClientId); + client.SetAdditionalProperty(Plugin.MuteKey, state); + } +} + +public enum MuteState +{ + Muted, + Unmuting, + Unmuted +} diff --git a/Plugins/Mute/Mute.csproj b/Plugins/Mute/Mute.csproj new file mode 100644 index 000000000..0889877e4 --- /dev/null +++ b/Plugins/Mute/Mute.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + MrAmos123 + Library + Debug;Release;Prerelease + AnyCPU + + + + + + + + + + diff --git a/Plugins/Mute/MuteManager.cs b/Plugins/Mute/MuteManager.cs new file mode 100644 index 000000000..12252626b --- /dev/null +++ b/Plugins/Mute/MuteManager.cs @@ -0,0 +1,21 @@ +using SharedLibraryCore; + +namespace Mute; + +public class MuteManager +{ + public async Task Mute(GameEvent gameEvent) + { + if (await Plugin.DataManager.ReadPersistentData(gameEvent.Target) == MuteState.Muted) + { + await gameEvent.Owner.ExecuteCommandAsync($"unmute {gameEvent.Target.ClientNumber}"); + await Plugin.DataManager.WritePersistentData(gameEvent.Target, + gameEvent.Target.IsIngame ? MuteState.Unmuted : MuteState.Unmuting); + return false; + } + + await gameEvent.Owner.ExecuteCommandAsync($"muteClient {gameEvent.Target.ClientNumber}"); + await Plugin.DataManager.WritePersistentData(gameEvent.Target, MuteState.Muted); + return true; + } +} diff --git a/Plugins/Mute/Plugin.cs b/Plugins/Mute/Plugin.cs new file mode 100644 index 000000000..8db8e0d14 --- /dev/null +++ b/Plugins/Mute/Plugin.cs @@ -0,0 +1,60 @@ +using SharedLibraryCore; +using SharedLibraryCore.Interfaces; + +namespace Mute; + +public class Plugin : IPlugin +{ + public Plugin(IMetaServiceV2 metaService) + { + DataManager = new DataManager(metaService); + } + + public string Name => "Mute"; + public float Version => (float) Utilities.GetVersionAsDouble(); + public string Author => "Amos"; + + public static string MuteKey = "IW4MMute"; + + public static DataManager DataManager; + public static readonly Server.Game[] SupportedGames = {Server.Game.IW4}; + + public async Task OnEventAsync(GameEvent gameEvent, Server server) + { + if (!SupportedGames.Contains(server.GameName)) return; + + switch (gameEvent.Type) + { + case GameEvent.EventType.Join: + switch (await DataManager.ReadPersistentData(gameEvent.Origin)) + { + case MuteState.Muted: + await server.ExecuteCommandAsync($"muteClient {gameEvent.Origin.ClientNumber}"); + break; + case MuteState.Unmuting: + await server.ExecuteCommandAsync($"unmute {gameEvent.Origin.ClientNumber}"); + await DataManager.WritePersistentData(gameEvent.Origin, MuteState.Unmuted); + break; + case MuteState.Unmuted: + break; + } + + break; + } + } + + public Task OnLoadAsync(IManager manager) + { + return Task.CompletedTask; + } + + public Task OnUnloadAsync() + { + return Task.CompletedTask; + } + + public Task OnTickAsync(Server server) + { + return Task.CompletedTask; + } +}