From ba9e393363b89623467803944bbce70290b6c253 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sat, 11 Feb 2023 20:46:08 -0600 Subject: [PATCH] update live radar plugin to IPluginV2 --- .../Configuration/LiveRadarConfiguration.cs | 705 +++++++++--------- .../LiveRadar/Controllers/RadarController.cs | 24 +- Plugins/LiveRadar/Events/LiveRadarEvent.cs | 38 + Plugins/LiveRadar/Events/Script.cs | 33 - Plugins/LiveRadar/LiveRadar.csproj | 2 +- Plugins/LiveRadar/MapInfo.cs | 49 +- Plugins/LiveRadar/Plugin.cs | 223 +++--- Plugins/LiveRadar/RadarEvent.cs | 95 ++- 8 files changed, 586 insertions(+), 583 deletions(-) create mode 100644 Plugins/LiveRadar/Events/LiveRadarEvent.cs delete mode 100644 Plugins/LiveRadar/Events/Script.cs diff --git a/Plugins/LiveRadar/Configuration/LiveRadarConfiguration.cs b/Plugins/LiveRadar/Configuration/LiveRadarConfiguration.cs index a9643c24a..bba535877 100644 --- a/Plugins/LiveRadar/Configuration/LiveRadarConfiguration.cs +++ b/Plugins/LiveRadar/Configuration/LiveRadarConfiguration.cs @@ -1,391 +1,390 @@ using SharedLibraryCore.Interfaces; using System.Collections.Generic; -namespace LiveRadar.Configuration +namespace IW4MAdmin.Plugins.LiveRadar.Configuration; + +public class LiveRadarConfiguration : IBaseConfiguration { - class LiveRadarConfiguration : IBaseConfiguration + public List Maps { get; set; } + + public IBaseConfiguration Generate() { - public List Maps { get; set; } - - public IBaseConfiguration Generate() + Maps = new List() { - Maps = new List() + new MapInfo() { - new MapInfo() - { - Name = "mp_afghan", - MaxLeft = 4600, // ymax - MaxRight = -1100, // ymin - MaxBottom = -1400, // xmin - MaxTop = 4600, // xmax - Left = 52, // pxmin - Right = 898, // pxmax - Bottom = 930, // pymax - Top = 44 // pymin - }, + Name = "mp_afghan", + MaxLeft = 4600, // ymax + MaxRight = -1100, // ymin + MaxBottom = -1400, // xmin + MaxTop = 4600, // xmax + Left = 52, // pxmin + Right = 898, // pxmax + Bottom = 930, // pymax + Top = 44 // pymin + }, - new MapInfo() - { - Name = "mp_rust", - Top = 212, - Bottom = 812, - Left = 314, - Right = 856, - MaxRight = -225, - MaxLeft = 1809, - MaxTop = 1773, - MaxBottom = -469 - }, + new MapInfo() + { + Name = "mp_rust", + Top = 212, + Bottom = 812, + Left = 314, + Right = 856, + MaxRight = -225, + MaxLeft = 1809, + MaxTop = 1773, + MaxBottom = -469 + }, - new MapInfo() - { - Name = "mp_terminal", - Top = 174, - Bottom = 846, - Left = 18, - Right = 1011, - MaxTop = 2929, - MaxBottom = -513, - MaxLeft = 7521, - MaxRight = 2447 - }, + new MapInfo() + { + Name = "mp_terminal", + Top = 174, + Bottom = 846, + Left = 18, + Right = 1011, + MaxTop = 2929, + MaxBottom = -513, + MaxLeft = 7521, + MaxRight = 2447 + }, - new MapInfo() - { - Name = "mp_subbase", - MaxLeft = 1841, - MaxRight = -3817, - MaxBottom = -1585, - MaxTop = 2593, - Left = 18, - Right = 968, - Bottom = 864, - Top = 160, - ViewPositionRotation = 180, - }, + new MapInfo() + { + Name = "mp_subbase", + MaxLeft = 1841, + MaxRight = -3817, + MaxBottom = -1585, + MaxTop = 2593, + Left = 18, + Right = 968, + Bottom = 864, + Top = 160, + ViewPositionRotation = 180, + }, - new MapInfo() - { - Name = "mp_estate", - Top = 52, - Bottom = 999, - Left = 173, - Right = 942, - MaxTop = 2103, - MaxBottom = -5077, - MaxLeft = 4437, - MaxRight = -1240, - Rotation = 143, - CenterX = -1440, - CenterY = 1920, - Scaler = 0.85f, - ViewPositionRotation = 180 - }, + new MapInfo() + { + Name = "mp_estate", + Top = 52, + Bottom = 999, + Left = 173, + Right = 942, + MaxTop = 2103, + MaxBottom = -5077, + MaxLeft = 4437, + MaxRight = -1240, + Rotation = 143, + CenterX = -1440, + CenterY = 1920, + Scaler = 0.85f, + ViewPositionRotation = 180 + }, - new MapInfo() - { - Name = "mp_highrise", - MaxBottom = -3909, - MaxTop = 1649, - MaxRight = 5111, - MaxLeft = 8906, - Left = 108, - Right = 722, - Top = 66, - Bottom = 974, - }, + new MapInfo() + { + Name = "mp_highrise", + MaxBottom = -3909, + MaxTop = 1649, + MaxRight = 5111, + MaxLeft = 8906, + Left = 108, + Right = 722, + Top = 66, + Bottom = 974, + }, - new MapInfo() - { - Name = "mp_quarry", - MaxBottom = -5905, - MaxTop = -1423, - MaxRight = -2095, - MaxLeft = 3217, - Left = 126, - Right = 968, - Top = 114, - Bottom = 824 - }, + new MapInfo() + { + Name = "mp_quarry", + MaxBottom = -5905, + MaxTop = -1423, + MaxRight = -2095, + MaxLeft = 3217, + Left = 126, + Right = 968, + Top = 114, + Bottom = 824 + }, - new MapInfo() - { - Name = "mp_boneyard", - MaxBottom = -1756, - MaxTop = 2345, - MaxRight = -715, - MaxLeft = 1664, - Left = 248, - Right = 728, - Top = 68, - Bottom = 897 - }, + new MapInfo() + { + Name = "mp_boneyard", + MaxBottom = -1756, + MaxTop = 2345, + MaxRight = -715, + MaxLeft = 1664, + Left = 248, + Right = 728, + Top = 68, + Bottom = 897 + }, - new MapInfo() - { - Name = "mp_brecourt", - MaxBottom = -3797, - MaxTop = 4240, - MaxRight = -3876, - MaxLeft = 2575, - Left = 240, - Right = 846, - Top = 180, - Bottom = 934 - }, + new MapInfo() + { + Name = "mp_brecourt", + MaxBottom = -3797, + MaxTop = 4240, + MaxRight = -3876, + MaxLeft = 2575, + Left = 240, + Right = 846, + Top = 180, + Bottom = 934 + }, - new MapInfo() - { - Name = "mp_checkpoint", - MaxBottom = -2273, - MaxTop = 2153, - MaxRight = -3457, - MaxLeft = 2329, - Left = 30, - Right = 1010, - Top = 136, - Bottom = 890 - }, + new MapInfo() + { + Name = "mp_checkpoint", + MaxBottom = -2273, + MaxTop = 2153, + MaxRight = -3457, + MaxLeft = 2329, + Left = 30, + Right = 1010, + Top = 136, + Bottom = 890 + }, - new MapInfo() - { - Name = "mp_derail", - MaxBottom = -2775, - MaxTop = 3886, - MaxRight = -3807, - MaxLeft = 4490, - Left = 130, - Right = 892, - Top = 210, - Bottom = 829 - }, + new MapInfo() + { + Name = "mp_derail", + MaxBottom = -2775, + MaxTop = 3886, + MaxRight = -3807, + MaxLeft = 4490, + Left = 130, + Right = 892, + Top = 210, + Bottom = 829 + }, - new MapInfo() - { - Name = "mp_favela", - MaxBottom = -2017, - MaxTop = 1769, - MaxRight = -1239, - MaxLeft = 2998, - Left = 120, - Right = 912, - Top = 174, - Bottom = 878 - }, + new MapInfo() + { + Name = "mp_favela", + MaxBottom = -2017, + MaxTop = 1769, + MaxRight = -1239, + MaxLeft = 2998, + Left = 120, + Right = 912, + Top = 174, + Bottom = 878 + }, - new MapInfo() - { - Name = "mp_invasion", - MaxBottom = -3673, - MaxTop = 2540, - MaxRight = -3835, - MaxLeft = 980, - Left = 20, - Right = 808, - Top = 0, - Bottom = 1006 - }, + new MapInfo() + { + Name = "mp_invasion", + MaxBottom = -3673, + MaxTop = 2540, + MaxRight = -3835, + MaxLeft = 980, + Left = 20, + Right = 808, + Top = 0, + Bottom = 1006 + }, - new MapInfo() - { - Name = "mp_nightshift", - MaxBottom = -2497, - MaxTop = 1977, - MaxRight = -2265, - MaxLeft = 945, - Left = 246, - Right = 826, - Top = 104, - Bottom = 916 - }, + new MapInfo() + { + Name = "mp_nightshift", + MaxBottom = -2497, + MaxTop = 1977, + MaxRight = -2265, + MaxLeft = 945, + Left = 246, + Right = 826, + Top = 104, + Bottom = 916 + }, - new MapInfo() - { - Name = "mp_rundown", - MaxBottom = -2304, - MaxTop = 3194, - MaxRight = -3558, - MaxLeft = 3361, - Left = 32, - Right = 1030, - Top = 96, - Bottom = 892 - }, + new MapInfo() + { + Name = "mp_rundown", + MaxBottom = -2304, + MaxTop = 3194, + MaxRight = -3558, + MaxLeft = 3361, + Left = 32, + Right = 1030, + Top = 96, + Bottom = 892 + }, - new MapInfo() - { - Name = "mp_underpass", - MaxBottom = -601, - MaxTop = 3761, - MaxRight = -1569, - MaxLeft = 3615, - Left = 42, - Right = 978, - Top = 157, - Bottom = 944 - }, + new MapInfo() + { + Name = "mp_underpass", + MaxBottom = -601, + MaxTop = 3761, + MaxRight = -1569, + MaxLeft = 3615, + Left = 42, + Right = 978, + Top = 157, + Bottom = 944 + }, - new MapInfo() - { - Name = "mp_abandon", - MaxBottom = -1290, - MaxTop = 3855, - MaxRight = -2907, - MaxLeft = 2723, - Left = 6, - Right = 1016, - Top = 32, - Bottom = 945 - }, + new MapInfo() + { + Name = "mp_abandon", + MaxBottom = -1290, + MaxTop = 3855, + MaxRight = -2907, + MaxLeft = 2723, + Left = 6, + Right = 1016, + Top = 32, + Bottom = 945 + }, - new MapInfo() - { - Name = "mp_compact", - MaxBottom = 0, - MaxTop = 4264, - MaxRight = -1552, - MaxLeft = 3344, - Left = 35, - Right = 1003, - Top = 94, - Bottom = 935 - }, + new MapInfo() + { + Name = "mp_compact", + MaxBottom = 0, + MaxTop = 4264, + MaxRight = -1552, + MaxLeft = 3344, + Left = 35, + Right = 1003, + Top = 94, + Bottom = 935 + }, - new MapInfo() - { - Name = "mp_complex", - MaxBottom = -2869, - MaxTop = 2867, - MaxRight = -4204, - MaxLeft = -1218, - Left = 282, - Right = 749, - Top = 48, - Bottom = 991 - }, + new MapInfo() + { + Name = "mp_complex", + MaxBottom = -2869, + MaxTop = 2867, + MaxRight = -4204, + MaxLeft = -1218, + Left = 282, + Right = 749, + Top = 48, + Bottom = 991 + }, - new MapInfo() - { - Name = "mp_crash", - MaxBottom = -953, - MaxTop = 1811, - MaxRight = -2129, - MaxLeft = 2277, - Left = 52, - Right = 1017, - Top = 201, - Bottom = 807 - }, + new MapInfo() + { + Name = "mp_crash", + MaxBottom = -953, + MaxTop = 1811, + MaxRight = -2129, + MaxLeft = 2277, + Left = 52, + Right = 1017, + Top = 201, + Bottom = 807 + }, - new MapInfo() - { - Name = "mp_fuel2", - MaxBottom = -2218, - MaxTop = 4324, - MaxRight = -3115, - MaxLeft = 3193, - Left = 39, - Right = 888, - Top = 24, - Bottom = 906 - }, + new MapInfo() + { + Name = "mp_fuel2", + MaxBottom = -2218, + MaxTop = 4324, + MaxRight = -3115, + MaxLeft = 3193, + Left = 39, + Right = 888, + Top = 24, + Bottom = 906 + }, - new MapInfo() - { - Name = "mp_overgrown", - MaxBottom = -2052, - MaxTop = 3236, - MaxRight = -5393, - MaxLeft = 808, - Left = 17, - Right = 1024, - Top = 0, - Bottom = 847 - }, + new MapInfo() + { + Name = "mp_overgrown", + MaxBottom = -2052, + MaxTop = 3236, + MaxRight = -5393, + MaxLeft = 808, + Left = 17, + Right = 1024, + Top = 0, + Bottom = 847 + }, - new MapInfo() - { - Name = "mp_storm", - MaxBottom = -2317, - MaxTop = 2537, - MaxRight = -2223, - MaxLeft = 2097, - Left = 79, - Right = 932, - Top = 20, - Bottom = 995 - }, + new MapInfo() + { + Name = "mp_storm", + MaxBottom = -2317, + MaxTop = 2537, + MaxRight = -2223, + MaxLeft = 2097, + Left = 79, + Right = 932, + Top = 20, + Bottom = 995 + }, - new MapInfo() - { - Name = "mp_strike", - MaxBottom = -2504, - MaxTop = 3359, - MaxRight = -3105, - MaxLeft = 2822, - Left = 40, - Right = 969, - Top = 36, - Bottom = 955 - }, + new MapInfo() + { + Name = "mp_strike", + MaxBottom = -2504, + MaxTop = 3359, + MaxRight = -3105, + MaxLeft = 2822, + Left = 40, + Right = 969, + Top = 36, + Bottom = 955 + }, - new MapInfo() - { - Name = "mp_trailerpark", - MaxBottom = -2709, - MaxTop = 2027, - MaxRight = -1719, - MaxLeft = 1666, - Left = 152, - Right = 785, - Top = 50, - Bottom = 931 - }, + new MapInfo() + { + Name = "mp_trailerpark", + MaxBottom = -2709, + MaxTop = 2027, + MaxRight = -1719, + MaxLeft = 1666, + Left = 152, + Right = 785, + Top = 50, + Bottom = 931 + }, - new MapInfo() - { - Name = "mp_vacant", - MaxBottom = -2089, - MaxTop = 1652, - MaxRight = -1393, - MaxLeft = 1789, - Left = 122, - Right = 909, - Top = 16, - Bottom = 951 - }, + new MapInfo() + { + Name = "mp_vacant", + MaxBottom = -2089, + MaxTop = 1652, + MaxRight = -1393, + MaxLeft = 1789, + Left = 122, + Right = 909, + Top = 16, + Bottom = 951 + }, - new MapInfo() - { - Name = "mp_nuked", - MaxLeft = 1211, - MaxRight = -557, - MaxBottom = -2110, - MaxTop = 2092, - Left = 340, - Right = 698, - Bottom = 930, - Top = 92 - }, + new MapInfo() + { + Name = "mp_nuked", + MaxLeft = 1211, + MaxRight = -557, + MaxBottom = -2110, + MaxTop = 2092, + Left = 340, + Right = 698, + Bottom = 930, + Top = 92 + }, - new MapInfo() - { - Name = "mp_killhouse", - MaxLeft = 4276, - MaxRight = 2973, - MaxBottom = -1164, - MaxTop = 1392, - Left = 319, - Right = 758, - Bottom = 937, - Top = 87 - } - }; + new MapInfo() + { + Name = "mp_killhouse", + MaxLeft = 4276, + MaxRight = 2973, + MaxBottom = -1164, + MaxTop = 1392, + Left = 319, + Right = 758, + Bottom = 937, + Top = 87 + } + }; - return this; - } - - public string Name() => "LiveRadar"; + return this; } + + public string Name() => "LiveRadar"; } diff --git a/Plugins/LiveRadar/Controllers/RadarController.cs b/Plugins/LiveRadar/Controllers/RadarController.cs index 6038b3d0c..b82805b7c 100644 --- a/Plugins/LiveRadar/Controllers/RadarController.cs +++ b/Plugins/LiveRadar/Controllers/RadarController.cs @@ -1,26 +1,24 @@ -using LiveRadar.Configuration; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using SharedLibraryCore; using SharedLibraryCore.Dtos; using SharedLibraryCore.Interfaces; using System.Linq; using System.Threading.Tasks; +using IW4MAdmin.Plugins.LiveRadar.Configuration; using Microsoft.AspNetCore.Http; -namespace LiveRadar.Web.Controllers +namespace IW4MAdmin.Plugins.LiveRadar.Web.Controllers { public class RadarController : BaseController { private readonly IManager _manager; - private static LiveRadarConfiguration _config; - private readonly IConfigurationHandler _configurationHandler; + private readonly LiveRadarConfiguration _config; - public RadarController(IManager manager, IConfigurationHandlerFactory configurationHandlerFactory) : + public RadarController(IManager manager, LiveRadarConfiguration config) : base(manager) { _manager = manager; - _configurationHandler = - configurationHandlerFactory.GetConfigurationHandler("LiveRadarConfiguration"); + _config = config; } [HttpGet] @@ -32,8 +30,8 @@ namespace LiveRadar.Web.Controllers .Select(server => new ServerInfo { Name = server.Hostname, - IPAddress = server.IP, - Port = server.Port + IPAddress = server.ListenAddress, + Port = server.ListenPort }); ViewBag.Title = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"]; @@ -56,12 +54,6 @@ namespace LiveRadar.Web.Controllers return NotFound(); } - if (_config == null) - { - await _configurationHandler.BuildAsync(); - _config = _configurationHandler.Configuration() ?? new LiveRadarConfiguration(); - } - var map = _config.Maps.FirstOrDefault(map => map.Name == server.CurrentMap.Name); if (map == null) diff --git a/Plugins/LiveRadar/Events/LiveRadarEvent.cs b/Plugins/LiveRadar/Events/LiveRadarEvent.cs new file mode 100644 index 000000000..7121e5829 --- /dev/null +++ b/Plugins/LiveRadar/Events/LiveRadarEvent.cs @@ -0,0 +1,38 @@ +using SharedLibraryCore; +using SharedLibraryCore.Database.Models; +using SharedLibraryCore.Interfaces; +using System.Collections.Generic; +using SharedLibraryCore.Events.Game; +using EventGeneratorCallback = System.ValueTuple>; + +namespace IW4MAdmin.Plugins.LiveRadar.Events; + +public class Script : IRegisterEvent +{ + private const string EventLiveRadar = "LiveRadar"; + + private EventGeneratorCallback LiveRadar() + { + return (EventLiveRadar, EventLiveRadar, (eventLine, _, _) => + { + var radarEvent = new LiveRadarEvent + { + Type = GameEvent.EventType.Other, + Subtype = EventLiveRadar, + Origin = new EFClient { NetworkId = 0 }, + ScriptData = eventLine + }; + return radarEvent; + } + ); + } + + public IEnumerable Events => new[] { LiveRadar() }; +} + +public class LiveRadarEvent : GameScriptEvent +{ +} diff --git a/Plugins/LiveRadar/Events/Script.cs b/Plugins/LiveRadar/Events/Script.cs deleted file mode 100644 index 649155bde..000000000 --- a/Plugins/LiveRadar/Events/Script.cs +++ /dev/null @@ -1,33 +0,0 @@ -using SharedLibraryCore; -using SharedLibraryCore.Database.Models; -using SharedLibraryCore.Interfaces; -using System.Collections.Generic; -using EventGeneratorCallback = System.ValueTuple>; - -namespace LiveRadar.Events -{ - public class Script : IRegisterEvent - { - private const string EVENT_LIVERADAR = "LiveRadar"; - private EventGeneratorCallback LiveRadar() - { - return (EVENT_LIVERADAR, EVENT_LIVERADAR, (string eventLine, IEventParserConfiguration config, GameEvent autoEvent) => - { - string[] lineSplit = eventLine.Split(";"); - - autoEvent.Type = GameEvent.EventType.Other; - autoEvent.Subtype = EVENT_LIVERADAR; - autoEvent.Origin = new EFClient() { NetworkId = 0 }; - autoEvent.Extra = lineSplit[1]; // guid - - return autoEvent; - } - ); - } - - public IEnumerable Events => new[] { LiveRadar() }; - } -} diff --git a/Plugins/LiveRadar/LiveRadar.csproj b/Plugins/LiveRadar/LiveRadar.csproj index 92f65fa61..68bc9b39f 100644 --- a/Plugins/LiveRadar/LiveRadar.csproj +++ b/Plugins/LiveRadar/LiveRadar.csproj @@ -16,7 +16,7 @@ - + diff --git a/Plugins/LiveRadar/MapInfo.cs b/Plugins/LiveRadar/MapInfo.cs index cb2899267..0ff6595d0 100644 --- a/Plugins/LiveRadar/MapInfo.cs +++ b/Plugins/LiveRadar/MapInfo.cs @@ -1,31 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace IW4MAdmin.Plugins.LiveRadar; -namespace LiveRadar +public class MapInfo { - public class MapInfo - { - public string Name { get; set; } - public string Alias { get; set; } - // distance from the edge of the minimap image - // to the "playable" area - public int Top { get; set; } - public int Bottom { get; set; } - public int Left { get; set; } - public int Right { get; set; } - // maximum coordinate values for the map - public int MaxTop { get; set; } - public int MaxBottom { get; set; } - public int MaxLeft { get; set; } - public int MaxRight { get; set; } - public float Rotation { get; set; } - public float ViewPositionRotation { get; set; } - public float CenterX { get; set; } - public float CenterY { get; set; } - public float Scaler { get; set; } = 1.0f; + public string Name { get; set; } + public string Alias { get; set; } + // distance from the edge of the minimap image + // to the "playable" area + public int Top { get; set; } + public int Bottom { get; set; } + public int Left { get; set; } + public int Right { get; set; } + // maximum coordinate values for the map + public int MaxTop { get; set; } + public int MaxBottom { get; set; } + public int MaxLeft { get; set; } + public int MaxRight { get; set; } + public float Rotation { get; set; } + public float ViewPositionRotation { get; set; } + public float CenterX { get; set; } + public float CenterY { get; set; } + public float Scaler { get; set; } = 1.0f; - public int Width => MaxLeft - MaxRight; - public int Height => MaxTop - MaxBottom; - } + public int Width => MaxLeft - MaxRight; + public int Height => MaxTop - MaxBottom; } diff --git a/Plugins/LiveRadar/Plugin.cs b/Plugins/LiveRadar/Plugin.cs index 78682fb13..467359b87 100644 --- a/Plugins/LiveRadar/Plugin.cs +++ b/Plugins/LiveRadar/Plugin.cs @@ -1,132 +1,145 @@ -using LiveRadar.Configuration; -using SharedLibraryCore; +using SharedLibraryCore; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Threading; using System.Threading.Tasks; +using Data.Models; +using IW4MAdmin.Plugins.LiveRadar.Configuration; +using IW4MAdmin.Plugins.LiveRadar.Events; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharedLibraryCore.Configuration; +using SharedLibraryCore.Events.Game; +using SharedLibraryCore.Events.Server; using SharedLibraryCore.Interfaces; +using SharedLibraryCore.Interfaces.Events; using ILogger = Microsoft.Extensions.Logging.ILogger; -namespace LiveRadar +namespace IW4MAdmin.Plugins.LiveRadar; + +public class Plugin : IPluginV2 { - public class Plugin : IPlugin + public string Name => "Live Radar"; + + public string Version => Utilities.GetVersionAsString(); + + public string Author => "RaidMax"; + + private bool _addedPage; + private readonly Dictionary _botGuidLookups; + private readonly object _lockObject = new(); + private readonly ILogger _logger; + private readonly ApplicationConfiguration _appConfig; + + public static void RegisterDependencies(IServiceCollection serviceCollection) { - public string Name => "Live Radar"; + serviceCollection.AddConfiguration(); + } - public float Version => (float)Utilities.GetVersionAsDouble(); + public Plugin(ILogger logger, ApplicationConfiguration appConfig) + { + _botGuidLookups = new Dictionary(); + _logger = logger; + _appConfig = appConfig; + + IGameServerEventSubscriptions.MonitoringStarted += OnMonitoringStarted; + IGameEventSubscriptions.ClientEnteredMatch += OnClientEnteredMatch; + IGameEventSubscriptions.ScriptEventTriggered += OnScriptEvent; + } - public string Author => "RaidMax"; - - private readonly IConfigurationHandler _configurationHandler; - private readonly Dictionary _botGuidLookups; - private bool addedPage; - private readonly object lockObject = new object(); - private readonly ILogger _logger; - private readonly ApplicationConfiguration _appConfig; - - public Plugin(ILogger logger, IConfigurationHandlerFactory configurationHandlerFactory, ApplicationConfiguration appConfig) + private Task OnScriptEvent(GameScriptEvent scriptEvent, CancellationToken token) + { + if (scriptEvent is not LiveRadarEvent radarEvent) { - _configurationHandler = configurationHandlerFactory.GetConfigurationHandler("LiveRadarConfiguration"); - _botGuidLookups = new Dictionary(); - _logger = logger; - _appConfig = appConfig; + return Task.CompletedTask; + } + + try + { + var originalBotGuid = radarEvent.ScriptData.Split(";")[1]; + + if (originalBotGuid.IsBotGuid() && _appConfig.IgnoreBots) + { + return Task.CompletedTask; + } + + var botKey = $"BotGuid_{originalBotGuid}"; + long generatedBotGuid; + + lock (_lockObject) + { + var hasBotKey = _botGuidLookups.ContainsKey(botKey); + + if (!hasBotKey && originalBotGuid.IsBotGuid()) + { + // edge case where the bot guid has not been registered yet + return Task.CompletedTask; + } + + generatedBotGuid = hasBotKey + ? _botGuidLookups[botKey] + : (originalBotGuid ?? "0").ConvertGuidToLong(NumberStyles.HexNumber); + } + + var radarUpdate = RadarEvent.Parse(scriptEvent.ScriptData, generatedBotGuid); + var client = + radarEvent.Owner.ConnectedClients.FirstOrDefault(client => client.NetworkId == radarUpdate.Guid); + + if (client != null) + { + radarUpdate.Name = client.Name.StripColors(); + client.SetAdditionalProperty("LiveRadar", radarUpdate); + } } - public Task OnEventAsync(GameEvent E, Server S) + 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) + { + return Task.CompletedTask; + } + + var botKey = $"BotGuid_{clientEvent.ClientNetworkId}"; + lock (_lockObject) + { + if (!_botGuidLookups.ContainsKey(botKey)) + { + _botGuidLookups.Add(botKey, clientEvent.Client.NetworkId); + } + } + + return Task.CompletedTask; + } + + private Task OnMonitoringStarted(MonitorStartEvent monitorEvent, CancellationToken token) + { + lock (_lockObject) { // if it's an IW4 game, with custom callbacks, we want to // enable the live radar page - lock (lockObject) + var shouldRegisterPage = monitorEvent.Server.GameCode != Reference.Game.IW4 || + !monitorEvent.Server.IsLegacyGameIntegrationEnabled || + _addedPage; + if (shouldRegisterPage) { - if (E.Type == GameEvent.EventType.Start && - S.GameName == Server.Game.IW4 && - S.CustomCallback && - !addedPage) - { - E.Owner.Manager.GetPageList().Pages.Add(Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"], "/Radar"); - addedPage = true; - } + return Task.CompletedTask; } - if (E.Type == GameEvent.EventType.PreConnect && E.Origin.IsBot) - { - string botKey = $"BotGuid_{E.Extra}"; - lock (lockObject) - { - if (!_botGuidLookups.ContainsKey(botKey)) - { - _botGuidLookups.Add(botKey, E.Origin.NetworkId); - } - } - } - - if (E.Type == GameEvent.EventType.Other && E.Subtype == "LiveRadar") - { - try - { - if (((string) E.Extra).IsBotGuid() && _appConfig.IgnoreBots) - { - return Task.CompletedTask; - } - - string botKey = $"BotGuid_{E.Extra}"; - long generatedBotGuid; - - lock (lockObject) - { - var hasBotKey = _botGuidLookups.ContainsKey(botKey); - - if (!hasBotKey && ((string)E.Extra).IsBotGuid()) - { - // edge case where the bot guid has not been registered yet - return Task.CompletedTask; - } - - generatedBotGuid = hasBotKey - ? _botGuidLookups[botKey] - : (E.Extra.ToString() ?? "0").ConvertGuidToLong(NumberStyles.HexNumber); - } - - var radarUpdate = RadarEvent.Parse(E.Data, generatedBotGuid); - var client = S.Manager.GetActiveClients().FirstOrDefault(_client => _client.NetworkId == radarUpdate.Guid); - - if (client != null) - { - radarUpdate.Name = client.Name.StripColors(); - client.SetAdditionalProperty("LiveRadar", radarUpdate); - } - } - - catch (Exception e) - { - _logger.LogError(e, "Could not parse live radar output: {data}", e.Data); - } - } - - return Task.CompletedTask; + (monitorEvent.Source as IManager)?.GetPageList().Pages + .Add(Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_RADAR_TITLE"], "/Radar"); + _addedPage = true; } - public async Task OnLoadAsync(IManager manager) - { - await _configurationHandler.BuildAsync(); - if (_configurationHandler.Configuration() == null) - { - _configurationHandler.Set((LiveRadarConfiguration)new LiveRadarConfiguration().Generate()); - await _configurationHandler.Save(); - } - } - - public Task OnTickAsync(Server S) - { - return Task.CompletedTask; - } - - public Task OnUnloadAsync() - { - return Task.CompletedTask; - } + return Task.CompletedTask; } } diff --git a/Plugins/LiveRadar/RadarEvent.cs b/Plugins/LiveRadar/RadarEvent.cs index 699324522..bcbe7cea9 100644 --- a/Plugins/LiveRadar/RadarEvent.cs +++ b/Plugins/LiveRadar/RadarEvent.cs @@ -5,60 +5,59 @@ using System.Linq; // ReSharper disable CompareOfFloatsByEqualityOperator #pragma warning disable CS0659 -namespace LiveRadar +namespace IW4MAdmin.Plugins.LiveRadar; + +public class RadarEvent { - public class RadarEvent + public string Name { get; set; } + public long Guid { get; set; } + public Vector3 Location { get; set; } + public Vector3 ViewAngles { get; set; } + public string Team { get; set; } + public int Kills { get; set; } + public int Deaths { get; set; } + public int Score { get; set; } + public int PlayTime { get; set; } + public string Weapon { get; set; } + public int Health { get; set; } + public bool IsAlive { get; set; } + public Vector3 RadianAngles => new Vector3(ViewAngles.X.ToRadians(), ViewAngles.Y.ToRadians(), ViewAngles.Z.ToRadians()); + public int Id => GetHashCode(); + + public override bool Equals(object obj) { - public string Name { get; set; } - public long Guid { get; set; } - public Vector3 Location { get; set; } - public Vector3 ViewAngles { get; set; } - public string Team { get; set; } - public int Kills { get; set; } - public int Deaths { get; set; } - public int Score { get; set; } - public int PlayTime { get; set; } - public string Weapon { get; set; } - public int Health { get; set; } - public bool IsAlive { get; set; } - public Vector3 RadianAngles => new Vector3(ViewAngles.X.ToRadians(), ViewAngles.Y.ToRadians(), ViewAngles.Z.ToRadians()); - public int Id => GetHashCode(); - - public override bool Equals(object obj) + if (obj is RadarEvent re) { - if (obj is RadarEvent re) - { - return re.ViewAngles.X == ViewAngles.X && - re.ViewAngles.Y == ViewAngles.Y && - re.ViewAngles.Z == ViewAngles.Z && - re.Location.X == Location.X && - re.Location.Y == Location.Y && - re.Location.Z == Location.Z; - } - - return false; + return re.ViewAngles.X == ViewAngles.X && + re.ViewAngles.Y == ViewAngles.Y && + re.ViewAngles.Z == ViewAngles.Z && + re.Location.X == Location.X && + re.Location.Y == Location.Y && + re.Location.Z == Location.Z; } - public static RadarEvent Parse(string input, long generatedBotGuid) + return false; + } + + public static RadarEvent Parse(string input, long generatedBotGuid) + { + var items = input.Split(';').Skip(1).ToList(); + + var parsedEvent = new RadarEvent() { - var items = input.Split(';').Skip(1).ToList(); + Guid = generatedBotGuid, + Location = Vector3.Parse(items[1]), + ViewAngles = Vector3.Parse(items[2]).FixIW4Angles(), + Team = items[3], + Kills = int.Parse(items[4]), + Deaths = int.Parse(items[5]), + Score = int.Parse(items[6]), + Weapon = items[7], + Health = int.Parse(items[8]), + IsAlive = items[9] == "1", + PlayTime = Convert.ToInt32(items[10]) + }; - var parsedEvent = new RadarEvent() - { - Guid = generatedBotGuid, - Location = Vector3.Parse(items[1]), - ViewAngles = Vector3.Parse(items[2]).FixIW4Angles(), - Team = items[3], - Kills = int.Parse(items[4]), - Deaths = int.Parse(items[5]), - Score = int.Parse(items[6]), - Weapon = items[7], - Health = int.Parse(items[8]), - IsAlive = items[9] == "1", - PlayTime = Convert.ToInt32(items[10]) - }; - - return parsedEvent; - } + return parsedEvent; } }