-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_threaded_query.pwn
More file actions
69 lines (57 loc) · 2.06 KB
/
Copy path02_threaded_query.pwn
File metadata and controls
69 lines (57 loc) · 2.06 KB
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
// 02_threaded_query.pwn — non-blocking SELECT with a callback, FIFO-ordered.
//
// mysql_query() runs the query on a worker thread and dispatches the result
// to the named callback. Inside the callback the result is the *active cache*
// — read it with the cache_* natives. The cache is freed automatically when
// the callback returns (use cache_save() to keep it longer).
//
// Format spec for the variadic args: each letter maps to one extra param.
// "d" int, "f" float, "s" string. Order must match the callback signature.
#include <a_samp>
#include <mysql_samp>
#define MYSQL_HOST "127.0.0.1"
#define MYSQL_USER "samp"
#define MYSQL_PASSWORD "secret"
#define MYSQL_DATABASE "samp_server"
new g_MysqlConn = 0;
public OnGameModeInit()
{
g_MysqlConn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
return 1;
}
public OnPlayerConnect(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
new query[256];
mysql_format(g_MysqlConn, query, sizeof(query),
"SELECT id, money, score FROM players WHERE name = '%e' LIMIT 1",
name);
// Pass playerid through to the callback so we know which session to apply
// the result to. FIFO ensures the result lands before any later query for
// the same player.
mysql_query(g_MysqlConn, query, "OnPlayerLoad", "d", playerid);
return 1;
}
forward OnPlayerLoad(playerid);
public OnPlayerLoad(playerid)
{
if (cache_get_row_count() == 0)
{
printf("[mysql] new player, no row yet (id=%d)", playerid);
return 1;
}
new playerDbId = cache_get_value_name_int(0, "id");
new money = cache_get_value_name_int(0, "money");
new score = cache_get_value_name_int(0, "score");
GivePlayerMoney(playerid, money);
SetPlayerScore(playerid, score);
printf("[mysql] loaded player %d (db_id=%d, money=%d, score=%d)",
playerid, playerDbId, money, score);
return 1;
}
public OnGameModeExit()
{
if (g_MysqlConn != 0) mysql_close(g_MysqlConn);
return 1;
}