mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
moved event API stuff around
finally fixed threading issue (which actually had to do with IW4x log outputs being out of sync (not an issue with my code). What a lot of headache over something that wasn't my fault.
This commit is contained in:
80
SharedLibraryCore/Events/EventAPI.cs
Normal file
80
SharedLibraryCore/Events/EventAPI.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SharedLibraryCore.Dtos;
|
||||
|
||||
namespace SharedLibraryCore.Events
|
||||
{
|
||||
public class EventApi
|
||||
{
|
||||
const int MaxEvents = 100;
|
||||
static Queue<EventInfo> RecentEvents = new Queue<EventInfo>();
|
||||
|
||||
public static IEnumerable<EventInfo> GetEvents(bool shouldConsume)
|
||||
{
|
||||
var eventList = RecentEvents.ToArray();
|
||||
|
||||
// clear queue if events should be consumed
|
||||
if (shouldConsume)
|
||||
{
|
||||
RecentEvents.Clear();
|
||||
}
|
||||
|
||||
return eventList;
|
||||
}
|
||||
|
||||
public static void OnGameEvent(object sender, GameEventArgs eventState)
|
||||
{
|
||||
var E = eventState.Event;
|
||||
// don't want to clog up the api with unknown events
|
||||
if (E.Type == GameEvent.EventType.Unknown)
|
||||
return;
|
||||
|
||||
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()
|
||||
{
|
||||
Name = E.Owner.Hostname,
|
||||
Id = E.Owner.GetHashCode()
|
||||
},
|
||||
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
|
||||
};
|
||||
|
||||
// add the new event to the list
|
||||
AddNewEvent(apiEvent);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
RecentEvents.Dequeue();
|
||||
|
||||
RecentEvents.Enqueue(info);
|
||||
}
|
||||
}
|
||||
}
|
102
SharedLibraryCore/Events/GameEvent.cs
Normal file
102
SharedLibraryCore/Events/GameEvent.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using SharedLibraryCore.Objects;
|
||||
|
||||
namespace SharedLibraryCore
|
||||
{
|
||||
public class GameEvent
|
||||
{
|
||||
public enum EventType
|
||||
{
|
||||
Unknown,
|
||||
|
||||
// events "generated" by the server
|
||||
Start,
|
||||
Stop,
|
||||
Connect,
|
||||
Join,
|
||||
Quit,
|
||||
Disconnect,
|
||||
MapEnd,
|
||||
MapChange,
|
||||
|
||||
// events "generated" by clients
|
||||
Say,
|
||||
Report,
|
||||
Flag,
|
||||
Unflag,
|
||||
Kick,
|
||||
TempBan,
|
||||
Ban,
|
||||
Command,
|
||||
|
||||
// events "generated" by IW4MAdmin
|
||||
Broadcast,
|
||||
Tell,
|
||||
|
||||
// events "generated" by script/log
|
||||
ScriptDamage,
|
||||
ScriptKill,
|
||||
Damage,
|
||||
Kill,
|
||||
JoinTeam,
|
||||
|
||||
StatusUpdate
|
||||
}
|
||||
|
||||
static long NextEventId;
|
||||
static long GetNextEventId() => Interlocked.Increment(ref NextEventId);
|
||||
|
||||
public GameEvent()
|
||||
{
|
||||
OnProcessed = new SemaphoreSlim(0);
|
||||
OnProcessed.Release();
|
||||
Time = DateTime.UtcNow;
|
||||
Id = GetNextEventId();
|
||||
}
|
||||
|
||||
public EventType Type;
|
||||
public string Data; // Data is usually the message sent by player
|
||||
public string Message;
|
||||
public Player Origin;
|
||||
public Player Target;
|
||||
public Server Owner;
|
||||
public Boolean Remote = false;
|
||||
public object Extra { get; set; }
|
||||
public SemaphoreSlim OnProcessed { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
public long Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// determine whether an event should be delayed or not
|
||||
/// applies only to the origin entity
|
||||
/// </summary>
|
||||
/// <param name="queuedEvent">event to determine status for</param>
|
||||
/// <returns>true if event should be delayed, false otherwise</returns>
|
||||
public static bool ShouldOriginEventBeDelayed(GameEvent queuedEvent)
|
||||
{
|
||||
return queuedEvent.Origin != null &&
|
||||
queuedEvent.Origin.State != Player.ClientState.Connected &&
|
||||
// we want to allow join and quit events
|
||||
queuedEvent.Type != EventType.Connect &&
|
||||
queuedEvent.Type != EventType.Join &&
|
||||
queuedEvent.Type != EventType.Quit &&
|
||||
queuedEvent.Type != EventType.Disconnect &&
|
||||
// we don't care about unknown events
|
||||
queuedEvent.Origin.NetworkId != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// determine whether an event should be delayed or not
|
||||
/// applies only to the target entity
|
||||
/// </summary>
|
||||
/// <param name="queuedEvent">event to determine status for</param>
|
||||
/// <returns>true if event should be delayed, false otherwise</returns>
|
||||
public static bool ShouldTargetEventBeDelayed(GameEvent queuedEvent)
|
||||
{
|
||||
return queuedEvent.Target != null &&
|
||||
queuedEvent.Target.State != Player.ClientState.Connected &&
|
||||
queuedEvent.Target.NetworkId != 0;
|
||||
}
|
||||
}
|
||||
}
|
23
SharedLibraryCore/Events/GameEventArgs.cs
Normal file
23
SharedLibraryCore/Events/GameEventArgs.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SharedLibraryCore.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// represents the state of a game event for event processing
|
||||
/// </summary>
|
||||
public class GameEventArgs : System.ComponentModel.AsyncCompletedEventArgs
|
||||
{
|
||||
|
||||
public GameEventArgs(Exception error, bool cancelled, GameEvent userState) : base(error, cancelled, userState)
|
||||
{
|
||||
Event = userState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Game event that occured on a server
|
||||
/// </summary>
|
||||
public GameEvent Event { get; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user