IW4M-Admin/Plugins/LiveRadar/Plugin.cs

146 lines
4.7 KiB
C#
Raw Normal View History

2023-02-11 21:46:08 -05:00
using SharedLibraryCore;
2019-06-30 14:37:59 -04:00
using System;
using System.Collections.Generic;
using System.Globalization;
2019-06-30 14:37:59 -04:00
using System.Linq;
2023-02-11 21:46:08 -05:00
using System.Threading;
2019-06-30 14:37:59 -04:00
using System.Threading.Tasks;
2023-02-11 21:46:08 -05:00
using Data.Models;
using IW4MAdmin.Plugins.LiveRadar.Configuration;
using IW4MAdmin.Plugins.LiveRadar.Events;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharedLibraryCore.Configuration;
2023-02-11 21:46:08 -05:00
using SharedLibraryCore.Events.Game;
using SharedLibraryCore.Events.Server;
using SharedLibraryCore.Interfaces;
2023-02-11 21:46:08 -05:00
using SharedLibraryCore.Interfaces.Events;
using ILogger = Microsoft.Extensions.Logging.ILogger;
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
namespace IW4MAdmin.Plugins.LiveRadar;
public class Plugin : IPluginV2
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
public string Name => "Live Radar";
public string Version => Utilities.GetVersionAsString();
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
public string Author => "RaidMax";
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
private bool _addedPage;
private readonly Dictionary<string, long> _botGuidLookups;
private readonly object _lockObject = new();
private readonly ILogger _logger;
private readonly ApplicationConfiguration _appConfig;
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
serviceCollection.AddConfiguration<LiveRadarConfiguration>();
}
2023-02-11 21:46:08 -05:00
public Plugin(ILogger<Plugin> logger, ApplicationConfiguration appConfig)
{
_botGuidLookups = new Dictionary<string, long>();
_logger = logger;
_appConfig = appConfig;
IGameServerEventSubscriptions.MonitoringStarted += OnMonitoringStarted;
IGameEventSubscriptions.ClientEnteredMatch += OnClientEnteredMatch;
IGameEventSubscriptions.ScriptEventTriggered += OnScriptEvent;
}
private Task OnScriptEvent(GameScriptEvent scriptEvent, CancellationToken token)
{
if (scriptEvent is not LiveRadarEvent radarEvent)
{
2023-02-11 21:46:08 -05:00
return Task.CompletedTask;
}
2023-02-11 21:46:08 -05:00
try
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
var originalBotGuid = radarEvent.ScriptData.Split(";")[1];
if (originalBotGuid.IsBotGuid() && _appConfig.IgnoreBots)
{
2023-02-11 21:46:08 -05:00
return Task.CompletedTask;
}
2023-02-11 21:46:08 -05:00
var botKey = $"BotGuid_{originalBotGuid}";
long generatedBotGuid;
2023-02-11 21:46:08 -05:00
lock (_lockObject)
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
var hasBotKey = _botGuidLookups.ContainsKey(botKey);
if (!hasBotKey && originalBotGuid.IsBotGuid())
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
// edge case where the bot guid has not been registered yet
return Task.CompletedTask;
}
2023-02-11 21:46:08 -05:00
generatedBotGuid = hasBotKey
? _botGuidLookups[botKey]
: (originalBotGuid ?? "0").ConvertGuidToLong(NumberStyles.HexNumber);
}
2023-02-11 21:46:08 -05:00
var radarUpdate = RadarEvent.Parse(scriptEvent.ScriptData, generatedBotGuid);
var client =
radarEvent.Owner.ConnectedClients.FirstOrDefault(client => client.NetworkId == radarUpdate.Guid);
2023-02-11 21:46:08 -05:00
if (client != null)
{
radarUpdate.Name = client.Name.StripColors();
client.SetAdditionalProperty("LiveRadar", radarUpdate);
2019-06-30 14:37:59 -04:00
}
2023-02-11 21:46:08 -05:00
}
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
catch (Exception e)
{
_logger.LogError(e, "Could not parse live radar output: {Data}", e.Data);
}
return Task.CompletedTask;
}
private Task OnClientEnteredMatch(ClientEnterMatchEvent clientEvent, CancellationToken token)
{
if (!clientEvent.Client.IsBot)
{
2019-06-30 14:37:59 -04:00
return Task.CompletedTask;
}
2023-02-11 21:46:08 -05:00
var botKey = $"BotGuid_{clientEvent.ClientNetworkId}";
lock (_lockObject)
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
if (!_botGuidLookups.ContainsKey(botKey))
2019-07-05 21:53:03 -04:00
{
2023-02-11 21:46:08 -05:00
_botGuidLookups.Add(botKey, clientEvent.Client.NetworkId);
2019-07-05 21:53:03 -04:00
}
2019-06-30 14:37:59 -04:00
}
2023-02-11 21:46:08 -05:00
return Task.CompletedTask;
}
2019-06-30 14:37:59 -04:00
2023-02-11 21:46:08 -05:00
private Task OnMonitoringStarted(MonitorStartEvent monitorEvent, CancellationToken token)
{
lock (_lockObject)
2019-06-30 14:37:59 -04:00
{
2023-02-11 21:46:08 -05:00
// if it's an IW4 game, with custom callbacks, we want to
// enable the live radar page
var shouldRegisterPage = monitorEvent.Server.GameCode != Reference.Game.IW4 ||
!monitorEvent.Server.IsLegacyGameIntegrationEnabled ||
_addedPage;
if (shouldRegisterPage)
{
return Task.CompletedTask;
}
(monitorEvent.Source as IManager)?.GetPageList().Pages
.Add(Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"], "/Radar");
_addedPage = true;
2019-06-30 14:37:59 -04:00
}
2023-02-11 21:46:08 -05:00
return Task.CompletedTask;
2019-06-30 14:37:59 -04:00
}
}