266 lines
11 KiB
C#
266 lines
11 KiB
C#
using SharedLibraryCore;
|
|
using SharedLibraryCore.Interfaces;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using SharedLibraryCore.Database.Models;
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
using System.Collections.Generic;
|
|
using Microsoft.Extensions.Logging;
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
using Data.Models;
|
|
using System;
|
|
using SharedLibraryCore.Commands;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
using System.Linq;
|
|
using Serilog.Core;
|
|
|
|
namespace ClanTagRankCommands
|
|
{
|
|
|
|
public class Plugin : IPlugin
|
|
{
|
|
private readonly IConfigurationHandler<Configuration> _configurationHandler;
|
|
private readonly ILogger _logger;
|
|
private Configuration 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";
|
|
|
|
private readonly IMetaServiceV2 _metaService;
|
|
|
|
public static string DonatorKey = "IW4MDonator";
|
|
|
|
public static DataManager DataManager;
|
|
|
|
public Plugin(IMetaServiceV2 metaService, IConfigurationHandlerFactory configurationHandlerFactory, ILogger<Plugin> logger, IInteractionRegistration interactionRegistration, IRemoteCommandService remoteCommandService)
|
|
{
|
|
_remoteCommandService = remoteCommandService;
|
|
_logger = logger;
|
|
_metaService = metaService;
|
|
_configurationHandler = (IConfigurationHandler<Configuration>)configurationHandlerFactory.GetConfigurationHandler<Configuration>("ClanTagRankCommands");
|
|
_interactionRegistration = interactionRegistration;
|
|
DataManager = new DataManager(metaService, logger);
|
|
}
|
|
public Task OnLoadAsync(IManager manager)// => Task.CompletedTask;
|
|
{
|
|
if (_configurationHandler.Configuration() == null)
|
|
{
|
|
_configurationHandler.Set((Configuration)new Configuration().Generate());
|
|
_configurationHandler.Save();
|
|
}
|
|
Config = _configurationHandler.Configuration();
|
|
string version = manager.Version;
|
|
string str = string.Format("Loaded {0} ({1}) by {2} in {3} ({4})!", (object)((IPlugin)this).Name, (object)((IPlugin)this).Version, (object)((IPlugin)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 = Data.Models.Client.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 = Data.Models.Client.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 = Data.Models.Client.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 = Data.Models.Client.EFClient.Permission.Trusted,
|
|
Source = Name,
|
|
Enabled = true,
|
|
PermissionAccess = "read",
|
|
PermissionEntity = "Interaction",
|
|
Description = "Cancel a guest invite"
|
|
};
|
|
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
public async Task OnEventAsync(GameEvent E, Server S)// => Task.CompletedTask;
|
|
{
|
|
if (E.Type == GameEvent.EventType.Disconnect && (S.Hostname.Contains("Donator") || S.Hostname.Contains("Test")) && E.Origin.Tag == "Guest")
|
|
{
|
|
E.Origin.Tag = null;
|
|
await _metaService.RemovePersistentMeta(EFMeta.ClientTagV2, E.Origin.ClientId,
|
|
E.Owner.Manager.CancellationToken);
|
|
}
|
|
|
|
}
|
|
public Task OnTickAsync(Server S)// =>
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
public Task OnUnloadAsync() //=>
|
|
{
|
|
|
|
_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, EFClient OriginClient, 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);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|