From b992f4d910d9afb7b58087049e0d78af27c30898 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Fri, 11 Oct 2019 15:26:13 -0500 Subject: [PATCH] add unlink command fix parsing names with colors codes enabled --- Application/ApplicationManager.cs | 1 + .../Commands/CommandProcessing.cs | 3 ++- .../Commands/UnlinkClientCommand.cs | 18 +++++++++++++ SharedLibraryCore/Server.cs | 2 ++ SharedLibraryCore/Services/ClientService.cs | 25 +++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 SharedLibraryCore/Commands/UnlinkClientCommand.cs diff --git a/Application/ApplicationManager.cs b/Application/ApplicationManager.cs index 3f527b1f6..c4ff88576 100644 --- a/Application/ApplicationManager.cs +++ b/Application/ApplicationManager.cs @@ -420,6 +420,7 @@ namespace IW4MAdmin.Application Commands.Add(new CSetGravatar()); Commands.Add(new CNextMap()); Commands.Add(new RequestTokenCommand()); + Commands.Add(new UnlinkClientCommand()); foreach (Command C in SharedLibraryCore.Plugins.PluginImporter.ActiveCommands) { diff --git a/SharedLibraryCore/Commands/CommandProcessing.cs b/SharedLibraryCore/Commands/CommandProcessing.cs index c9acd25f5..5a3c13a98 100644 --- a/SharedLibraryCore/Commands/CommandProcessing.cs +++ b/SharedLibraryCore/Commands/CommandProcessing.cs @@ -86,12 +86,13 @@ namespace SharedLibraryCore.Commands if (E.Target == null && C.RequiresTarget) // Find active player including quotes (multiple words) { - matchingPlayers = E.Owner.GetClientByName(E.Data.Trim()); + matchingPlayers = E.Owner.GetClientByName(E.Data); if (matchingPlayers.Count > 1) { E.Origin.Tell(loc["COMMAND_TARGET_MULTI"]); throw new CommandException($"{E.Origin} had multiple players found for {C.Name}"); } + else if (matchingPlayers.Count == 1) { E.Target = matchingPlayers.First(); diff --git a/SharedLibraryCore/Commands/UnlinkClientCommand.cs b/SharedLibraryCore/Commands/UnlinkClientCommand.cs new file mode 100644 index 000000000..abc7eb984 --- /dev/null +++ b/SharedLibraryCore/Commands/UnlinkClientCommand.cs @@ -0,0 +1,18 @@ +using SharedLibraryCore.Database.Models; +using System.Threading.Tasks; + +namespace SharedLibraryCore.Commands +{ + public class UnlinkClientCommand : Command + { + public UnlinkClientCommand() : + base("unlinkclient", Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_UNLINK_CLIENT_DESC"], "uc", EFClient.Permission.Administrator, true) + { } + + public override async Task ExecuteAsync(GameEvent E) + { + await E.Owner.Manager.GetClientService().UnlinkClient(E.Target.ClientId); + E.Origin.Tell(Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_UNLINK_CLIENT_SUCCESS"].FormatExt(E.Target)); + } + } +} diff --git a/SharedLibraryCore/Server.cs b/SharedLibraryCore/Server.cs index 10ad0942b..440291a86 100644 --- a/SharedLibraryCore/Server.cs +++ b/SharedLibraryCore/Server.cs @@ -89,6 +89,8 @@ namespace SharedLibraryCore return new List(); } + pName = pName.Trim().StripColors(); + string[] QuoteSplit = pName.Split('"'); bool literal = false; if (QuoteSplit.Length > 1) diff --git a/SharedLibraryCore/Services/ClientService.cs b/SharedLibraryCore/Services/ClientService.cs index 175da7ea7..f357ef2ad 100644 --- a/SharedLibraryCore/Services/ClientService.cs +++ b/SharedLibraryCore/Services/ClientService.cs @@ -638,5 +638,30 @@ namespace SharedLibraryCore.Services .AnyAsync(); } } + + /// + /// Unlinks shared GUID account into its own separate account + /// + /// + /// + public async Task UnlinkClient(int clientId) + { + using (var ctx = new DatabaseContext()) + { + var newLink = new EFAliasLink() { Active = true }; + ctx.AliasLinks.Add(newLink); + await ctx.SaveChangesAsync(); + + var client = await ctx.Clients.Include(_client => _client.CurrentAlias) + .FirstAsync(_client => _client.ClientId == clientId); + client.AliasLinkId = newLink.AliasLinkId; + client.Level = Permission.User; + + await ctx.Aliases.Where(_alias => _alias.IPAddress == client.IPAddress) + .ForEachAsync(_alias => _alias.LinkId = newLink.AliasLinkId); + + await ctx.SaveChangesAsync(); + } + } } }