using SharedLibraryCore.Database.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SharedLibraryCore
{
public class GameEvent
{
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
}
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
}
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 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(int timeOut = int.MaxValue)
{
return Task.Run(() =>
{
OnProcessed.Wait(timeOut);
return this;
});
}
}
}