· 9 years ago · Nov 23, 2016, 03:54 AM
1#include <a_samp>
2#include <a_mysql>
3
4main() {
5
6}
7
8
9
10// dialogs
11enum {
12 DIALOG_REGISTER,
13 DIALOG_LOGIN
14}
15
16
17
18
19// mysql
20new mysqlConnection;
21
22
23
24//
25enum {
26 PLAYER_STATUS_DISCONNECTED,
27 PLAYER_STATUS_CONNECTED,
28 PLAYER_STATUS_REGISTER,
29 PLAYER_STATUS_LOGIN,
30 PLAYER_STATUS_SPAWNED
31}
32
33enum E_PLAYER_INFO {
34 player_ID,
35 player_Status,
36 player_Name[21],
37 player_World,
38 player_Interior,
39 Float:player_Pos[4]
40}
41new gPlayerInfo[MAX_PLAYERS][E_PLAYER_INFO];
42
43
44
45// public functions
46public OnGameModeInit() {
47 if(mysql_errno((mysqlConnection = mysql_connect("localhost", "root", "samp", #)))) {
48 printf("[mysql] Não foi possÃvel conectar ao banco de dados.");
49 SendRconCommand("exit");
50 return 1;
51 }
52
53 mysql_tquery(mysqlConnection, "CREATE TABLE IF NOT EXISTS player (\
54 id int not null AUTO_INCREMENT,\
55 name varchar(20) not null,\
56 password varchar(128) not null,\
57 world int not null default 0,\
58 interior int not null default 0,\
59 x double not null default 823.0223,\
60 y double not null default -1340.7954,\
61 z double not null default 13.5174,\
62 a double not null default 87.1076,\
63 primary key(id));"
64 );
65
66 printf("[mysql] Conectado com sucesso.");
67 return 1;
68}
69
70public OnPlayerConnect(playerid) {
71 new temp[E_PLAYER_INFO];
72 SetPlayerColor(playerid, 0xCCCCCCFF);
73
74 gPlayerInfo[playerid] = temp;
75 gPlayerInfo[playerid][player_Status] = PLAYER_STATUS_CONNECTED;
76 GetPlayerName(playerid, gPlayerInfo[playerid][player_Name], 21);
77
78 new query[64];
79 mysql_format(mysqlConnection,
80 query,
81 sizeof query,
82 "SELECT id FROM player WHERE name='%s'",
83 gPlayerInfo[playerid][player_Name]
84 );
85
86 mysql_tquery(mysqlConnection, query, "r@Player_Connect", "i", playerid);
87 return 1;
88}
89
90public OnPlayerDisconnect(playerid, reason) {
91 if(gPlayerInfo[playerid][player_Status] < PLAYER_STATUS_SPAWNED)
92 return 1;
93
94 new Float:px,
95 Float:py,
96 Float:pz,
97 Float:angle;
98
99 GetPlayerPos(playerid, px, py, pz);
100 GetPlayerFacingAngle(playerid, angle);
101
102 new query[150];
103 mysql_format(mysqlConnection,
104 query,
105 sizeof query,
106 "UPDATE player SET world=%d, interior=%d, x=%f, y=%f, z=%f, a=%f WHERE id=%d",
107 GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid), px, py, pz, angle, gPlayerInfo[playerid][player_ID]
108 );
109
110 mysql_tquery(mysqlConnection, query);
111 return 1;
112}
113
114public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
115 switch(dialogid) {
116 case DIALOG_REGISTER: {
117 if(!response) {
118 Kick(playerid);
119 return 1;
120 }
121
122 if(!(5 <= strlen(inputtext) <= 32)) {
123 return ShowPlayerDialog(
124 playerid,
125 DIALOG_REGISTER,
126 DIALOG_STYLE_PASSWORD,
127 "Registro",
128 "Digite uma senha (5 a 32 caracteres) para se registrar.",
129 "Registrar",
130 "Sair"
131 );
132 }
133
134 new query[256];
135 mysql_format(mysqlConnection,
136 query,
137 sizeof query,
138 "INSERT INTO player (name, password) VALUES ('%s', PASSWORD('%e'))",
139 gPlayerInfo[playerid][player_Name], inputtext
140 );
141
142 mysql_tquery(mysqlConnection, query, "r@Player_Register", "i", playerid);
143 return 1;
144 }
145
146 case DIALOG_LOGIN: {
147 if(!response) {
148 Kick(playerid);
149 return 1;
150 }
151
152 new query[256];
153 mysql_format(mysqlConnection,
154 query,
155 sizeof query,
156 "SELECT * FROM player WHERE name='%s' AND password=PASSWORD('%e')",
157 gPlayerInfo[playerid][player_Name], inputtext
158 );
159
160 mysql_tquery(mysqlConnection, query, "r@Player_Login", "i", playerid);
161 return 1;
162 }
163 }
164 return 0;
165}
166
167
168
169// queries
170forward r@Player_Connect(playerid);
171forward r@Player_Register(playerid);
172forward r@Player_Login(playerid);
173
174public r@Player_Connect(playerid) {
175 new rows = cache_num_rows(mysqlConnection);
176 if(!rows) {
177 ShowPlayerDialog(
178 playerid,
179 DIALOG_REGISTER,
180 DIALOG_STYLE_PASSWORD,
181 "Registro",
182 "Digite uma senha (5 a 32 caracteres) para se registrar.",
183 "Registrar",
184 "Sair"
185 );
186
187 TogglePlayerSpectating(playerid, true);
188 gPlayerInfo[playerid][player_Status] = PLAYER_STATUS_REGISTER;
189 return 1;
190 }
191
192 gPlayerInfo[playerid][player_ID] = cache_get_field_content_int(0, "id", mysqlConnection);
193 ShowPlayerDialog(
194 playerid,
195 DIALOG_LOGIN,
196 DIALOG_STYLE_PASSWORD,
197 "Login",
198 "Digite sua senha para logar.",
199 "Logar",
200 "Sair"
201 );
202
203 TogglePlayerSpectating(playerid, true);
204 gPlayerInfo[playerid][player_Status] = PLAYER_STATUS_LOGIN;
205 return 1;
206}
207
208public r@Player_Register(playerid) {
209 gPlayerInfo[playerid][player_ID] = cache_insert_id(mysqlConnection);
210
211 new string[128];
212 format(string,
213 sizeof string, "Registrado com sucesso - ID: %d\nDigite sua senha para logar.", gPlayerInfo[playerid][player_ID]);
214
215 ShowPlayerDialog(
216 playerid,
217 DIALOG_LOGIN,
218 DIALOG_STYLE_PASSWORD,
219 "Login",
220 string,
221 "Logar",
222 "Sair"
223 );
224
225 gPlayerInfo[playerid][player_Status] = PLAYER_STATUS_LOGIN;
226 return 1;
227}
228
229public r@Player_Login(playerid) {
230 new rows = cache_num_rows(mysqlConnection);
231 if(!rows) {
232 return ShowPlayerDialog(
233 playerid,
234 DIALOG_LOGIN,
235 DIALOG_STYLE_PASSWORD,
236 "Login",
237 "Senha incorreta.",
238 "Logar",
239 "Sair"
240 );
241 }
242
243 SendClientMessage(playerid, -1, "Logado com sucesso.");
244 gPlayerInfo[playerid][player_World] = cache_get_field_content_int(0, "world", mysqlConnection);
245 gPlayerInfo[playerid][player_Interior] = cache_get_field_content_int(0, "interior", mysqlConnection);
246 gPlayerInfo[playerid][player_Pos][0] = cache_get_field_content_int(0, "x", mysqlConnection);
247 gPlayerInfo[playerid][player_Pos][1] = cache_get_field_content_int(0, "y", mysqlConnection);
248 gPlayerInfo[playerid][player_Pos][2] = cache_get_field_content_int(0, "z", mysqlConnection);
249 gPlayerInfo[playerid][player_Pos][3] = cache_get_field_content_int(0, "a", mysqlConnection);
250
251 SetPlayerVirtualWorld(playerid, gPlayerInfo[playerid][player_World]);
252 SetPlayerInterior(playerid, gPlayerInfo[playerid][player_Interior]);
253 SetSpawnInfo(playerid,
254 NO_TEAM,
255 23,
256 gPlayerInfo[playerid][player_Pos][0],
257 gPlayerInfo[playerid][player_Pos][1],
258 gPlayerInfo[playerid][player_Pos][2],
259 gPlayerInfo[playerid][player_Pos][3],
260 0, 0,
261 0, 0,
262 0, 0
263 );
264
265 gPlayerInfo[playerid][player_Status] = PLAYER_STATUS_SPAWNED;
266 TogglePlayerSpectating(playerid, false);
267 return 1;
268}