Skip to content

Commit 521231e

Browse files
committed
Prevent auto display rank when player disconnects the server
1 parent 8a8561a commit 521231e

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

scripting/l4d2_simpleplayerstats.sp

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103

104104
Database g_hDatabase = null;
105105
StringMap g_mStatModifiers;
106+
StringMap g_mPlayersInitialized;
107+
108+
//Prepared statements
109+
DBStatement g_hQueryRecordExists = null;
106110

107111
bool g_bPlayerInitialized[MAXPLAYERS + 1] = false;
108112
bool g_bInitializing[MAXPLAYERS + 1] = false;
@@ -199,6 +203,7 @@ public void OnPluginStart()
199203
g_sGameMode = FindConVar("mp_gamemode");
200204
g_sServerName = FindConVar("hostname");
201205
g_mStatModifiers = new StringMap();
206+
g_mPlayersInitialized = new StringMap();
202207

203208
if (!InitDatabase()) {
204209
Error("Could not connect to the database. Please check your database configuration file and make sure everything is configured correctly. (db section name: %s)", DB_CONFIG_NAME);
@@ -566,6 +571,32 @@ bool LoadConfigData() {
566571
return true;
567572
}
568573

574+
public bool PlayerRecordExists(const char[] steamId) {
575+
int count = 0;
576+
577+
if (g_hQueryRecordExists == null) {
578+
char error[255];
579+
g_hQueryRecordExists = SQL_PrepareQuery(g_hDatabase, "SELECT COUNT(1) FROM STATS_PLAYERS s WHERE s.steam_id = ? LIMIT 1", error, sizeof(error));
580+
if (g_hQueryRecordExists == null) {
581+
Error("PlayerRecordExists :: Unable to prepare sql query (Reason: %s)", error);
582+
return false;
583+
}
584+
}
585+
586+
SQL_BindParamString(g_hQueryRecordExists, 0, steamId, false);
587+
588+
if (!SQL_Execute(g_hQueryRecordExists)) {
589+
Error("Unable to execute query for PlayerRecordExists");
590+
return false;
591+
}
592+
593+
if (SQL_FetchRow(g_hQueryRecordExists)) {
594+
count = SQL_FetchInt(g_hQueryRecordExists, 0);
595+
}
596+
597+
return count > 0;
598+
}
599+
569600
public int GetStatModifierCount() {
570601
int count = 0;
571602
DBResultSet query = SQL_Query(g_hDatabase, "SELECT COUNT(1) FROM STATS_SKILLS");
@@ -679,7 +710,13 @@ public Action Event_PlayerConnect(Event event, const char[] name, bool dontBroad
679710

680711
if (!isBot) {
681712
int client = GetClientOfUserId(userid);
682-
Debug("\n\nPLAYER_CONNECT_EVENT :: Name = %s, Steam ID: %s, IP: %s, Slot: %i, User ID: %i, Is Bot: %i, Client ID: %i\n\n", playerName, steamId, ipAddress, slot, userid, isBot, client);
713+
Debug("\n\nPLAYER_CONNECT_EVENT :: Name = %s, Steam ID: %s, IP: %s, Slot: %i, User ID: %i, Is Bot: %i, Client ID: %i\n\n", playerName, steamId, ipAddress, slot, userid, isBot, client);
714+
if (PlayerRecordExists(steamId)) {
715+
Info("Found existing record for player '%s' (%s)", playerName, steamId);
716+
} else {
717+
Debug("\n\nNo player record found for %s (%s)", playerName, steamId);
718+
}
719+
683720
//InitializePlayer(client, true);
684721
}
685722
}
@@ -1741,6 +1778,74 @@ public void InitializePlayer(int client, bool updateJoinDateIfExists) {
17411778
g_hDatabase.Query(TQ_InitializePlayer, query, client);
17421779
}
17431780

1781+
public void InitializePlayerById(const char[] name, const char[] steamId, bool updateJoinDateIfExists) {
1782+
Debug("Initializing Player %s (%s)", name, steamId);
1783+
1784+
//unnecessary?
1785+
int len = strlen(steamId) * 2 + 1;
1786+
char[] qSteamId = new char[len];
1787+
SQL_EscapeString(g_hDatabase, steamId, qSteamId, len);
1788+
1789+
len = strlen(name) * 2 + 1;
1790+
char[] qName = new char[len];
1791+
SQL_EscapeString(g_hDatabase, name, qName, len);
1792+
1793+
char query[512];
1794+
1795+
if (updateJoinDateIfExists) {
1796+
Debug("InitializePlayerById :: Join date will be updated for %s", name);
1797+
FormatEx(query, sizeof(query), "INSERT INTO STATS_PLAYERS (steam_id, last_known_alias, last_join_date) VALUES ('%s', '%s', CURRENT_TIMESTAMP()) ON DUPLICATE KEY UPDATE last_join_date = CURRENT_TIMESTAMP(), last_known_alias = '%s'", qSteamId, qName, qName);
1798+
}
1799+
else {
1800+
Debug("InitializePlayerById :: Join date will NOT be updated for %s", name);
1801+
FormatEx(query, sizeof(query), "INSERT INTO STATS_PLAYERS (steam_id, last_known_alias, last_join_date) VALUES ('%s', '%s', CURRENT_TIMESTAMP()) ON DUPLICATE KEY UPDATE last_known_alias = '%s'", qSteamId, qName, qName);
1802+
}
1803+
1804+
DataPack pack = new DataPack();
1805+
pack.WriteString(name);
1806+
pack.WriteString(steamId);
1807+
1808+
//g_bInitializing[client] = true;
1809+
g_hDatabase.Query(TQ_InitializePlayerBySteamId, query, pack);
1810+
}
1811+
1812+
/**
1813+
* SQL Callback for InitializePlayer threaded query
1814+
*/
1815+
public void TQ_InitializePlayerBySteamId(Database db, DBResultSet results, const char[] error, DataPack pack) {
1816+
1817+
char name[MAX_NAME_LENGTH];
1818+
char steamId[MAX_STEAMAUTH_LENGTH];
1819+
1820+
pack.Reset();
1821+
pack.ReadString(name, sizeof(name));
1822+
pack.ReadString(steamId, sizeof(steamId));
1823+
1824+
if (results == null) {
1825+
Error("TQ_InitializePlayerBySteamId :: Query failed (Reason: %s)", error);
1826+
//g_bPlayerInitialized[client] = false;
1827+
//g_bInitializing[client] = false;
1828+
return;
1829+
}
1830+
1831+
if (results.AffectedRows == 0) {
1832+
Debug("TQ_InitializePlayerBySteamId :: Nothing was updated for player %s (%s)", name, steamId);
1833+
}
1834+
else if (results.AffectedRows == 1) {
1835+
Debug("TQ_InitializePlayerBySteamId :: Player %s (%s) has been initialized for the first time", name, steamId);
1836+
}
1837+
else if (results.AffectedRows > 1) {
1838+
Debug("TQ_InitializePlayerBySteamId :: Existing record has been updated for player %s (%s)", name, steamId);
1839+
}
1840+
1841+
//g_bPlayerInitialized[client] = true;
1842+
//g_bInitializing[client] = false;
1843+
1844+
g_mPlayersInitialized.SetValue(steamId, true);
1845+
1846+
Debug("Player %s (%s) successfully initialized", name, steamId);
1847+
}
1848+
17441849
/**
17451850
* SQL Callback for InitializePlayer threaded query
17461851
*/
@@ -2033,7 +2138,7 @@ public Action Event_PlayerTeam(Event event, const char[] name, bool dontBroadcas
20332138
event.GetString("name", playerName, sizeof(playerName));
20342139

20352140
//Only display the rank panel if the player has completed transitioning to a team
2036-
if (IS_VALID_CLIENT(clientId) && !isBot) {
2141+
if (IS_VALID_CLIENT(clientId) && !isBot && !disconnect) {
20372142
Debug("Player %N has joined a team (old team = %i, new team = %i, disconnect = %i, bot = %i)", clientId, oldTeamId, newTeamId, disconnect, isBot);
20382143
if (ShowRankOnConnect() && !PlayerRankShown(clientId) && IS_VALID_HUMAN(clientId)) {
20392144
char steamId[MAX_STEAMAUTH_LENGTH];

0 commit comments

Comments
 (0)