using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Events;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SharedLibraryCore
{
public class GameEvent
{
// define what the delagate function looks like
public delegate void OnServerEventEventHandler(object sender, GameEventArgs e);
public enum EventFailReason
{
///
/// event execution did not fail
///
None,
///
/// an internal exception prevented the event
/// from executing
///
Exception,
///
/// event origin didn't have the necessary privileges
/// to execute the command
///
Permission,
///
/// executing the event would cause an invalid state
///
Invalid,
///
/// client is doing too much of something
///
Throttle,
///
/// the event timed out before completion
///
Timeout
}
public enum EventType
{
///
/// the event wasn't parsed properly
///
Unknown,
// events "generated" by the server
///
/// a server started being monitored
///
Start,
///
/// a server stopped being monitored
///
Stop,
///
/// a client was detecting as connecting via log
///
Connect,
///
/// a client was detecting joining by RCon
///
Join,
///
/// a client was detected leaving via log
///
Quit,
///
/// a client was detected leaving by RCon
///
Disconnect,
///
/// the current map ended
///
MapEnd,
///
/// the current map changed
///
MapChange,
///
/// a client was detected as starting to connect
///
PreConnect,
///
/// a client was detecting as starting to disconnect
///
PreDisconnect,
///
/// a client's information was updated
///
Update,
///
/// connection was lost to a server (the server has not responded after a number of attempts)
///
ConnectionLost,
///
/// connection was restored to a server (the server began responding again)
///
ConnectionRestored,
// events "generated" by clients
///
/// a client sent a message
///
Say = 100,
///
/// a client was warned
///
Warn = 101,
///
/// all warnings for a client were cleared
///
WarnClear = 102,
///
/// a client was reported
///
Report = 103,
///
/// a client was flagged
///
Flag = 104,
///
/// a client was unflagged
///
Unflag = 105,
///
/// a client was kicked
///
Kick = 106,
///
/// a client was tempbanned
///
TempBan = 107,
///
/// a client was banned
///
Ban = 108,
///
/// a client was unbanned
///
Unban = 109,
///
/// a client entered a command
///
Command = 110,
///
/// a client's permission was changed
///
ChangePermission = 111,
// events "generated" by IW4MAdmin
///
/// a message is sent to all clients
///
Broadcast = 200,
///
/// a message is sent to a specific client
///
Tell = 201,
// events "generated" by script/log
///
/// AC Damage Log
///
ScriptDamage = 300,
///
/// AC Kill Log
///
ScriptKill = 301,
///
/// damage info printed out by game script
///
Damage = 302,
///
/// kill info printed out by game script
///
Kill = 303,
///
/// team info printed out by game script
///
JoinTeam = 304,
///
/// used for community generated plugin events
///
Other
}
[Flags]
public enum EventRequiredEntity
{
None = 1,
Origin = 2,
Target = 4
}
static long NextEventId;
static long GetNextEventId()
{
return Interlocked.Increment(ref NextEventId);
}
public GameEvent()
{
OnProcessed = new ManualResetEventSlim(false);
Time = DateTime.UtcNow;
Id = GetNextEventId();
}
public EventType Type;
public EventRequiredEntity RequiredEntity { get; set; }
public string Data; // Data is usually the message sent by player
public string Message;
public EFClient Origin;
public EFClient Target;
public Server Owner;
public bool IsRemote { get; set; } = false;
public object Extra { get; set; }
public ManualResetEventSlim OnProcessed { get; set; }
public DateTime Time { get; set; }
public long Id { get; private set; }
public EventFailReason FailReason { get; set; }
public bool Failed => FailReason != EventFailReason.None;
///
/// asynchronously wait for GameEvent to be processed
///
/// waitable task
public Task WaitAsync(TimeSpan timeSpan, CancellationToken token)
{
return Task.Run(() =>
{
bool processed = OnProcessed.Wait(timeSpan, token);
// this let's us know if the the action timed out
FailReason = FailReason == EventFailReason.None & !processed ? EventFailReason.Timeout : FailReason;
return this;
});
}
public GameEvent Wait()
{
OnProcessed.Wait();
return this;
}
}
}