diff --git a/discord_verificiation/scripting/include/unloze_playtime.inc b/discord_verificiation/scripting/include/unloze_playtime.inc new file mode 100755 index 00000000..277e555e --- /dev/null +++ b/discord_verificiation/scripting/include/unloze_playtime.inc @@ -0,0 +1,9 @@ +#if defined _unloze_playtime_included_ + #endinput +#endif +#define _unloze_playtime_included_ + +/** +* @returns the average playtime of all connected players excluding autism bots, fakeclients, AFKS and nosteamers +*/ +native int GetAveragePlayerTimeOnServer(); diff --git a/discord_verificiation/scripting/unloze_player_time.sp b/discord_verificiation/scripting/unloze_player_time.sp index 1600217c..af21ba16 100644 --- a/discord_verificiation/scripting/unloze_player_time.sp +++ b/discord_verificiation/scripting/unloze_player_time.sp @@ -1,11 +1,17 @@ #pragma semicolon 1 #define PLUGIN_AUTHOR "jenz" -#define PLUGIN_VERSION "1.0" +#define PLUGIN_VERSION "1.1" #include +#include +#include Database g_hDatabase; +//check if autismbot +bool is_bot_player[MAXPLAYERS + 1]; Handle g_h_time_activity = null; char g_cTimeRecords[100][128]; +int g_iPlayerTimeServer[MAXPLAYERS + 1]; +int g_iPlayerAFKTime; public Plugin myinfo = { @@ -73,8 +79,42 @@ public void OnPluginStart() RegConsoleCmd("sm_topplaytime", Command_TopTime, "retreives top 12 playtime highscores on all connected servers"); g_h_time_activity = CreateTimer(10.0, time_query_activity, INVALID_HANDLE, TIMER_REPEAT); + + ConVar cvar; + HookConVarChange((cvar = CreateConVar("sm_mapchooser_afk_detect_time", "120", "Time in seconds until a player is considered as AFK and therefore excluded from player average time.")), Cvar_playerAFKTime); + g_iPlayerAFKTime = cvar.IntValue; + delete cvar; } +public void Cvar_playerAFKTime(ConVar convar, const char[] oldValue, const char[] newValue) +{ + g_iPlayerAFKTime = convar.IntValue; +} + +public APLRes AskPluginLoad2(Handle myself, bool late, char [] error, int err_max) +{ + CreateNative("GetAveragePlayerTimeOnServer", Native_GetAveragePlayerActiveTimeServer); + return APLRes_Success; +} + +public int Native_GetAveragePlayerActiveTimeServer(Handle plugin, int numParams) +{ + int total_hours = 0; + int total_players = 0; + for (int i = 0; i < MaxClients; i++) + { + if (IsValidClient(i) && !IsFakeClient(i) && !IsClientSourceTV(i) && !is_bot_player[i] && PM_IsPlayerSteam(i) && GetClientIdleTime(i) < g_iPlayerAFKTime) + { + total_hours += g_iPlayerTimeServer[i]; + total_players++; + } + } + if (total_hours == 0) + { + return 0; + } + return total_hours / total_players; +} public void OnPluginEnd() { if (g_h_time_activity != null) @@ -85,8 +125,8 @@ public void SQL_OnDatabaseConnect(Database db, const char[] error, any data) { if(!db || strlen(error)) { - LogError("Database error: %s", error); - return; + LogError("Database error: %s", error); + return; } g_hDatabase = db; for (int i = 1; i <= MaxClients; i++) @@ -96,6 +136,8 @@ public void SQL_OnDatabaseConnect(Database db, const char[] error, any data) public void OnClientPostAdminCheck(int client) { + is_bot_player[client] = false; + g_iPlayerTimeServer[client] = 0; if(!IsValidClient(client) || IsFakeClient(client)) return; if (!g_hDatabase) @@ -104,6 +146,84 @@ public void OnClientPostAdminCheck(int client) return; } insert_client(client); + select_client_time_server(client); + + char auth[50]; + GetClientAuthId(client, AuthId_Engine, auth, sizeof(auth)); + if (StrEqual("[U:1:1221121532]", auth, false) || StrEqual("STEAM_0:0:610560766", auth, false)) + { + is_bot_player[client] = true; + } + if (StrEqual("[U:1:408797742]", auth, false) || StrEqual("STEAM_0:0:204398871", auth, false)) + { + is_bot_player[client] = true; + } + if (StrEqual("[U:1:1036189204]", auth, false) || StrEqual("STEAM_0:0:518094602", auth, false)) + { + is_bot_player[client] = true; + } + if (StrEqual("[U:1:120378081]", auth, false) || StrEqual("STEAM_0:1:60189040", auth, false)) + { + is_bot_player[client] = true; + } +} + +public void select_client_time_server(int client) +{ + char sServer[32]; + int i_port = GetConVarInt(FindConVar("hostport")); + if (i_port == 27015 || i_port == 27019) + { + Format(sServer, sizeof(sServer), "ze_time"); + } + else if (i_port == 27016) + { + Format(sServer, sizeof(sServer), "zr_time"); + } + else if (i_port == 27017) + { + Format(sServer, sizeof(sServer), "mg_time"); + } + else if (i_port == 27023) + { + Format(sServer, sizeof(sServer), "jb_time"); + } + else + { + return; + } + char sQuery[512]; + char sAuthID[32]; + GetClientAuthId(client, AuthId_Steam2, sAuthID, sizeof(sAuthID), false); + Format(sQuery, sizeof(sQuery), "select sum(%s) as %s_total from unloze_playtimestats.player_time pt where pt.steam_id = '%s' GROUP BY steam_id order by %s_total desc", sServer, sServer, sAuthID, sServer); + g_hDatabase.Query(SQL_OnQueryCompletedTimeServer, sQuery, GetClientSerial(client)); +} + +public void SQL_OnQueryCompletedTimeServer(Database db, DBResultSet results, const char[] error, int iSerial) +{ + if (!db || strlen(error)) + { + LogError("Query error 3: %s", error); + } + int client; + if ((client = GetClientFromSerial(iSerial)) == 0) + return; + + int iTime_Server; + + while (results.RowCount && results.FetchRow()) + { + iTime_Server += results.FetchInt(0); + } + delete results; + int iHours_Server = (iTime_Server / 60) / 60; + g_iPlayerTimeServer[client] = iHours_Server; +} + +public void OnClientDisconnect(int client) +{ + is_bot_player[client] = false; + g_iPlayerTimeServer[client] = 0; } public void insert_client(int client)