#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <csgo_colors>
#include <ripext>
public Plugin myinfo =
{
name = "[TJN] Teammates Join Notification",
author = "SAPSAN 隼#5667",
version = "1.0.2",
url = ""
};
ArrayList aClientsFrends[MAXPLAYERS + 1] = {null, ...};
char g_WebApiKey[64];
public void OnPluginStart()
{
ConVar hConVar;
(hConVar = CreateConVar("sm_tjn_steam_web_api_key", "", "Steam WEB API Key")).AddChangeHook(ChangeCvar_TJN_WEB_KEY);
ChangeCvar_TJN_WEB_KEY(hConVar, NULL_STRING, NULL_STRING);
AutoExecConfig(true, "TJN");
LoadTranslations("TJN.phrases");
ServerCommand("sm_reload_translations");
for (int i = 1; i <= MaxClients; ++i)
{
aClientsFrends[i] = new ArrayList();
}
}
void ChangeCvar_TJN_WEB_KEY(ConVar Convar, const char[] oldValue, const char[] newValue)
{
Convar.GetString(g_WebApiKey, sizeof g_WebApiKey);
}
public void OnClientAuthorized(int iClient, const char [] sAuth)
{
if(IsClientConnected(iClient) && !IsFakeClient(iClient))
{
if(g_WebApiKey[0])
{
char sSteamID64[64];
GetClientAuthId(iClient, AuthId_SteamID64, sSteamID64, sizeof(sSteamID64));
HTTPRequest request = new HTTPRequest("http://api.steampowered.com/ISteamUser/GetFriendList/v0001");
request.AppendQueryParam("key", "%s", g_WebApiKey);
request.AppendQueryParam("steamid", "%s", sSteamID64);
request.AppendQueryParam("relationship", "%s", "friend");
request.Get(OnTodosGet, iClient);
}
else
{
LogError("[TJN] Not found Steam WEB API KEY!");
}
}
}
void OnTodosGet(HTTPResponse response, int iClient)
{
if (response.Status != HTTPStatus_OK)
{
return;
}
JSONObject JsonReturn = view_as<JSONObject>(response.Data);
JsonReturn = view_as<JSONObject>(JsonReturn.Get("friendslist"));
JSONArray JsonArray = view_as<JSONArray>(JsonReturn.Get("friends"));
delete JsonReturn;
JSONObject JsonArrReturns;
char sFrSteam[64];
for(int i = 0; i < JsonArray.Length; i++)
{
JsonArrReturns = view_as<JSONObject>(JsonArray.Get(i));
JsonArrReturns.GetString("steamid", sFrSteam, sizeof(sFrSteam));
int iTarget = FindClientFromAuth(sFrSteam);
if(iTarget)
{
if(IsClientConnected(iClient) && IsClientInGame(iTarget))
{
char sNameJoin[MAX_NAME_LENGTH];
GetClientName(iClient, sNameJoin, sizeof(sNameJoin));
CGOPrintToChat(iTarget, "%t %t", "TJN_Prefix", "TJN_Join", sNameJoin);
}
aClientsFrends[iClient].Push(iTarget);
}
delete JsonArrReturns;
}
delete JsonArray;
}
public void OnClientPostAdminCheck(int iClient)
{
if(aClientsFrends[iClient].Length)
{
for(int i = 0; i < aClientsFrends[iClient].Length; i++)
{
int iFrendIndex = aClientsFrends[iClient].Get(i);
if(IsClientInGame(iFrendIndex) && !IsFakeClient(iFrendIndex))
{
DataPack hPack = new DataPack();
hPack.WriteCell(iFrendIndex);
hPack.WriteCell(iClient);
CreateTimer(5.0, SenMSG, hPack);
}
}
}
}
Action SenMSG(Handle hTimer, Handle hDataPack)
{
DataPack hPack = view_as<DataPack>(hDataPack);
hPack.Reset();
char sFrendName[MAX_NAME_LENGTH];
GetClientName(hPack.ReadCell(), sFrendName, sizeof(sFrendName));
CGOPrintToChat(hPack.ReadCell(), "%t %t", "TJN_Prefix", "TJN_Play", sFrendName);
delete hPack;
}
public void OnClientDisconnect(int iClient)
{
if(aClientsFrends[iClient].Length)
{
for(int i = 0; i < aClientsFrends[iClient].Length; i++)
{
int iFrendIndex = aClientsFrends[iClient].Get(i);
if(IsClientInGame(iFrendIndex) && !IsFakeClient(iFrendIndex))
{
char sFrendName[MAX_NAME_LENGTH];
GetClientName(iClient, sFrendName, sizeof(sFrendName));
CGOPrintToChat(iFrendIndex, "%t %t", "TJN_Prefix", "TJN_Diconnected", sFrendName);
}
}
aClientsFrends[iClient].Clear();
}
}
int FindClientFromAuth(char[] szSourceAuth)
{
for (int iClient = MaxClients; iClient != 0; --iClient)
{
if (IsClientInGame(iClient) && !IsFakeClient(iClient))
{
char sSteamID64[64];
GetClientAuthId(iClient, AuthId_SteamID64, sSteamID64, sizeof(sSteamID64));
if(!strcmp(sSteamID64, szSourceAuth))
{
return iClient;
}
}
}
return 0;
}