added new index to session table and moved queries some around to be chained
This commit is contained in:
@@ -7,7 +7,6 @@
|
|||||||
#include <cstrike>
|
#include <cstrike>
|
||||||
|
|
||||||
Database g_hDatabase;
|
Database g_hDatabase;
|
||||||
bool g_bInsertedPlayerSession[MAXPLAYERS + 1];
|
|
||||||
bool g_bCorrectServer = false;
|
bool g_bCorrectServer = false;
|
||||||
|
|
||||||
int g_iClientHours[MAXPLAYERS + 1];
|
int g_iClientHours[MAXPLAYERS + 1];
|
||||||
@@ -39,14 +38,6 @@ public void OnPluginStart()
|
|||||||
}
|
}
|
||||||
g_hPendingVoteChoices = new ArrayList(sizeof(VoteChoiceRecord));
|
g_hPendingVoteChoices = new ArrayList(sizeof(VoteChoiceRecord));
|
||||||
g_bCorrectServer = GetConVarInt(FindConVar("hostport")) == 27015 ? true : false;
|
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)
|
public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
|
||||||
@@ -73,6 +64,19 @@ public void OnMapStart()
|
|||||||
return;
|
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];
|
char sMapname[64];
|
||||||
GetCurrentMap(sMapname, sizeof(sMapname));
|
GetCurrentMap(sMapname, sizeof(sMapname));
|
||||||
insert_map(sMapname);
|
insert_map(sMapname);
|
||||||
@@ -85,22 +89,6 @@ public void insert_map(char sMapname[64])
|
|||||||
g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_Normal);
|
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.
|
//forward is called from OnClientAuthorized but also from timer every 1 minute.
|
||||||
public void GetPlayerHoursServer(int client, int hours, int minutes)
|
public void GetPlayerHoursServer(int client, int hours, int minutes)
|
||||||
{
|
{
|
||||||
@@ -108,19 +96,53 @@ public void GetPlayerHoursServer(int client, int hours, int minutes)
|
|||||||
{
|
{
|
||||||
return;
|
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_iClientHours[client] = hours;
|
||||||
g_iClientMinutes[client] = minutes;
|
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
|
//chaining the calls
|
||||||
upsert_player(client);
|
upsert_player(client);
|
||||||
}
|
}
|
||||||
|
delete results;
|
||||||
|
}
|
||||||
|
|
||||||
public void update_play_session(int client, int client_hours, int client_minutes)
|
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;
|
delete results;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
delete results;
|
||||||
upsert_name_history(client);
|
upsert_name_history(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,12 +230,12 @@ public void SQL_FinishedQueryUpsertNameHistoryCallback(Database db, DBResultSet
|
|||||||
delete results;
|
delete results;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
delete results;
|
||||||
insert_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
|
insert_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insert_play_session(int client, int hours, int minutes)
|
public void insert_play_session(int client, int hours, int minutes)
|
||||||
{
|
{
|
||||||
g_bInsertedPlayerSession[client] = true;
|
|
||||||
char sName[MAX_NAME_LENGTH];
|
char sName[MAX_NAME_LENGTH];
|
||||||
GetClientName(client, sName, sizeof(sName));
|
GetClientName(client, sName, sizeof(sName));
|
||||||
int size2 = 2 * strlen(sName) + 1;
|
int size2 = 2 * strlen(sName) + 1;
|
||||||
@@ -228,7 +251,7 @@ public void insert_play_session(int client, int hours, int minutes)
|
|||||||
char sSID[64];
|
char sSID[64];
|
||||||
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
|
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
|
||||||
char sQuery[512];
|
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);
|
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)
|
public void OnClientDisconnect(int client)
|
||||||
{
|
{
|
||||||
g_bInsertedPlayerSession[client] = false;
|
update_play_session(client, g_iClientHours[client], g_iClientMinutes[client]);
|
||||||
g_iClientHours[client] = 0;
|
g_iClientHours[client] = 0;
|
||||||
g_iClientMinutes[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
|
CONSTRAINT `fk_votechoice_steamid` FOREIGN KEY (`steamid`) REFERENCES `playtime_display_players` (`steamid`) ON DELETE CASCADE
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
-- unloze_playtimestats.playtime_display_sessions definition
|
|
||||||
|
|
||||||
CREATE TABLE `playtime_display_sessions` (
|
CREATE TABLE `playtime_display_sessions` (
|
||||||
`session_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
`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_interval` (`session_start_dt`,`session_end_dt`),
|
||||||
KEY `idx_dow_tod` (`session_start_dow`,`session_start_tod`),
|
KEY `idx_dow_tod` (`session_start_dow`,`session_start_tod`),
|
||||||
KEY `idx_country` (`country_code`),
|
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
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user