#include <sdktools_gamerules>
StringMap g_hTrie;
bool g_bGame, g_bActivateSave;
public APLRes AskPluginLoad2(Handle hMySelf, bool bLate, char[] sError, int iErrMax)
{
if(GetEngineVersion() == Engine_CSGO) g_bGame = true;
else g_bGame = false;
return APLRes_Success;
}
public void OnPluginStart()
{
g_hTrie = new StringMap();
HookEvent("round_start", RoundStart, EventHookMode_PostNoCopy);
HookEvent("player_team", PlayerTeam, EventHookMode_Pre);
}
public void OnMapStart()
{
g_bActivateSave = false;
if(g_hTrie != null) g_hTrie.Clear();
}
public void RoundStart(Event hEvent, const char[] sEvName, bool bDbc)
{
if(!g_bGame || !GameRules_GetProp("m_bWarmupPeriod")) g_bActivateSave = true;
}
public void PlayerTeam(Event hEvent, const char[] sEvName, bool bDbc)
{
if(g_bActivateSave)
{
int iClient = GetClientOfUserId(hEvent.GetInt("userid"));
if(!IsFakeClient(iClient))
{
int iOldTeam = GetClientTeam(iClient), iNewTeam = hEvent.GetInt("team");
if(iOldTeam > 1 && iNewTeam == 1) SaveStats(iClient);
else if(iOldTeam < 2 && iNewTeam > 1)
{
char sBuffer[64], sId[32];
IntToString(GetSteamAccountID(iClient), sId, sizeof(sId));
if((g_hTrie.GetString(sId, sBuffer, sizeof(sBuffer))))
{
char sExplodeBuffer[3][10];
ExplodeString(sBuffer, " ", sExplodeBuffer, sizeof(sExplodeBuffer), sizeof(sExplodeBuffer[]));
SetEntProp(iClient, Prop_Send, "m_iAccount", StringToInt(sExplodeBuffer[0]));
SetEntProp(iClient, Prop_Data, "m_iFrags", StringToInt(sExplodeBuffer[1]));
SetEntProp(iClient, Prop_Data, "m_iDeaths", StringToInt(sExplodeBuffer[2]));
g_hTrie.Remove(sId);
}
}
}
}
}
public void OnClientDisconnect(int iClient)
{
if(g_bActivateSave && IsClientInGame(iClient) && !IsFakeClient(iClient) && GetClientTeam(iClient) > 1) SaveStats(iClient);
}
void SaveStats(int iClient)
{
char sBuffer[32], sId[32];
FormatEx(sBuffer, sizeof(sBuffer), "%i %i %i", GetEntProp(iClient, Prop_Send, "m_iAccount"),
GetEntProp(iClient, Prop_Data, "m_iFrags"),
GetEntProp(iClient, Prop_Data, "m_iDeaths"));
IntToString(GetSteamAccountID(iClient), sId, sizeof(sId));
g_hTrie.SetString(sId, sBuffer);
}