· 4 years ago · Apr 04, 2021, 03:32 PM
1#include <a_samp>
2
3// Suppose USER_ID and USER_NICKNAME are already associated with the database, that is to say, you already have a login/register system, so I only show you how to store skin information when the player exits and how to set the player's skin when the player connects.
4
5new DB:db_handle; // Create a global variable to make a db connection and manipulate later on the code.
6
7enum USER_DATA {
8 USER_ID,
9 USER_NICKNAME[MAX_PLAYER_NAME],
10 USER_SKIN // We need to use this only for when the player connects.
11};
12
13new gUserData[MAX_PLAYERS][USER_DATA];
14
15public OnGameModeInit() {
16 db_handle = db_open("main.db");
17
18 if (db_handle) {
19 print("Successfully created a connection to database \"main.db\".");
20
21 db_free_result(db_query(db_handle, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname TEXT, skin INTEGER)"));
22 }
23
24 else {
25 print("Failed to open a connection to database \"main.db\".");
26 }
27 return 1;
28}
29
30// To store the skin that the player has before exit, all you need to do is to get the player skin and use the UPDATE clause from SQL.
31public OnPlayerDisconnect(playerid, reason) {
32 new query[256];
33
34 format(query, sizeof query, "UPDATE users SET skin = '%i' WHERE nickname ='%q'", GetPlayerSkin(playerid), gUserData[playerid][USER_NICKNAME]);
35
36 db_query(db_handle, query);
37
38 // Reset player variables
39 new
40 tmp[USER_DATA];
41
42 gUserData[playerid] = tmp;
43 return 1;
44}
45
46// Now you need to set the skin stored in the database to the player, and then make it spawn with that skin for example.
47public OnPlayerConnect(playerid)
48{
49 new query[256], DBResult: result; // Create a query variable and DBResult to free the result of the query.
50
51 // Suppose we have a function to check the player login succefully.
52 if (login()) {
53 SendClientMessage(playerid, -1, "Login succefully.");
54
55 // We make the query, selecting all fields from the users table.
56 format(query, sizeof query, "SELECT * FROM users WHERE nickname='%q' LIMIT 1", gUserData[playerid][USER_NICKNAME]);
57
58 result = db_query(db_handle, query);
59
60 // If there any row, it says that the user registered have the fields that you declare before (id, nickname and skin).
61 if (db_num_rows (result)) {
62 // So we will procced to associate the result of the field "skin" from the database to our variable gUserData[playerid][USER_SKIN].
63 gUserData[playerid][USER_SKIN] = db_get_field_assoc_int(result, "skin");
64 }
65 }
66
67 else {
68 ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "La contraseña que ingresaste es incorrecta.\nPor favor, intenta de nuevo!", "Ingresar", "Salir");
69 }
70
71 db_free_result(result); // Free the query result.
72 return 1;
73}
74
75public OnPlayerSpawn(playerid) {
76 SetSpawnInfo(playerid, 0, gUserData[playerid][USER_SKIN], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // Here is where we use the value already passed to our variable from the database.
77 SpawnPlayer(playerid);
78 return 1;
79}