2018-04-08 14:48:40 -04:00
|
|
|
|
using System;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
namespace SharedLibraryCore.Events
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04: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>();
|
2018-06-16 22:11:25 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public static IEnumerable<EventInfo> GetEvents(bool shouldConsume)
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-06-16 22:11:25 -04:00
|
|
|
|
var eventList = RecentEvents.ToArray();
|
2018-04-08 14:48:40 -04:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// clear queue if events should be consumed
|
|
|
|
|
if (shouldConsume)
|
|
|
|
|
{
|
|
|
|
|
RecentEvents.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eventList;
|
|
|
|
|
}
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public static void OnGameEvent(GameEvent gameEvent)
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
var E = gameEvent;
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// 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
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
var apiEvent = new EventInfo()
|
|
|
|
|
{
|
|
|
|
|
ExtraInfo = E.Extra?.ToString() ?? E.Data,
|
2018-07-30 20:31:00 -04:00
|
|
|
|
GameInfo = new EntityInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = E.Owner.GameName.ToString(),
|
|
|
|
|
Id = (int)E.Owner.GameName
|
|
|
|
|
},
|
2018-06-16 22:11:25 -04:00
|
|
|
|
OwnerEntity = new EntityInfo()
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-06-16 22:11:25 -04:00
|
|
|
|
Name = E.Owner.Hostname,
|
2018-11-27 19:31:48 -05:00
|
|
|
|
Id = E.Owner.EndPoint
|
2018-06-16 22:11:25 -04:00
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// add the new event to the list
|
|
|
|
|
AddNewEvent(apiEvent);
|
|
|
|
|
}
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds event to the list and removes first added if reached max capacity
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">EventInfo to add</param>
|
2018-08-30 21:53:00 -04:00
|
|
|
|
private static void AddNewEvent(EventInfo info)
|
2018-06-16 22:11:25 -04:00
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
RecentEvents.Enqueue(info);
|
2018-03-09 03:01:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|