2023-02-11 21:46:08 -05:00
|
|
|
|
using SharedLibraryCore;
|
2019-06-30 14:37:59 -04:00
|
|
|
|
using System;
|
2020-09-21 16:30:42 -04:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
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;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2023-02-11 21:46:08 -05:00
|
|
|
|
using SharedLibraryCore.Events.Game;
|
|
|
|
|
using SharedLibraryCore.Events.Server;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2023-02-11 21:46:08 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces.Events;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
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>();
|
|
|
|
|
}
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
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)
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
2023-02-11 21:46:08 -05:00
|
|
|
|
return Task.CompletedTask;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
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)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2023-02-11 21:46:08 -05:00
|
|
|
|
return Task.CompletedTask;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
2023-02-11 21:46:08 -05:00
|
|
|
|
|
|
|
|
|
var botKey = $"BotGuid_{originalBotGuid}";
|
|
|
|
|
long generatedBotGuid;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
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;
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
2023-02-11 21:46:08 -05:00
|
|
|
|
|
|
|
|
|
generatedBotGuid = hasBotKey
|
|
|
|
|
? _botGuidLookups[botKey]
|
|
|
|
|
: (originalBotGuid ?? "0").ConvertGuidToLong(NumberStyles.HexNumber);
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
2020-09-21 16:30:42 -04:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|