2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-05-10 01:34:29 -04:00
|
|
|
|
using System.Linq;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Text;
|
2018-05-10 01:34:29 -04:00
|
|
|
|
using System.Threading;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application
|
|
|
|
|
{
|
|
|
|
|
class GameEventHandler : IEventHandler
|
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
private ConcurrentQueue<GameEvent> EventQueue;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
private Queue<GameEvent> DelayedEventQueue;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
private IManager Manager;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
private const int DelayAmount = 5000;
|
|
|
|
|
private DateTime LastDelayedEvent;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
public GameEventHandler(IManager mgr)
|
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
EventQueue = new ConcurrentQueue<GameEvent>();
|
2018-06-30 21:55:16 -04:00
|
|
|
|
DelayedEventQueue = new Queue<GameEvent>();
|
2018-05-10 01:34:29 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Manager = mgr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-30 21:55:16 -04:00
|
|
|
|
public void AddEvent(GameEvent gameEvent, bool delayedExecution = false)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Manager.GetLogger().WriteDebug($"Got new event of type {gameEvent.Type} for {gameEvent.Owner}");
|
|
|
|
|
#endif
|
2018-06-30 21:55:16 -04:00
|
|
|
|
if (delayedExecution)
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-06-30 21:55:16 -04:00
|
|
|
|
DelayedEventQueue.Enqueue(gameEvent);
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
EventQueue.Enqueue(gameEvent);
|
2018-05-10 01:34:29 -04:00
|
|
|
|
Manager.SetHasEvent();
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Manager.GetLogger().WriteDebug($"There are now {EventQueue.Count} events in queue");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetEventOutput()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-30 21:55:16 -04:00
|
|
|
|
public GameEvent GetNextEvent()
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-07-01 20:30:38 -04:00
|
|
|
|
if (DelayedEventQueue.Count > 0 &&
|
|
|
|
|
(DateTime.Now - LastDelayedEvent).TotalMilliseconds > DelayAmount)
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-07-01 20:30:38 -04:00
|
|
|
|
LastDelayedEvent = DateTime.Now;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
#if DEBUG
|
2018-07-01 20:30:38 -04:00
|
|
|
|
Manager.GetLogger().WriteDebug("Getting next delayed event to be processed");
|
2018-06-30 21:55:16 -04:00
|
|
|
|
#endif
|
2018-07-01 20:30:38 -04:00
|
|
|
|
if (!DelayedEventQueue.TryDequeue(out GameEvent newEvent))
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-07-01 20:30:38 -04:00
|
|
|
|
Manager.GetLogger().WriteWarning("Could not dequeue delayed event for processing");
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
2018-04-28 01:22:18 -04:00
|
|
|
|
|
2018-06-30 21:55:16 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return newEvent;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-01 20:30:38 -04:00
|
|
|
|
if (EventQueue.Count > 0)
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
#if DEBUG
|
2018-07-01 20:30:38 -04:00
|
|
|
|
Manager.GetLogger().WriteDebug("Getting next event to be processed");
|
2018-04-26 02:13:04 -04:00
|
|
|
|
#endif
|
2018-07-01 20:30:38 -04:00
|
|
|
|
if (!EventQueue.TryDequeue(out GameEvent newEvent))
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-07-01 20:30:38 -04:00
|
|
|
|
Manager.GetLogger().WriteWarning("Could not dequeue event for processing");
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return newEvent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
return null;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|