added new index to session table and moved queries some around to be chained

This commit is contained in:
jenz
2026-08-21 12:32:28 +02:00
parent e1a93bd0dd
commit b21a6b6e0b
2 changed files with 64 additions and 40 deletions
@@ -7,7 +7,6 @@
#include <cstrike>
Database g_hDatabase;
bool g_bInsertedPlayerSession[MAXPLAYERS + 1];
bool g_bCorrectServer = false;
int g_iClientHours[MAXPLAYERS + 1];
@@ -39,14 +38,6 @@ public void OnPluginStart()
}
g_hPendingVoteChoices = new ArrayList(sizeof(VoteChoiceRecord));
g_bCorrectServer = GetConVarInt(FindConVar("hostport")) == 27015 ? true : false;
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i) && !IsFakeClient(i))
{
g_bInsertedPlayerSession[i] = false;
}
}
}
public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
@@ -73,6 +64,19 @@ public void OnMapStart()
return;
}
char sQuery[512];
Format(sQuery, sizeof(sQuery), "UPDATE `playtime_display_map_history` set map_end_dt = NOW() order by map_id desc limit 1");
g_hDatabase.Query(SQL_FinishedQueryUpdateMapHistory, sQuery, _, DBPrio_Normal);
}
public void SQL_FinishedQueryUpdateMapHistory(Database db, DBResultSet results, const char[] error, any data)
{
if (!db || strlen(error))
{
LogError("Query error 3: %s", error);
}
delete results;
char sMapname[64];
GetCurrentMap(sMapname, sizeof(sMapname));
insert_map(sMapname);
@@ -85,22 +89,6 @@ public void insert_map(char sMapname[64])
g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_Normal);
}
public void OnMapEnd()
{
if (!g_bCorrectServer)
{
return;
}
char sQuery[512];
Format(sQuery, sizeof(sQuery), "UPDATE `playtime_display_map_history` set map_end_dt = NOW() order by map_id desc limit 1");
g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_Normal);
}
public void OnClientAuthorized(int client, const char[] sSteamID32)
{
g_bInsertedPlayerSession[client] = false; //the query will take a little to finish for playtime so should be save to set here
}
//forward is called from OnClientAuthorized but also from timer every 1 minute.
public void GetPlayerHoursServer(int client, int hours, int minutes)
{
@@ -108,18 +96,52 @@ public void GetPlayerHoursServer(int client, int hours, int minutes)
{
return;
}
if (g_bInsertedPlayerSession[client])
{
//update the last session entry every time the forward is called, in case of server crashes this still means we have it up to date.
update_play_session(client, hours, minutes);
return;
}
g_iClientHours[client] = hours;
g_iClientMinutes[client] = minutes;
select_user_session(client);
}
public void select_user_session(int client)
{
char sSID[64];
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
char sQuery[512];
Format(sQuery, sizeof(sQuery), "select 1 from `playtime_display_sessions` where steamid = '%s' and session_end_dt > NOW() - INTERVAL 10 MINUTE limit 1", sSID);
g_hDatabase.Query(SQL_FinishedQuerySelectUserSession, sQuery, GetClientSerial(client), DBPrio_Normal);
}
public void SQL_FinishedQuerySelectUserSession(Database db, DBResultSet results, const char[] error, int Serial)
{
if (!db)
{
delete results;
return;
}
int client;
if ((client = GetClientFromSerial(Serial)) == 0)
{
delete results;
return;
}
if (!IsValidClient(client))
{
delete results;
return;
}
if (results.RowCount && results.FetchRow())
{
//active session that we keep updating
update_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
}
else
{
//new session that we insert
//chaining the calls
upsert_player(client);
}
delete results;
}
public void update_play_session(int client, int client_hours, int client_minutes)
@@ -171,6 +193,7 @@ public void SQL_FinishedQueryUpsertPlayerCallback(Database db, DBResultSet resul
delete results;
return;
}
delete results;
upsert_name_history(client);
}
@@ -207,12 +230,12 @@ public void SQL_FinishedQueryUpsertNameHistoryCallback(Database db, DBResultSet
delete results;
return;
}
delete results;
insert_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
}
public void insert_play_session(int client, int hours, int minutes)
{
g_bInsertedPlayerSession[client] = true;
char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, sizeof(sName));
int size2 = 2 * strlen(sName) + 1;
@@ -228,7 +251,7 @@ public void insert_play_session(int client, int hours, int minutes)
char sSID[64];
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
char sQuery[512];
Format(sQuery, sizeof(sQuery), "INSERT INTO `playtime_display_sessions` (`steamid`, `country_code`, `player_name`, `session_start_playtime_minutes`) VALUES('%s', '%s','%s','%i')", sSID, countryCode, sEscapedName, (hours * 60) + minutes);
Format(sQuery, sizeof(sQuery), "INSERT INTO `playtime_display_sessions` (`steamid`, `country_code`, `player_name`, `session_start_playtime_minutes`, `session_end_dt`) VALUES('%s', '%s','%s','%i', CURRENT_TIMESTAMP)", sSID, countryCode, sEscapedName, (hours * 60) + minutes);
g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_Normal);
}
@@ -243,7 +266,7 @@ public void SQL_FinishedQuery(Database db, DBResultSet results, const char[] err
public void OnClientDisconnect(int client)
{
g_bInsertedPlayerSession[client] = false;
update_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
g_iClientHours[client] = 0;
g_iClientMinutes[client] = 0;
}
@@ -60,7 +60,7 @@ CREATE TABLE `playtime_display_map_vote_choices` (
CONSTRAINT `fk_votechoice_steamid` FOREIGN KEY (`steamid`) REFERENCES `playtime_display_players` (`steamid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- unloze_playtimestats.playtime_display_sessions definition
CREATE TABLE `playtime_display_sessions` (
`session_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -79,5 +79,6 @@ CREATE TABLE `playtime_display_sessions` (
KEY `idx_interval` (`session_start_dt`,`session_end_dt`),
KEY `idx_dow_tod` (`session_start_dow`,`session_start_tod`),
KEY `idx_country` (`country_code`),
KEY `idx_steamid_end` (`steamid`,`session_end_dt`),
CONSTRAINT `fk_session_steamid` FOREIGN KEY (`steamid`) REFERENCES `playtime_display_players` (`steamid`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=215 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
) ENGINE=InnoDB AUTO_INCREMENT=1201 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;