2021-08-31 19:21:40 -04:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
|
|
|
{
|
|
|
|
|
public class EventPublisher : IEventPublisher
|
|
|
|
|
{
|
|
|
|
|
public event EventHandler<GameEvent> OnClientDisconnect;
|
|
|
|
|
public event EventHandler<GameEvent> OnClientConnect;
|
2022-07-13 17:10:16 -04:00
|
|
|
|
public event EventHandler<GameEvent> OnClientMetaUpdated;
|
2021-08-31 19:21:40 -04:00
|
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public EventPublisher(ILogger<EventPublisher> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Publish(GameEvent gameEvent)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Handling publishing event of type {EventType}", gameEvent.Type);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.Connect)
|
|
|
|
|
{
|
|
|
|
|
OnClientConnect?.Invoke(this, gameEvent);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 16:00:23 -04:00
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.Disconnect && gameEvent.Origin.ClientId != 0)
|
2021-08-31 19:21:40 -04:00
|
|
|
|
{
|
|
|
|
|
OnClientDisconnect?.Invoke(this, gameEvent);
|
|
|
|
|
}
|
2022-07-13 17:10:16 -04:00
|
|
|
|
|
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.MetaUpdated)
|
|
|
|
|
{
|
|
|
|
|
OnClientMetaUpdated?.Invoke(this, gameEvent);
|
|
|
|
|
}
|
2021-08-31 19:21:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not publish event of type {EventType}", gameEvent.Type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-14 16:00:23 -04:00
|
|
|
|
}
|