- fix interaction profile buttons not working - update remaining functions to pluginV2 - fix guest tag not being removed upon disconnecting from a donator server
359 lines
16 KiB
C#
359 lines
16 KiB
C#
using SharedLibraryCore;
|
|
using SharedLibraryCore.Interfaces;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using SharedLibraryCore.Database.Models;
|
|
using SharedLibraryCore.Helpers;
|
|
using SharedLibraryCore.Commands;
|
|
using SharedLibraryCore.Events.Management;
|
|
using SharedLibraryCore.Interfaces.Events;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Extensions.Logging;
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
using Data.Models;
|
|
using System;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
using System.Linq;
|
|
using Serilog.Core;
|
|
using EFClient = Data.Models.Client.EFClient;
|
|
using static IW4MAdmin.Plugins.ClanTagRankCommands.Events.Script;
|
|
using SharedLibraryCore.Events.Game;
|
|
using SharedLibraryCore.Events.Server;
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
|
|
namespace ClanTagRankCommands
|
|
{
|
|
|
|
public class Plugin : IPluginV2
|
|
{
|
|
private readonly ILogger _logger;
|
|
//private ClanTagConfiguration Config;
|
|
//readonly string rank = "rank";
|
|
//string rankName = "none";
|
|
private readonly IInteractionRegistration _interactionRegistration;
|
|
private static readonly string DonatorInteraction = "Webfront::Profile::Donator";
|
|
private static readonly string GuestInteraction = "Webfront::Profile::Guest";
|
|
private readonly IRemoteCommandService _remoteCommandService;
|
|
|
|
|
|
|
|
public string Name => "ClanTagRankCommands";
|
|
|
|
//public float Version => 1.5f;
|
|
|
|
public string Author => "INSANEMODE";
|
|
|
|
string IModularAssembly.Version => "2.0";
|
|
|
|
private readonly IMetaServiceV2 _metaService;
|
|
|
|
public static string DonatorKey = "IW4MDonator";
|
|
|
|
public static DataManager DataManager;
|
|
|
|
public Plugin(IMetaServiceV2 metaService, ClanTagConfiguration Config, ILogger<Plugin> logger, IInteractionRegistration interactionRegistration, IRemoteCommandService remoteCommandService)
|
|
{
|
|
|
|
IManagementEventSubscriptions.Load += OnLoad;
|
|
IManagementEventSubscriptions.Unload += OnUnloadAsync;
|
|
IGameEventSubscriptions.ScriptEventTriggered += OnScriptEvent;
|
|
|
|
_remoteCommandService = remoteCommandService;
|
|
_logger = logger;
|
|
_metaService = metaService;
|
|
_interactionRegistration = interactionRegistration;
|
|
DataManager = new DataManager(metaService, logger);
|
|
IManagementEventSubscriptions.ClientStateDisposed += async (clientEvent, token) =>
|
|
{
|
|
if ((clientEvent.Client.CurrentServer.Hostname.Contains("Donator") || clientEvent.Client.CurrentServer.Hostname.Contains("Test")) && clientEvent.Client.Tag == "Guest")
|
|
{
|
|
Console.WriteLine("Guest disconnected, removing guest tag.");
|
|
_logger.LogInformation("Guest disconnected, removing guest tag.");
|
|
clientEvent.Client.Tag = null;
|
|
await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, clientEvent.Client.ClientId,
|
|
token);
|
|
}
|
|
};
|
|
}
|
|
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
|
{
|
|
serviceCollection.AddConfiguration<ClanTagConfiguration>("ClanTagRankCommands");
|
|
|
|
}
|
|
private Task OnLoad(IManager manager, CancellationToken token)
|
|
{
|
|
|
|
string version = manager.Version;
|
|
string str = string.Format("Loaded {0} ({1}) by {2} in {3} ({4})!", (object)((IPluginV2)this).Name, (object)((IPluginV2)this).Version, (object)((IPluginV2)this).Author, (object)"IW4MAdmin", (object)version);
|
|
_logger.LogInformation(str);
|
|
_interactionRegistration.RegisterInteraction(DonatorInteraction, async (clientId, game, token) =>
|
|
{
|
|
await Task.Delay(1);
|
|
_logger.LogDebug($"Donator button interaction for {clientId.Value}");
|
|
if (!clientId.HasValue)
|
|
{
|
|
_logger.LogDebug($"No client id provided");
|
|
return null;
|
|
}
|
|
//var client = manager.GetClientService().Get(clientId.Value).Result;
|
|
var DonatorState = await DataManager.ReadPersistentData(clientId.Value, manager);
|
|
_logger.LogDebug($"DonatorState: {(int)DonatorState}");
|
|
return DonatorState is DonatorState.User
|
|
? new InteractionData
|
|
{
|
|
EntityId = clientId.Value,
|
|
Name = "Set Donator",
|
|
DisplayMeta = "oi-circle-check",
|
|
ActionPath = "DynamicAction",
|
|
ActionMeta = new()
|
|
{
|
|
{ "InteractionId", "command" },
|
|
{ "Data", $"Donator" },
|
|
{ "ActionButtonLabel", "Confirm" },
|
|
{ "Name", "Set Donator rank" },
|
|
{ "ShouldRefresh", true.ToString() }
|
|
},
|
|
MinimumPermission = EFClient.Permission.Administrator,
|
|
Source = Name,
|
|
Enabled = true,
|
|
PermissionAccess = "read",
|
|
PermissionEntity = "Interaction",
|
|
Description = "Set Donator rank for client"
|
|
}
|
|
: new InteractionData
|
|
{
|
|
EntityId = clientId.Value,
|
|
Name = "UnSet Donator",
|
|
DisplayMeta = "oi-circle-x",
|
|
ActionPath = "DynamicAction",
|
|
ActionMeta = new()
|
|
{
|
|
{ "InteractionId", "command" },
|
|
{ "Data", $"Donator" },
|
|
{ "ActionButtonLabel", "Confirm" },
|
|
{ "Name", "UnSet Donator rank" },
|
|
{ "ShouldRefresh", true.ToString() }
|
|
},
|
|
MinimumPermission = EFClient.Permission.Administrator,
|
|
Source = Name,
|
|
Enabled = true,
|
|
PermissionAccess = "read",
|
|
PermissionEntity = "Interaction",
|
|
Description = "UnSet Donator rank for client"
|
|
};
|
|
|
|
});
|
|
_logger.LogDebug("Registered Donator Interaction");
|
|
_interactionRegistration.RegisterInteraction(GuestInteraction, async (clientId, game, token) =>
|
|
{
|
|
await Task.Delay(1);
|
|
_logger.LogDebug($"guest button interaction for {clientId.Value}");
|
|
if (!clientId.HasValue)
|
|
{
|
|
_logger.LogDebug($"No client id provided");
|
|
return null;
|
|
}
|
|
bool guest;
|
|
var clienttag = await _metaService.GetPersistentMetaByLookup("ClientTagV2", "ClientTagNameV2", (int)clientId,
|
|
token);
|
|
if(clienttag is not null && clienttag.Value.Contains("Guest"))
|
|
{
|
|
guest = false;
|
|
}
|
|
else if(clienttag is not null && !clienttag.Value.Contains("Guest"))
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
guest = true;
|
|
}
|
|
return guest
|
|
? new InteractionData
|
|
{
|
|
EntityId = clientId.Value,
|
|
Name = "Invite Guest",
|
|
DisplayMeta = "oi-people",
|
|
ActionPath = "DynamicAction",
|
|
ActionMeta = new()
|
|
{
|
|
{ "InteractionId", "command" },
|
|
{ "Data", $"Guest" },
|
|
{ "ActionButtonLabel", "Confirm" },
|
|
{ "Name", "Temporarily invite guest to donator server" },
|
|
{ "ShouldRefresh", true.ToString() }
|
|
},
|
|
MinimumPermission = EFClient.Permission.Trusted,
|
|
Source = Name,
|
|
Enabled = true,
|
|
PermissionAccess = "read",
|
|
PermissionEntity = "Interaction",
|
|
Description = "Temporarily invite a guest, until they leave the server"
|
|
}
|
|
: new InteractionData
|
|
{
|
|
EntityId = clientId.Value,
|
|
Name = "Cancel Guest Invite",
|
|
DisplayMeta = "oi-circle-x",
|
|
ActionPath = "DynamicAction",
|
|
ActionMeta = new()
|
|
{
|
|
{ "InteractionId", "command" },
|
|
{ "Data", $"Guest" },
|
|
{ "ActionButtonLabel", "Confirm" },
|
|
{ "Name", "Cancel guest invite" },
|
|
{ "ShouldRefresh", true.ToString() }
|
|
},
|
|
MinimumPermission = EFClient.Permission.Trusted,
|
|
Source = Name,
|
|
Enabled = true,
|
|
PermissionAccess = "read",
|
|
PermissionEntity = "Interaction",
|
|
Description = "Cancel a guest invite"
|
|
};
|
|
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private Task OnScriptEvent(GameScriptEvent scriptEvent, CancellationToken token)
|
|
{
|
|
if (scriptEvent is not ScriptBanEvent ScriptBan)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] lineSplit = ScriptBan.ScriptData.Split(";");
|
|
var sender = Utilities.IW4MAdminClient(ScriptBan.Owner);
|
|
sender.AdministeredPenalties = new List<EFPenalty>
|
|
{
|
|
new()
|
|
{
|
|
|
|
AutomatedOffense = $"Profanity Filter Match: {lineSplit[4]} - {string.Join(",", lineSplit[5])}"
|
|
}
|
|
};
|
|
SharedLibraryCore.Database.Models.EFClient efClient = (SharedLibraryCore.Database.Models.EFClient)null;
|
|
string rule = ScriptBan.Owner.Manager.GetApplicationSettings().Configuration().GlobalRules.Where(rule => rule.Contains("hate")).FirstOrDefault().ToString();
|
|
efClient = ScriptBan.Owner.GetClientsAsList().First<SharedLibraryCore.Database.Models.EFClient>((Func<SharedLibraryCore.Database.Models.EFClient, bool>)(_client => _client.NetworkId == ScriptBan.Target.NetworkId));
|
|
efClient.Ban($"Profanity filter Racism match - -{rule}", sender, false);
|
|
|
|
//ScriptBan.Owner.Ban("Blatent racism match", ScriptBan.Target., sender);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Could not parse ScriptBan Event: {Data}", e.Data);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
//public async Task OnDisconnect(GameEvent E, Server S)// => Task.CompletedTask;
|
|
//{
|
|
// if (E.Type == GameEventV2.EventType.Disconnect && (S.Hostname.Contains("Donator") || S.Hostname.Contains("Test")) && E.Origin.Tag == "Guest")
|
|
// {
|
|
// Console.WriteLine("Guest disconnected, removing guest tag.");
|
|
// _logger.LogInformation("Guest disconnected, removing guest tag.");
|
|
// E.Origin.Tag = null;
|
|
// await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, E.Origin.ClientId,
|
|
// E.Owner.Manager.CancellationToken);
|
|
// }
|
|
// else if (E.Type == GameEventV2.EventType.Other && (S.Hostname.Contains("Donator") || S.Hostname.Contains("Test")) && E.Origin.Tag == "Guest")
|
|
// {
|
|
// Console.WriteLine("Guest triggered event of type Other, removing guest tag.");
|
|
// _logger.LogInformation("Guest triggered event of type Other, removing guest tag.");
|
|
// E.Origin.Tag = null;
|
|
// await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, E.Origin.ClientId,
|
|
// E.Owner.Manager.CancellationToken);
|
|
// }
|
|
|
|
|
|
//}
|
|
//public async Task OnEventAsync(GameEventV2 E, Server S)// => Task.CompletedTask;
|
|
//{
|
|
// if (E.Type == GameEventV2.EventType.Disconnect && (S.Hostname.Contains("Donator") || S.Hostname.Contains("Test")) && E.Origin.Tag == "Guest")
|
|
// {
|
|
// Console.WriteLine("Guest disconnected, removing guest tag.");
|
|
// _logger.LogInformation("Guest disconnected, removing guest tag.");
|
|
// E.Origin.Tag = null;
|
|
// await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, E.Origin.ClientId,
|
|
// E.Owner.Manager.CancellationToken);
|
|
// }
|
|
// else if (E.Type == GameEventV2.EventType.Other && (S.Hostname.Contains("Donator") || S.Hostname.Contains("Test")) && E.Origin.Tag == "Guest")
|
|
// {
|
|
// Console.WriteLine("Guest triggered event of type Other, removing guest tag.");
|
|
// _logger.LogInformation("Guest triggered event of type Other, removing guest tag.");
|
|
// E.Origin.Tag = null;
|
|
// await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, E.Origin.ClientId,
|
|
// E.Owner.Manager.CancellationToken);
|
|
// }
|
|
|
|
|
|
//}
|
|
//public Task OnTickAsync(Server S)// =>
|
|
//{
|
|
|
|
// return Task.CompletedTask;
|
|
|
|
//}
|
|
|
|
private Task OnUnloadAsync(IManager manager, CancellationToken token) //=>
|
|
{
|
|
|
|
_interactionRegistration.UnregisterInteraction(DonatorInteraction);
|
|
_interactionRegistration.UnregisterInteraction(GuestInteraction);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
}
|
|
public static class yadb
|
|
{
|
|
public static Task notifyYADB(string message, string title, string reason, string icon, Server S, SharedLibraryCore.Database.Models.EFClient OriginClient, SharedLibraryCore.Database.Models.EFClient TargetClient)
|
|
{
|
|
string json = "";
|
|
using (var stream = new System.IO.MemoryStream())
|
|
{
|
|
using (var writer = new System.Text.Json.Utf8JsonWriter(stream))
|
|
{
|
|
writer.WriteStartObject();
|
|
writer.WriteString("title", title);
|
|
writer.WriteString("message", message);
|
|
writer.WriteString("reason", reason);
|
|
writer.WriteString("icon", icon);
|
|
writer.WriteEndObject();
|
|
}
|
|
|
|
json = System.Text.Encoding.UTF8.GetString(stream.ToArray());
|
|
}
|
|
var e = new GameEvent()
|
|
{
|
|
Origin = OriginClient,
|
|
Target = TargetClient,
|
|
Owner = S,
|
|
Type = GameEvent.EventType.Other,
|
|
Subtype = "YADB-Embed",
|
|
Extra = json
|
|
};
|
|
|
|
S.ExecuteEvent(e);
|
|
Console.WriteLine("event triggered!" + message);
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
//notifyYADB("Kicked player @{target} after a successful vote started from @{origin}", $"Vote kicked {target.Name}", gameEvent.Data,":pencil:", Server, origin, target);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|