From 501bfecdbed13d5169f22ad9f31cb16b0d037b77 Mon Sep 17 00:00:00 2001
From: neon <>
Date: Sun, 24 Feb 2019 11:56:01 +0100
Subject: [PATCH] ConnectAnnounce: Update to latest RevEmu API and mysql

---
 ConnectAnnounce/scripting/ConnectAnnounce.sp | 46 +++++++++++++-------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/ConnectAnnounce/scripting/ConnectAnnounce.sp b/ConnectAnnounce/scripting/ConnectAnnounce.sp
index 33b2a773..62344633 100644
--- a/ConnectAnnounce/scripting/ConnectAnnounce.sp
+++ b/ConnectAnnounce/scripting/ConnectAnnounce.sp
@@ -1,10 +1,10 @@
 #pragma semicolon 1
 
 #include <sourcemod>
-#include <connect>
 #include <geoip>
 #include <multicolors>
 #include <clientprefs>
+#include <RevEmuAPI>
 
 #pragma newdecls required
 
@@ -19,6 +19,8 @@ Handle g_hCustomMessageFile2;
 bool g_bHideCsays[MAXPLAYERS + 1] = { false, ... };
 Handle g_hCookieHideCsays = null;
 
+ConVar g_cvHlstatsServerName;
+
 #define MSGLENGTH 100
 
 //----------------------------------------------------------------------------------------------------
@@ -37,20 +39,13 @@ public Plugin myinfo = {
 //----------------------------------------------------------------------------------------------------
 public void OnPluginStart()
 {
+	g_cvHlstatsServerName = CreateConVar("sm_connectannounce_hlstats_server_name", "css-ze", "Hlstats server name to check the rank for");
+	AutoExecConfig(true);
+
 	BuildPath(Path_SM, g_sCustomMessageFile, sizeof(g_sCustomMessageFile), "configs/connect_announce/custom-messages.cfg");
 	BuildPath(Path_SM, g_sDataFile, sizeof(g_sDataFile), "configs/connect_announce/settings.cfg");
 
-	char error[255];
-
-	if (SQL_CheckConfig("hlstatsx"))
-	{
-		g_hDatabase = SQL_Connect("hlstatsx", true, error, sizeof(error));
-	}
-
-	if (g_hDatabase == null)
-	{
-		LogError("Could not connect to database: %s", error);
-	}
+	Database.Connect(OnDatabaseConnect, "hlstatsx");
 
 	RegAdminCmd("sm_joinmsg", Command_JoinMsg, ADMFLAG_CUSTOM1, "Sets a custom message which will be shown upon connecting to the server");
 	RegAdminCmd("sm_resetjoinmsg", Command_ResetJoinMsg, ADMFLAG_CUSTOM1, "Resets your custom connect message");
@@ -62,6 +57,20 @@ public void OnPluginStart()
 	SetCookieMenuItem(MenuHandler_CookieMenu, 0, "Hide Connect Csays");
 }
 
+//----------------------------------------------------------------------------------------------------
+// Purpose:
+//----------------------------------------------------------------------------------------------------
+public void OnDatabaseConnect(Database db, const char[] error, any data)
+{
+	if(db == INVALID_HANDLE || strlen(error) > 0)
+	{
+		LogError("Error connecting to database: %s", error);
+		return;
+	}
+
+	g_hDatabase = db;
+}
+
 //----------------------------------------------------------------------------------------------------
 // Purpose:
 //----------------------------------------------------------------------------------------------------
@@ -228,6 +237,7 @@ public Action Command_JoinMsg(int client, int args)
 			return Plugin_Handled;
 		}
 
+		StripQuotes(sArg);
 
 		if (KvJumpToKey(g_hCustomMessageFile, sAuth, true))
 			KvSetString(g_hCustomMessageFile, "message", sArg);
@@ -365,7 +375,7 @@ public void TQueryCB(Handle owner, Handle rs, const char[] error, any data)
 
 	if(StrContains(sRawMsg, "{NOSTEAM}"))
 	{
-		if(!SteamClientAuthenticated(sAuth))
+		if(!RevEmu_IsPlayerSteam(client))
 			ReplaceString(sRawMsg, sizeof(sRawMsg), "{NOSTEAM}", " <NoSteam>");
 		else
 			ReplaceString(sRawMsg, sizeof(sRawMsg), "{NOSTEAM}", "");
@@ -467,12 +477,18 @@ public void OnClientPostAdminCheck(int client)
 	if(IsFakeClient(client))
 		return;
 
+	if(g_hDatabase == INVALID_HANDLE)
+			return;
+
 	static char sAuth[32];
 
 	GetClientAuthId(client, AuthId_Steam2, sAuth, sizeof(sAuth));
 	strcopy(sAuth, sizeof(sAuth), sAuth[8]);
 
+	char sServer[16];
+	g_cvHlstatsServerName.GetString(sServer, sizeof(sServer));
+
 	char sQuery[512];
-	Format(sQuery, sizeof(sQuery), "SELECT COUNT(*) AS rank FROM hlstats_Players WHERE hlstats_Players.game = 'css-ze' AND hideranking = 0 AND skill > (SELECT skill from hlstats_Players JOIN hlstats_PlayerUniqueIds ON hlstats_Players.playerId = hlstats_PlayerUniqueIds.playerId WHERE uniqueId = '%s' AND hlstats_PlayerUniqueIds.game = 'css-ze')", sAuth);
-	SQL_TQuery(g_hDatabase, TQueryCB, sQuery, GetClientUserId(client));
+	Format(sQuery, sizeof(sQuery), "SELECT COUNT(*) AS rank FROM hlstats_Players WHERE hlstats_Players.game = '%s' AND hideranking = 0 AND skill > (SELECT skill from hlstats_Players JOIN hlstats_PlayerUniqueIds ON hlstats_Players.playerId = hlstats_PlayerUniqueIds.playerId WHERE uniqueId = '%s' AND hlstats_PlayerUniqueIds.game = '%s')", sServer, sAuth, sServer);
+	g_hDatabase.Query(TQueryCB, sQuery, GetClientUserId(client));
 }
\ No newline at end of file