IW4M-Admin/Plugins/LiveRadar/Plugin.cs

111 lines
3.7 KiB
C#
Raw Normal View History

2019-07-05 21:53:03 -04:00
using LiveRadar.Configuration;
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
2019-06-30 14:37:59 -04:00
using SharedLibraryCore.Interfaces;
using System;
using System.Collections.Generic;
2019-06-30 14:37:59 -04:00
using System.Linq;
using System.Threading.Tasks;
namespace LiveRadar
{
public class Plugin : IPlugin
{
public string Name => "Live Radar";
public float Version => (float)Utilities.GetVersionAsDouble();
public string Author => "RaidMax";
private readonly IConfigurationHandler<LiveRadarConfiguration> _configurationHandler;
private readonly Dictionary<string, long> _botGuidLookups;
private bool addedPage;
2020-04-22 22:08:25 -04:00
private readonly object lockObject = new object();
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
{
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<LiveRadarConfiguration>("LiveRadarConfiguration");
_botGuidLookups = new Dictionary<string, long>();
}
2019-07-05 21:53:03 -04:00
2019-06-30 14:37:59 -04:00
public Task OnEventAsync(GameEvent E, Server S)
{
// if it's an IW4 game, with custom callbacks, we want to
// enable the live radar page
lock (lockObject)
{
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;
}
}
if (E.Type == GameEvent.EventType.PreConnect && E.Origin.IsBot)
2019-06-30 14:37:59 -04:00
{
string botKey = $"BotGuid_{E.Extra}";
lock (lockObject)
2019-06-30 14:37:59 -04:00
{
if (!_botGuidLookups.ContainsKey(botKey))
{
_botGuidLookups.Add(botKey, E.Origin.NetworkId);
}
}
}
if (E.Type == GameEvent.EventType.Other && E.Subtype == "LiveRadar")
{
try
{
string botKey = $"BotGuid_{E.Extra}";
long generatedBotGuid;
lock (lockObject)
{
generatedBotGuid = _botGuidLookups.ContainsKey(botKey) ? _botGuidLookups[botKey] : 0;
}
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
if (client != null)
2019-07-05 21:53:03 -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
}
catch (Exception e)
{
S.Logger.WriteWarning($"Could not parse live radar output: {e.Data}");
S.Logger.WriteDebug(e.GetExceptionInfo());
}
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
{
if (_configurationHandler.Configuration() == null)
2019-07-05 21:53:03 -04: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;
}
}
}