SMJSONAPI: Add Forwards

This commit is contained in:
BotoX 2019-10-12 20:25:26 +02:00
parent 802ef98c0c
commit 2d336798e2
2 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,13 @@
public void OnClientPostAdminCheck(int client)
{
JSONObject jEvent = new JSONObject();
jEvent.SetString("name", "OnClientPostAdminCheck");
JSONObject jEventData = new JSONObject();
jEventData.SetInt("client", client);
jEvent.Set("data", jEventData);
Subscribe_Forwards_Publish("OnClientPostAdminCheck", jEvent);
}

View File

@ -1,4 +1,5 @@
#include "GameEvents.inc" #include "GameEvents.inc"
#include "Forwards.inc"
#define MAX_SUBSCRIBERS MAX_CLIENTS #define MAX_SUBSCRIBERS MAX_CLIENTS
@ -7,12 +8,14 @@ enum
eSubscribeError_OutOfRange = -1, eSubscribeError_OutOfRange = -1,
eSubscribeError_Inactive = -2, eSubscribeError_Inactive = -2,
eSubscribeError_GameEvent = -3, eSubscribeError_GameEvent = -3,
eSubscribeError_Forward = -4,
} }
static bool g_Subscriber_Active[MAX_SUBSCRIBERS] = { false, ... }; static bool g_Subscriber_Active[MAX_SUBSCRIBERS] = { false, ... };
static int g_Subscriber_Client[MAX_SUBSCRIBERS] = { -1, ... }; static int g_Subscriber_Client[MAX_SUBSCRIBERS] = { -1, ... };
static ArrayList g_Subscriber_GameEvents[MAX_SUBSCRIBERS]; static ArrayList g_Subscriber_GameEvents[MAX_SUBSCRIBERS];
static ArrayList g_Subscriber_Forwards[MAX_SUBSCRIBERS];
bool Subscribe_OnPluginStart() bool Subscribe_OnPluginStart()
{ {
@ -33,6 +36,7 @@ int Subscriber_Create(int Client)
g_Subscriber_Client[Index] = Client; g_Subscriber_Client[Index] = Client;
g_Subscriber_GameEvents[Index] = new ArrayList(ByteCountToCells(32)); g_Subscriber_GameEvents[Index] = new ArrayList(ByteCountToCells(32));
g_Subscriber_Forwards[Index] = new ArrayList(ByteCountToCells(32));
return Index; return Index;
} }
@ -53,6 +57,7 @@ int Subscriber_Destroy(int Index)
} }
delete g_Subscriber_GameEvents[Index]; delete g_Subscriber_GameEvents[Index];
delete g_Subscriber_Forwards[Index];
g_Subscriber_Active[Index] = false; g_Subscriber_Active[Index] = false;
g_Subscriber_Client[Index] = -1; g_Subscriber_Client[Index] = -1;
@ -122,6 +127,54 @@ int Subscriber_HandleRequest(int Index, JSONObject Request, JSONObject Response)
Response.Set("events", EventArrayResponse); Response.Set("events", EventArrayResponse);
return 0; return 0;
} }
else if(StrEqual(sModule, "forwards"))
{
JSONArray EventArray = view_as<JSONArray>(Request.Get("events"));
if(EventArray == null || !EventArray.IsArray)
{
delete EventArray;
JSONObject jError = new JSONObject();
jError.SetString("error", "Request has no 'events' array-value.");
Response.Set("error", jError);
return -1;
}
int Method = 0;
if(StrEqual(sMethod, "subscribe"))
Method = 1;
else if(StrEqual(sMethod, "unsubscribe"))
Method = 2;
else if(StrEqual(sMethod, "replay"))
Method = 3;
JSONArray EventArrayResponse = new JSONArray();
for(int i = 0; i < EventArray.Length; i++)
{
static char sEventName[32];
if(EventArray.GetString(i, sEventName, sizeof(sEventName)) < 0)
{
JSONObject jError = new JSONObject();
jError.SetString("error", "not a string-value");
EventArrayResponse.Append(jError);
continue;
}
int Res;
if(Method == 1)
Res = Subscribe_Forwards_Subscribe(Index, sEventName);
else if(Method == 2)
Res = Subscribe_Forwards_Unsubscribe(Index, sEventName);
else if(Method == 3)
Res = Subscribe_Forwards_Replay(Index, sEventName);
EventArrayResponse.AppendInt(Res);
}
delete EventArray;
Response.Set("events", EventArrayResponse);
return 0;
}
JSONObject jError = new JSONObject(); JSONObject jError = new JSONObject();
jError.SetString("error", "No handler found for requested module."); jError.SetString("error", "No handler found for requested module.");
@ -275,3 +328,109 @@ void Subscribe_GameEvents_Publish(const char[] sEventName, JSONObject jEvent)
delete jPublish; delete jPublish;
} }
/* GameEvents */ /* GameEvents */
/* Forwards */
static int Subscribe_Forwards_Subscribe(int Index, const char[] sEventName)
{
if(Index < 0 || Index >= MAX_SUBSCRIBERS)
return eSubscribeError_OutOfRange;
if(!g_Subscriber_Active[Index])
return eSubscribeError_Inactive;
int Find = g_Subscriber_Forwards[Index].FindString(sEventName);
if(Find != -1)
return Find;
// TODO: forward exists?
return g_Subscriber_Forwards[Index].PushString(sEventName);
}
static int Subscribe_Forwards_Unsubscribe(int Index, const char[] sEventName)
{
if(Index < 0 || Index >= MAX_SUBSCRIBERS)
return eSubscribeError_OutOfRange;
if(!g_Subscriber_Active[Index])
return eSubscribeError_Inactive;
int Find = g_Subscriber_Forwards[Index].FindString(sEventName);
if(Find == -1)
return 0;
g_Subscriber_Forwards[Index].Erase(Find);
return Find;
}
static int Subscribe_Forwards_Replay(int Index, const char[] sEventName)
{
if(Index < 0 || Index >= MAX_SUBSCRIBERS)
return eSubscribeError_OutOfRange;
if(!g_Subscriber_Active[Index])
return eSubscribeError_Inactive;
if(StrEqual(sEventName, "OnClientPostAdminCheck"))
{
for(int client = 1; client <= MaxClients; client++)
{
if(!IsClientInGame(client) || !IsClientAuthorized(client))
continue;
JSONObject jEventData = new JSONObject();
jEventData.SetInt("client", client);
Subscribe_Forwards_FakePublish(g_Subscriber_Client[Index], sEventName, jEventData);
}
}
else
return eSubscribeError_Forward;
return 0;
}
static int Subscribe_Forwards_FakePublish(int Index, const char[] sEventName, JSONObject jEventData)
{
if(Index < 0 || Index >= MAX_SUBSCRIBERS)
return eSubscribeError_OutOfRange;
if(!g_Subscriber_Active[Index])
return eSubscribeError_Inactive;
JSONObject jEvent = new JSONObject();
jEvent.SetString("name", sEventName);
jEvent.Set("data", jEventData);
JSONObject jPublish = new JSONObject();
jPublish.SetString("method", "publish");
jPublish.SetString("module", "forwards");
jPublish.Set("event", jEvent);
PublishEvent(g_Subscriber_Client[Index], jPublish);
delete jPublish;
return 0;
}
void Subscribe_Forwards_Publish(const char[] sEventName, JSONObject jEvent)
{
JSONObject jPublish = new JSONObject();
jPublish.SetString("method", "publish");
jPublish.SetString("module", "forwards");
jPublish.Set("event", jEvent);
for(int Index = 0; Index < MAX_SUBSCRIBERS; Index++)
{
if(!g_Subscriber_Active[Index])
continue;
if(g_Subscriber_Forwards[Index].FindString(sEventName) == -1)
continue;
PublishEvent(g_Subscriber_Client[Index], jPublish);
}
delete jPublish;
}
/* Forwards */