IW4M-Admin/SharedLibraryCore/Events/EventAPI.cs

82 lines
2.5 KiB
C#
Raw Normal View History

using System;
2018-09-23 20:45:54 -04:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Dtos;
2018-03-09 03:01:12 -05:00
namespace SharedLibraryCore.Events
2018-03-09 03:01:12 -05:00
{
public class EventApi
2018-03-09 03:01:12 -05:00
{
2019-07-05 21:53:03 -04:00
const int MaxEvents = 25;
2018-09-23 20:45:54 -04:00
static ConcurrentQueue<EventInfo> RecentEvents = new ConcurrentQueue<EventInfo>();
public static IEnumerable<EventInfo> GetEvents(bool shouldConsume)
2018-03-09 03:01:12 -05:00
{
var eventList = RecentEvents.ToArray();
// clear queue if events should be consumed
if (shouldConsume)
{
RecentEvents.Clear();
}
return eventList;
}
2018-03-09 03:01:12 -05:00
public static void OnGameEvent(object sender, GameEventArgs eventState)
2018-03-09 03:01:12 -05:00
{
var E = eventState.Event;
// don't want to clog up the api with unknown events
if (E.Type == GameEvent.EventType.Unknown)
return;
2018-03-09 03:01:12 -05:00
var apiEvent = new EventInfo()
{
ExtraInfo = E.Extra?.ToString() ?? E.Data,
GameInfo = new EntityInfo()
{
Name = E.Owner.GameName.ToString(),
Id = (int)E.Owner.GameName
},
OwnerEntity = new EntityInfo()
2018-03-09 03:01:12 -05:00
{
Name = E.Owner.Hostname,
Id = E.Owner.EndPoint
},
OriginEntity = E.Origin == null ? null : new EntityInfo()
{
Id = E.Origin.ClientId,
Name = E.Origin.Name
},
TargetEntity = E.Target == null ? null : new EntityInfo()
{
Id = E.Target.ClientId,
Name = E.Target.Name
},
EventType = new EntityInfo()
{
Id = (int)E.Type,
Name = E.Type.ToString()
},
EventTime = E.Time
};
2018-03-09 03:01:12 -05:00
// add the new event to the list
AddNewEvent(apiEvent);
}
2018-03-09 03:01:12 -05:00
/// <summary>
/// Adds event to the list and removes first added if reached max capacity
/// </summary>
/// <param name="info">EventInfo to add</param>
private static void AddNewEvent(EventInfo info)
{
// remove the first added event
if (RecentEvents.Count >= MaxEvents)
2018-09-23 20:45:54 -04:00
RecentEvents.TryDequeue(out _);
2018-03-09 03:01:12 -05:00
RecentEvents.Enqueue(info);
2018-03-09 03:01:12 -05:00
}
}
}