2019-07-05 21:53:03 -04:00
|
|
|
|
using LiveRadar.Configuration;
|
|
|
|
|
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;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2019-06-30 14:37:59 -04:00
|
|
|
|
|
|
|
|
|
namespace LiveRadar
|
|
|
|
|
{
|
|
|
|
|
public class Plugin : IPlugin
|
|
|
|
|
{
|
|
|
|
|
public string Name => "Live Radar";
|
|
|
|
|
|
|
|
|
|
public float Version => (float)Utilities.GetVersionAsDouble();
|
|
|
|
|
|
|
|
|
|
public string Author => "RaidMax";
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private readonly IConfigurationHandler<LiveRadarConfiguration> _configurationHandler;
|
2020-09-21 16:30:42 -04:00
|
|
|
|
private readonly Dictionary<string, long> _botGuidLookups;
|
2020-04-20 11:45:58 -04:00
|
|
|
|
private bool addedPage;
|
2020-04-22 22:08:25 -04:00
|
|
|
|
private readonly object lockObject = new object();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public Plugin(ILogger<Plugin> logger, IConfigurationHandlerFactory configurationHandlerFactory, ApplicationConfiguration appConfig)
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
|
|
|
|
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<LiveRadarConfiguration>("LiveRadarConfiguration");
|
2020-09-21 16:30:42 -04:00
|
|
|
|
_botGuidLookups = new Dictionary<string, long>();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger = logger;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_appConfig = appConfig;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
2019-07-05 21:53:03 -04:00
|
|
|
|
|
2019-06-30 14:37:59 -04:00
|
|
|
|
public Task OnEventAsync(GameEvent E, Server S)
|
|
|
|
|
{
|
2020-04-17 16:05:16 -04:00
|
|
|
|
// if it's an IW4 game, with custom callbacks, we want to
|
|
|
|
|
// enable the live radar page
|
2020-04-22 21:51:04 -04:00
|
|
|
|
lock (lockObject)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2020-04-22 21:51:04 -04:00
|
|
|
|
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/All");
|
|
|
|
|
addedPage = true;
|
|
|
|
|
}
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 16:30:42 -04:00
|
|
|
|
if (E.Type == GameEvent.EventType.PreConnect && E.Origin.IsBot)
|
2019-06-30 14:37:59 -04:00
|
|
|
|
{
|
2020-09-21 16:30:42 -04:00
|
|
|
|
string botKey = $"BotGuid_{E.Extra}";
|
|
|
|
|
lock (lockObject)
|
2019-06-30 14:37:59 -04:00
|
|
|
|
{
|
2020-09-21 16:30:42 -04:00
|
|
|
|
if (!_botGuidLookups.ContainsKey(botKey))
|
2019-07-29 13:08:25 -04:00
|
|
|
|
{
|
2020-09-21 16:30:42 -04:00
|
|
|
|
_botGuidLookups.Add(botKey, E.Origin.NetworkId);
|
2019-07-29 13:08:25 -04:00
|
|
|
|
}
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == GameEvent.EventType.Other && E.Subtype == "LiveRadar")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (((string) E.Extra).IsBotGuid() && _appConfig.IgnoreBots)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 16:30:42 -04:00
|
|
|
|
string botKey = $"BotGuid_{E.Extra}";
|
|
|
|
|
long generatedBotGuid;
|
|
|
|
|
|
|
|
|
|
lock (lockObject)
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
generatedBotGuid = _botGuidLookups.ContainsKey(botKey)
|
|
|
|
|
? _botGuidLookups[botKey]
|
|
|
|
|
: (E.Extra.ToString() ?? "0").ConvertGuidToLong(NumberStyles.HexNumber);
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var radarUpdate = RadarEvent.Parse(E.Data, generatedBotGuid);
|
|
|
|
|
var client = S.Manager.GetActiveClients().FirstOrDefault(_client => _client.NetworkId == radarUpdate.Guid);
|
2019-07-05 21:53:03 -04:00
|
|
|
|
|
2020-09-21 16:30:42 -04:00
|
|
|
|
if (client != null)
|
2019-07-05 21:53:03 -04:00
|
|
|
|
{
|
2020-09-21 16:30:42 -04:00
|
|
|
|
radarUpdate.Name = client.Name.StripColors();
|
|
|
|
|
client.SetAdditionalProperty("LiveRadar", radarUpdate);
|
2019-07-05 21:53:03 -04:00
|
|
|
|
}
|
2019-06-30 14:37:59 -04:00
|
|
|
|
}
|
2020-09-21 16:30:42 -04:00
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogError(e, "Could not parse live radar output: {data}", e.Data);
|
2020-09-21 16:30:42 -04:00
|
|
|
|
}
|
2019-06-30 14:37:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 21:53:03 -04:00
|
|
|
|
public async Task OnLoadAsync(IManager manager)
|
2019-06-30 14:37:59 -04:00
|
|
|
|
{
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await _configurationHandler.BuildAsync();
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (_configurationHandler.Configuration() == null)
|
2019-07-05 21:53:03 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_configurationHandler.Set((LiveRadarConfiguration)new LiveRadarConfiguration().Generate());
|
|
|
|
|
await _configurationHandler.Save();
|
2019-07-05 21:53:03 -04:00
|
|
|
|
}
|
2019-06-30 14:37:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnTickAsync(Server S)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnUnloadAsync()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|