From e53aa439d5d66254cd868af82730a9e69a2f1112 Mon Sep 17 00:00:00 2001 From: Sikari <37634135+Sikarii@users.noreply.github.com> Date: Sun, 6 Nov 2022 04:04:41 +0200 Subject: [PATCH 1/5] Refactor sm_updatemappool to use transactions --- .../scripting/gokz-localranks/db/sql.sp | 6 +- .../db/update_ranked_map_pool.sp | 114 ++++++++---------- 2 files changed, 55 insertions(+), 65 deletions(-) diff --git a/addons/sourcemod/scripting/gokz-localranks/db/sql.sp b/addons/sourcemod/scripting/gokz-localranks/db/sql.sp index e0b07776..f768e9ad 100644 --- a/addons/sourcemod/scripting/gokz-localranks/db/sql.sp +++ b/addons/sourcemod/scripting/gokz-localranks/db/sql.sp @@ -17,16 +17,16 @@ ALTER TABLE Maps \ char sqlite_maps_insertranked[] = "\ INSERT OR IGNORE INTO Maps \ (InRankedPool, Name) \ - VALUES %s"; + VALUES (%d, '%s')"; char sqlite_maps_updateranked[] = "\ UPDATE OR IGNORE Maps \ SET InRankedPool=%d \ - WHERE Name IN (%s)"; + WHERE Name = '%s'"; char mysql_maps_upsertranked[] = "\ INSERT INTO Maps (InRankedPool, Name) \ - VALUES %s \ + VALUES (%d, '%s') \ ON DUPLICATE KEY UPDATE \ InRankedPool=VALUES(InRankedPool)"; diff --git a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp index 26f2b4ec..f3a0ec70 100644 --- a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp +++ b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp @@ -7,7 +7,18 @@ void DB_UpdateRankedMapPool(int client) { - Handle file = OpenFile(LR_CFG_MAP_POOL, "r"); + if (g_DBType != DatabaseType_SQLite && g_DBType != DatabaseType_MySQL) + { + LogError("Unsupported database type, cannot update map pool"); + if (IsValidClient(client)) + { + // TODO Translation phrases? + GOKZ_PrintToChat(client, true, "{grey}There was a problem updating the map pool.", LR_CFG_MAP_POOL); + } + return; + } + + File file = OpenFile(LR_CFG_MAP_POOL, "r"); if (file == null) { LogError("Failed to load file: \"%s\".", LR_CFG_MAP_POOL); @@ -20,82 +31,63 @@ void DB_UpdateRankedMapPool(int client) } char map[33]; - ArrayList maps = new ArrayList(33, 0); + int mapsCount = 0; + + Transaction txn = new Transaction(); + + // Reset all maps to be unranked + txn.AddQuery(sql_maps_reset_mappool); // Insert/Update maps in gokz-localranks-mappool.cfg to be ranked - while (ReadFileLine(file, map, sizeof(map))) + while (file.ReadLine(map, sizeof(map))) { TrimString(map); + String_ToLower(map, map, sizeof(map)); + + // Ignore blank lines and comments if (map[0] == '\0' || map[0] == ';' || (map[0] == '/' && map[1] == '/')) { continue; } - String_ToLower(map, map, sizeof(map)); - maps.PushString(map); - } - delete file; - if (maps.Length == 0) - { - if (client == 0) - { - PrintToServer("No maps found in file '%s'.", LR_CFG_MAP_POOL); - } - else + mapsCount++; + + switch (g_DBType) { - // TODO Translation phrases? - GOKZ_PrintToChat(client, true, "{darkred}No maps found in file '%s'.", LR_CFG_MAP_POOL); - GOKZ_PlayErrorSound(client); - } - return; - } + case DatabaseType_SQLite: + { + char updateQuery[256]; + gH_DB.Format(updateQuery, sizeof(updateQuery), sqlite_maps_updateranked, 1, map); - // Create VALUES e.g. (1,'kz_map1'),(1,'kz_map2') - int valuesSize = maps.Length * 40; - char[] values = new char[valuesSize]; - maps.GetString(0, map, sizeof(map)); - FormatEx(values, valuesSize, "(1,'%s')", map); - for (int i = 1; i < maps.Length; i++) - { - maps.GetString(i, map, sizeof(map)); - Format(values, valuesSize, "%s,(1,'%s')", values, map); - } + char insertQuery[256]; + gH_DB.Format(insertQuery, sizeof(insertQuery), sqlite_maps_insertranked, 1, map); - // Create query - int querySize = valuesSize + 128; - char[] query = new char[querySize]; + txn.AddQuery(updateQuery); + txn.AddQuery(insertQuery); + } + case DatabaseType_MySQL: + { + char query[256]; + gH_DB.Format(query, sizeof(query), mysql_maps_upsertranked, 1, map); - Transaction txn = SQL_CreateTransaction(); - // Reset all maps to be unranked - txn.AddQuery(sql_maps_reset_mappool); + txn.AddQuery(query); + } + } + } - switch (g_DBType) + delete file; + + if (mapsCount == 0) { - case DatabaseType_SQLite: - { - // Create list of maps e.g. 'kz_map1','kz_map2' - int mapListSize = maps.Length * 36; - char[] mapList = new char[mapListSize]; - maps.GetString(0, map, sizeof(map)); - FormatEx(mapList, mapListSize, "'%s'", map); - for (int i = 1; i < maps.Length; i++) - { - maps.GetString(i, map, sizeof(map)); - Format(mapList, mapListSize, "%s,'%s'", mapList, map); - } + GOKZ_PrintToChatAndLog(client, true, "{darkred}No maps found in file '%s'.", LR_CFG_MAP_POOL); - // UPDATE OR IGNORE - FormatEx(query, querySize, sqlite_maps_updateranked, 1, mapList); - txn.AddQuery(query); - // INSERT OR IGNORE - FormatEx(query, querySize, sqlite_maps_insertranked, values); - txn.AddQuery(query); - } - case DatabaseType_MySQL: + if (IsValidClient(client)) { - FormatEx(query, querySize, mysql_maps_upsertranked, values); - txn.AddQuery(query); + GOKZ_PlayErrorSound(client); } + + delete txn; + return; } // Pass client user ID (or -1) as data @@ -105,9 +97,7 @@ void DB_UpdateRankedMapPool(int client) data = GetClientUserId(client); } - SQL_ExecuteTransaction(gH_DB, txn, DB_TxnSuccess_UpdateRankedMapPool, DB_TxnFailure_Generic, data, DBPrio_Low); - - delete maps; + gH_DB.Execute(txn, DB_TxnSuccess_UpdateRankedMapPool, DB_TxnFailure_Generic, data); } public void DB_TxnSuccess_UpdateRankedMapPool(Handle db, int userid, int numQueries, Handle[] results, any[] queryData) From 4f9ba3cbc3c933382467da5967ccae70c250049c Mon Sep 17 00:00:00 2001 From: Sikari <37634135+Sikarii@users.noreply.github.com> Date: Sun, 6 Nov 2022 08:03:28 +0200 Subject: [PATCH 2/5] Raise buffer size for the lines being read --- .../gokz-localranks/db/update_ranked_map_pool.sp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp index f3a0ec70..84c90b59 100644 --- a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp +++ b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp @@ -30,7 +30,7 @@ void DB_UpdateRankedMapPool(int client) return; } - char map[33]; + char map[256]; int mapsCount = 0; Transaction txn = new Transaction(); @@ -56,10 +56,10 @@ void DB_UpdateRankedMapPool(int client) { case DatabaseType_SQLite: { - char updateQuery[256]; + char updateQuery[512]; gH_DB.Format(updateQuery, sizeof(updateQuery), sqlite_maps_updateranked, 1, map); - char insertQuery[256]; + char insertQuery[512]; gH_DB.Format(insertQuery, sizeof(insertQuery), sqlite_maps_insertranked, 1, map); txn.AddQuery(updateQuery); @@ -67,7 +67,7 @@ void DB_UpdateRankedMapPool(int client) } case DatabaseType_MySQL: { - char query[256]; + char query[512]; gH_DB.Format(query, sizeof(query), mysql_maps_upsertranked, 1, map); txn.AddQuery(query); From f73ba2840d7c4ab20275dd594ecafda6fc92ea75 Mon Sep 17 00:00:00 2001 From: Sikari <37634135+Sikarii@users.noreply.github.com> Date: Sun, 6 Nov 2022 08:04:06 +0200 Subject: [PATCH 3/5] Document maximum length for a single line in the map pool file --- cfg/sourcemod/gokz/gokz-localranks-mappool.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/cfg/sourcemod/gokz/gokz-localranks-mappool.cfg b/cfg/sourcemod/gokz/gokz-localranks-mappool.cfg index 50f5c1ad..8a8032fe 100644 --- a/cfg/sourcemod/gokz/gokz-localranks-mappool.cfg +++ b/cfg/sourcemod/gokz/gokz-localranks-mappool.cfg @@ -1,4 +1,5 @@ // Map list read by the gokz-localranks !updatemappool command +// The maximum supported length for a single line is 255 characters // You can comment like this ; or you can comment like this From 58f351237cb785144de508cb495c636b9dbd5316 Mon Sep 17 00:00:00 2001 From: Sikari <37634135+Sikarii@users.noreply.github.com> Date: Sat, 26 Nov 2022 18:42:02 +0200 Subject: [PATCH 4/5] Remove assertion for database type --- .../gokz-localranks/db/update_ranked_map_pool.sp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp index 84c90b59..72c1d0a1 100644 --- a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp +++ b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp @@ -7,17 +7,6 @@ void DB_UpdateRankedMapPool(int client) { - if (g_DBType != DatabaseType_SQLite && g_DBType != DatabaseType_MySQL) - { - LogError("Unsupported database type, cannot update map pool"); - if (IsValidClient(client)) - { - // TODO Translation phrases? - GOKZ_PrintToChat(client, true, "{grey}There was a problem updating the map pool.", LR_CFG_MAP_POOL); - } - return; - } - File file = OpenFile(LR_CFG_MAP_POOL, "r"); if (file == null) { From ac92b3e2b7ae34ea4f52f13316469834c053a2fa Mon Sep 17 00:00:00 2001 From: Sikari <37634135+Sikarii@users.noreply.github.com> Date: Sat, 26 Nov 2022 18:42:28 +0200 Subject: [PATCH 5/5] Add translations for ranked map pool messages --- .../gokz-localranks/db/update_ranked_map_pool.sp | 11 +++++------ .../translations/gokz-localranks.phrases.txt | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp index 72c1d0a1..ee9bd6dd 100644 --- a/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp +++ b/addons/sourcemod/scripting/gokz-localranks/db/update_ranked_map_pool.sp @@ -10,11 +10,10 @@ void DB_UpdateRankedMapPool(int client) File file = OpenFile(LR_CFG_MAP_POOL, "r"); if (file == null) { - LogError("Failed to load file: \"%s\".", LR_CFG_MAP_POOL); + LogError("Failed to load file: '%s'.", LR_CFG_MAP_POOL); if (IsValidClient(client)) { - // TODO Translation phrases? - GOKZ_PrintToChat(client, true, "{grey}There was a problem opening file '%s'.", LR_CFG_MAP_POOL); + GOKZ_PrintToChat(client, true, "%t", "Ranked Map Pool - Error"); } return; } @@ -68,10 +67,11 @@ void DB_UpdateRankedMapPool(int client) if (mapsCount == 0) { - GOKZ_PrintToChatAndLog(client, true, "{darkred}No maps found in file '%s'.", LR_CFG_MAP_POOL); + LogError("No maps found in file: '%s'.", LR_CFG_MAP_POOL); if (IsValidClient(client)) { + GOKZ_PrintToChat(client, true, "%t", "Ranked Map Pool - No Maps In File"); GOKZ_PlayErrorSound(client); } @@ -95,8 +95,7 @@ public void DB_TxnSuccess_UpdateRankedMapPool(Handle db, int userid, int numQuer if (IsValidClient(client)) { LogMessage("The ranked map pool was updated by %L.", client); - // TODO Translation phrases? - GOKZ_PrintToChat(client, true, "{grey}The ranked map pool was updated."); + GOKZ_PrintToChat(client, true, "%t", "Ranked Map Pool - Success"); } else { diff --git a/addons/sourcemod/translations/gokz-localranks.phrases.txt b/addons/sourcemod/translations/gokz-localranks.phrases.txt index ba1e3d73..20cebfa5 100644 --- a/addons/sourcemod/translations/gokz-localranks.phrases.txt +++ b/addons/sourcemod/translations/gokz-localranks.phrases.txt @@ -51,6 +51,18 @@ "chi" "{grey}本服务器没有设定可排名的地图." "ru" "{grey}На этом сервере нет ранжирования по картам." } + "Ranked Map Pool - Error" + { + "en" "{darkred}There was a problem updating the ranked map pool." + } + "Ranked Map Pool - Success" + { + "en" "{grey}The ranked map pool was updated." + } + "Ranked Map Pool - No Maps In File" + { + "en" "{darkred}No maps found in the ranked map pool file." + } "New Record (NUB)" { // Bill set a new NUB RECORD [Mode]