Skip to content

Commit

Permalink
Merge pull request #406 from Sikarii/refactor-update-mappool
Browse files Browse the repository at this point in the history
Refactor sm_updatemappool to use transactions
  • Loading branch information
zealain authored Jan 10, 2023
2 parents eb45102 + ac92b3e commit 3286584
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 71 deletions.
6 changes: 3 additions & 3 deletions addons/sourcemod/scripting/gokz-localranks/db/sql.sp
Original file line number Diff line number Diff line change
Expand Up @@ -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)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,76 @@

void DB_UpdateRankedMapPool(int client)
{
Handle file = OpenFile(LR_CFG_MAP_POOL, "r");
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;
}

char map[33];
ArrayList maps = new ArrayList(33, 0);
char map[256];
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[512];
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[512];
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[512];
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);
}
}
}

delete file;

switch (g_DBType)
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);
}
LogError("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_PrintToChat(client, true, "%t", "Ranked Map Pool - No Maps In File");
GOKZ_PlayErrorSound(client);
}

delete txn;
return;
}

// Pass client user ID (or -1) as data
Expand All @@ -105,9 +86,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)
Expand All @@ -116,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
{
Expand Down
12 changes: 12 additions & 0 deletions addons/sourcemod/translations/gokz-localranks.phrases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions cfg/sourcemod/gokz/gokz-localranks-mappool.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3286584

Please sign in to comment.