Skip to content

Commit 3d0cd96

Browse files
mourningsicknessrtldg
authored andcommitted
Add FirstLogin to database and profile (#1234)
fix FirstLogin query for sqlite fix statsmenu query oopsie teehee :3 sql-create-tables-and-migrations.sp - fix queries for sqlite Simplify firstlogin column initialization
1 parent 354c244 commit 3d0cd96

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

addons/sourcemod/scripting/include/shavit/sql-create-tables-and-migrations.sp

+19-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum
5353
Migration_DeprecateExactTimeInt,
5454
Migration_AddPlayertimesAuthFK,
5555
Migration_FixSQLiteMapzonesROWID,
56+
Migration_AddUsersFirstLogin,
5657
MIGRATIONS_END
5758
};
5859

@@ -87,6 +88,7 @@ char gS_MigrationNames[][] = {
8788
"DeprecateExactTimeInt",
8889
"AddPlayertimesAuthFK",
8990
"FixSQLiteMapzonesROWID",
91+
"AddUsersFirstLogin",
9092
};
9193

9294
static Database gH_SQL;
@@ -133,13 +135,13 @@ public void SQL_CreateTables(Database hSQL, const char[] prefix, int driver)
133135
if (driver == Driver_mysql)
134136
{
135137
FormatEx(sQuery, sizeof(sQuery),
136-
"CREATE TABLE IF NOT EXISTS `%susers` (`auth` INT NOT NULL, `name` VARCHAR(32) COLLATE 'utf8mb4_general_ci', `ip` INT, `lastlogin` INT NOT NULL DEFAULT -1, `points` FLOAT NOT NULL DEFAULT 0, `playtime` FLOAT NOT NULL DEFAULT 0, PRIMARY KEY (`auth`), INDEX `points` (`points`), INDEX `lastlogin` (`lastlogin`)) ENGINE=INNODB;",
138+
"CREATE TABLE IF NOT EXISTS `%susers` (`auth` INT NOT NULL, `name` VARCHAR(32) COLLATE 'utf8mb4_general_ci', `ip` INT, `lastlogin` INT NOT NULL DEFAULT -1, `firstlogin` INT NOT NULL DEFAULT -1, `points` FLOAT NOT NULL DEFAULT 0, `playtime` FLOAT NOT NULL DEFAULT 0, PRIMARY KEY (`auth`), INDEX `points` (`points`), INDEX `lastlogin` (`lastlogin`)) ENGINE=INNODB;",
137139
gS_SQLPrefix);
138140
}
139141
else
140142
{
141143
FormatEx(sQuery, sizeof(sQuery),
142-
"CREATE TABLE IF NOT EXISTS `%susers` (`auth` INT NOT NULL PRIMARY KEY, `name` VARCHAR(32), `ip` INT, `lastlogin` INTEGER NOT NULL DEFAULT -1, `points` FLOAT NOT NULL DEFAULT 0, `playtime` FLOAT NOT NULL DEFAULT 0);",
144+
"CREATE TABLE IF NOT EXISTS `%susers` (`auth` INT NOT NULL PRIMARY KEY, `name` VARCHAR(32), `ip` INT, `lastlogin` INTEGER NOT NULL DEFAULT -1, `firstlogin` INTEGER NOT NULL DEFAULT -1, `points` FLOAT NOT NULL DEFAULT 0, `playtime` FLOAT NOT NULL DEFAULT 0);",
143145
gS_SQLPrefix);
144146
}
145147

@@ -369,6 +371,7 @@ void ApplyMigration(int migration)
369371
case Migration_DeprecateExactTimeInt: ApplyMigration_DeprecateExactTimeInt();
370372
case Migration_AddPlayertimesAuthFK: ApplyMigration_AddPlayertimesAuthFK();
371373
case Migration_FixSQLiteMapzonesROWID: ApplyMigration_FixSQLiteMapzonesROWID();
374+
case Migration_AddUsersFirstLogin: ApplyMigration_AddUsersFirstLogin();
372375
}
373376
}
374377

@@ -686,6 +689,20 @@ public void Trans_FixSQLiteMapzonesROWID_Error(Database db, any data, int numQue
686689
LogError("Timer error! SQLiteMapzonesROWID migration transaction failed. Reason: %s", error);
687690
}
688691

692+
void ApplyMigration_AddUsersFirstLogin()
693+
{
694+
char sQuery[256];
695+
FormatEx(sQuery, sizeof(sQuery), "ALTER TABLE %susers ADD `firstlogin` INT NOT NULL DEFAULT -1 %s;", gS_SQLPrefix, (gI_Driver == Driver_mysql) ? "AFTER `lastlogin`" : "");
696+
QueryLog(gH_SQL, ApplyMigration_AddUsersFirstLogin2222222_Callback, sQuery, Migration_AddUsersFirstLogin, DBPrio_High);
697+
}
698+
699+
public void ApplyMigration_AddUsersFirstLogin2222222_Callback(Database db, DBResultSet results, const char[] error, any data)
700+
{
701+
char sQuery[256];
702+
FormatEx(sQuery, sizeof(sQuery), "UPDATE %susers SET firstlogin = lastlogin WHERE lastlogin > 0;", gS_SQLPrefix);
703+
QueryLog(gH_SQL, SQL_TableMigrationSingleQuery_Callback, sQuery, Migration_AddUsersFirstLogin, DBPrio_High);
704+
}
705+
689706
public void SQL_TableMigrationSingleQuery_Callback(Database db, DBResultSet results, const char[] error, any data)
690707
{
691708
InsertMigration(data);

addons/sourcemod/scripting/shavit-core.sp

+4-4
Original file line numberDiff line numberDiff line change
@@ -2807,14 +2807,14 @@ public void OnClientAuthorized(int client, const char[] auth)
28072807
if (gI_Driver == Driver_mysql)
28082808
{
28092809
FormatEx(sQuery, 512,
2810-
"INSERT INTO %susers (auth, name, ip, lastlogin) VALUES (%d, '%s', %d, %d) ON DUPLICATE KEY UPDATE name = '%s', ip = %d, lastlogin = %d;",
2811-
gS_MySQLPrefix, iSteamID, sEscapedName, iIPAddress, iTime, sEscapedName, iIPAddress, iTime);
2810+
"INSERT INTO %susers (auth, name, ip, lastlogin, firstlogin) VALUES (%d, '%s', %d, %d, %d) ON DUPLICATE KEY UPDATE name = '%s', ip = %d, lastlogin = %d;",
2811+
gS_MySQLPrefix, iSteamID, sEscapedName, iIPAddress, iTime, iTime, sEscapedName, iIPAddress, iTime);
28122812
}
28132813
else // postgresql & sqlite
28142814
{
28152815
FormatEx(sQuery, 512,
2816-
"INSERT INTO %susers (auth, name, ip, lastlogin) VALUES (%d, '%s', %d, %d) ON CONFLICT(auth) DO UPDATE SET name = '%s', ip = %d, lastlogin = %d;",
2817-
gS_MySQLPrefix, iSteamID, sEscapedName, iIPAddress, iTime, sEscapedName, iIPAddress, iTime);
2816+
"INSERT INTO %susers (auth, name, ip, lastlogin, firstlogin) VALUES (%d, '%s', %d, %d, %d) ON CONFLICT(auth) DO UPDATE SET name = '%s', ip = %d, lastlogin = %d;",
2817+
gS_MySQLPrefix, iSteamID, sEscapedName, iIPAddress, iTime, iTime, sEscapedName, iIPAddress, iTime);
28182818
}
28192819

28202820
QueryLog(gH_SQL, SQL_InsertUser_Callback, sQuery, GetClientSerial(client));

addons/sourcemod/scripting/shavit-stats.sp

+13-8
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,9 @@ Action OpenStatsMenu_Main(int steamid, int style, DataPack data)
890890
char sQuery[2048];
891891

892892
FormatEx(sQuery, sizeof(sQuery),
893-
"SELECT 0, points, lastlogin, ip, playtime, name FROM %susers WHERE auth = %d\n" ...
894-
"UNION ALL SELECT 1, SUM(playtime), 0, 0, 0, '' FROM %sstyleplaytime WHERE auth = %d AND style = %d\n" ...
895-
"UNION ALL SELECT 2, COUNT(*), 0, 0, 0, '' FROM %susers u1\n" ...
893+
"SELECT 0, points, lastlogin, firstlogin, ip, playtime, name FROM %susers WHERE auth = %d\n" ...
894+
"UNION ALL SELECT 1, SUM(playtime), 0, 0, 0, 0, '' FROM %sstyleplaytime WHERE auth = %d AND style = %d\n" ...
895+
"UNION ALL SELECT 2, COUNT(*), 0, 0, 0, 0, '' FROM %susers u1\n" ...
896896
" JOIN (SELECT points FROM %susers WHERE auth = %d) u2\n" ...
897897
" WHERE u1.points >= u2.points",
898898
gS_MySQLPrefix, steamid,
@@ -960,6 +960,7 @@ public void OpenStatsMenuCallback(Database db, DBResultSet results, const char[]
960960

961961
float fPoints;
962962
char sLastLogin[32];
963+
char sFirstLogin[32];
963964
char sCountry[64];
964965
char sPlaytime[16];
965966

@@ -989,7 +990,11 @@ public void OpenStatsMenuCallback(Database db, DBResultSet results, const char[]
989990
FormatTime(sLastLogin, 32, "%Y-%m-%d %H:%M:%S", iLastLogin);
990991
Format(sLastLogin, 32, "%T: %s", "LastLogin", client, (iLastLogin != -1)? sLastLogin:"N/A");
991992

992-
int iIPAddress = results.FetchInt(3);
993+
int iFirstLogin = results.FetchInt(3);
994+
FormatTime(sFirstLogin, 32, "%Y-%m-%d %H:%M:%S", iFirstLogin);
995+
Format(sFirstLogin, 32, "%T: %s", "FirstLogin", client, (iFirstLogin != -1)? sFirstLogin:"N/A");
996+
997+
int iIPAddress = results.FetchInt(4);
993998
char sIPAddress[32];
994999
IPAddressToString(iIPAddress, sIPAddress, 32);
9951000

@@ -998,10 +1003,10 @@ public void OpenStatsMenuCallback(Database db, DBResultSet results, const char[]
9981003
sCountry = "Local Area Network";
9991004
}
10001005

1001-
float fPlaytime = results.FetchFloat(4);
1006+
float fPlaytime = results.FetchFloat(5);
10021007
FormatSeconds(fPlaytime, sPlaytime, sizeof(sPlaytime), false, true, true);
10031008

1004-
results.FetchString(5, gS_TargetName[client], MAX_NAME_LENGTH);
1009+
results.FetchString(6, gS_TargetName[client], MAX_NAME_LENGTH);
10051010
ReplaceString(gS_TargetName[client], MAX_NAME_LENGTH, "#", "?");
10061011
}
10071012
else if (type == 1)
@@ -1056,8 +1061,8 @@ public void OpenStatsMenuCallback(Database db, DBResultSet results, const char[]
10561061
}
10571062

10581063
Menu menu = new Menu(MenuHandler_ProfileHandler);
1059-
menu.SetTitle("%s's %T. [U:1:%u]\n%T: %s\n%s\n%s\n%T: %s\n",
1060-
gS_TargetName[client], "Profile", client, gI_TargetSteamID[client], "Country", client, sCountry, sLastLogin,
1064+
menu.SetTitle("%s's %T. [U:1:%u]\n%T: %s\n%s\n%s\n%s\n%T: %s\n",
1065+
gS_TargetName[client], "Profile", client, gI_TargetSteamID[client], "Country", client, sCountry, sFirstLogin, sLastLogin,
10611066
sRankingString, "Playtime", client, sPlaytime);
10621067

10631068
int[] styles = new int[gI_Styles];

addons/sourcemod/translations/shavit-stats.phrases.txt

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
{
7575
"en" "Last Login"
7676
}
77+
"FirstLogin"
78+
{
79+
"en" "First Login"
80+
}
7781
"MapCompletions"
7882
{
7983
"en" "Map completions"

0 commit comments

Comments
 (0)