summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-localranks/db/helpers.sp
blob: 670a42071379865de941adc66ee1555b81896704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
	Database helper functions and callbacks.
*/



/* Error report callback for failed transactions */
public void DB_TxnFailure_Generic(Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
{
	LogError("Database transaction error: %s", error);
}

/* Error report callback for failed transactions which deletes the DataPack */
public void DB_TxnFailure_Generic_DataPack(Handle db, DataPack data, int numQueries, const char[] error, int failIndex, any[] queryData)
{
	delete data;
	LogError("Database transaction error: %s", error);
}

/*	Used to search the database for a player name and return their PlayerID and alias

	For SQLTxnSuccess onSuccess:
	results[0] - 0:PlayerID, 1:Alias
*/
void DB_FindPlayer(const char[] playerSearch, SQLTxnSuccess onSuccess, any data = 0, DBPriority priority = DBPrio_Normal)
{
	char query[1024], playerEscaped[MAX_NAME_LENGTH * 2 + 1];
	SQL_EscapeString(gH_DB, playerSearch, playerEscaped, sizeof(playerEscaped));
	
	String_ToLower(playerEscaped, playerEscaped, sizeof(playerEscaped));
	
	Transaction txn = SQL_CreateTransaction();
	
	// Look for player name and retrieve their PlayerID
	FormatEx(query, sizeof(query), sql_players_searchbyalias, playerEscaped, playerEscaped);
	txn.AddQuery(query);
	
	SQL_ExecuteTransaction(gH_DB, txn, onSuccess, DB_TxnFailure_Generic, data, priority);
}

/*	Used to search the database for a map name and return its MapID and name

	For SQLTxnSuccess onSuccess:
	results[0] - 0:MapID, 1:Name
*/
void DB_FindMap(const char[] mapSearch, SQLTxnSuccess onSuccess, any data = 0, DBPriority priority = DBPrio_Normal)
{
	char query[1024], mapEscaped[129];
	SQL_EscapeString(gH_DB, mapSearch, mapEscaped, sizeof(mapEscaped));
	
	Transaction txn = SQL_CreateTransaction();
	
	// Look for map name and retrieve it's MapID
	FormatEx(query, sizeof(query), sql_maps_searchbyname, mapEscaped, mapEscaped);
	txn.AddQuery(query);
	
	SQL_ExecuteTransaction(gH_DB, txn, onSuccess, DB_TxnFailure_Generic, data, priority);
}

/*	Used to search the database for a player name and return their PlayerID and alias,
	and search the database for a map name and return its MapID and name
	
	For SQLTxnSuccess onSuccess:
	results[0] - 0:PlayerID, 1:Alias
	results[1] - 0:MapID, 1:Name
*/
void DB_FindPlayerAndMap(const char[] playerSearch, const char[] mapSearch, SQLTxnSuccess onSuccess, any data = 0, DBPriority priority = DBPrio_Normal)
{
	char query[1024], mapEscaped[129], playerEscaped[MAX_NAME_LENGTH * 2 + 1];
	SQL_EscapeString(gH_DB, playerSearch, playerEscaped, sizeof(playerEscaped));
	SQL_EscapeString(gH_DB, mapSearch, mapEscaped, sizeof(mapEscaped));
	
	String_ToLower(playerEscaped, playerEscaped, sizeof(playerEscaped));
	
	Transaction txn = SQL_CreateTransaction();
	
	// Look for player name and retrieve their PlayerID
	FormatEx(query, sizeof(query), sql_players_searchbyalias, playerEscaped, playerEscaped);
	txn.AddQuery(query);
	// Look for map name and retrieve it's MapID
	FormatEx(query, sizeof(query), sql_maps_searchbyname, mapEscaped, mapEscaped);
	txn.AddQuery(query);
	
	SQL_ExecuteTransaction(gH_DB, txn, onSuccess, DB_TxnFailure_Generic, data, priority);
}

// Used to convert the Account ID to the SteamID we can use for a Global API query
int GetSteam2FromAccountId(char[] result, int maxlen, int account_id)
{
	return Format(result, maxlen, "STEAM_1:%d:%d", view_as<bool>(account_id % 2), account_id / 2);
}