/
register.pwn
280 lines (245 loc) · 8.56 KB
/
register.pwn
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
_ __ __ _ _ __ __ ______ ______
| | / _|/ _| ___ | | | | | \/ | |____ |____ |
| | ___| |_| |_ _ __ _ _ ( _ ) | |_ _ ___| |_| \ / | ___ / / / /
_ | |/ _ \ _| _| '__| | | | / _ \/\ _ | | | | / __| __| |\/| |/ _ \ / / / /
| |__| | __/ | | | | | | |_| | | (_> < | |__| | |_| \__ \ |_| | | | __/_ / / / /
\____/ \___|_| |_| |_| \__, | \___/\/ \____/ \__,_|___/\__|_| |_|\___(_)_/ /_/
__/ |
|___/
@ Titel: Login / Register System [MySQL R41-3] + Elegant Textdraws
@ Version: 2.0.0
@ Login/Register System by Jeffry
@ Login/Register TextDraws by JustMe.77
@ Jeffry's Default Version (keine TextDraws): https://goo.gl/XgWgpb
@ JustMe.77's Default Version (nur TextDraw Design): https://goo.gl/dzRNcT
@ Current Version (Login/Register + TextDraws): https://github.com/JustMe77/Login-Register-System-MySQL-R41-3-Elegant-Textdraws/tree/master/Englisch )
*/
#include <a_samp>
#include <a_mysql>
#include <logintextdraws-en>
#define MYSQL_HOST "127.0.0.1" //IP Adresse from the MySQL Server
#define MYSQL_USER "root" //Username to sign in
#define MYSQL_PASS "" //Password from the user
#define MYSQL_DBSE "samp_db" //Name from the database
new MySQL:handle; //The connection handle which we need later to acces the tables
//Dialog IDs (you may change them if they're already taken)
#define DIALOG_REGISTER 1403
#define DIALOG_LOGIN 2401
enum pDataEnum
{
p_id,
bool:pLoggedIn,
pName[MAX_PLAYER_NAME],
pLevel,
pMoney,
pKills,
pDeaths
}
new PlayerInfo[MAX_PLAYERS][pDataEnum];
public OnFilterScriptInit()
{
MySQL_SetupConnection();
return 1;
}
public OnFilterScriptExit()
{
mysql_close(handle);
return 1;
}
public OnPlayerConnect(playerid)
{
CreatePlayerLoginTextDraws(playerid);
PlayerInfo[playerid][p_id] = 0;
PlayerInfo[playerid][pLoggedIn] = false;
PlayerInfo[playerid][pLevel] = 0;
PlayerInfo[playerid][pMoney] = 0;
PlayerInfo[playerid][pKills] = 0;
PlayerInfo[playerid][pDeaths] = 0;
GetPlayerName(playerid, PlayerInfo[playerid][pName], MAX_PLAYER_NAME);
return 1;
}
public OnPlayerRequestClass(playerid)
{
//If the player enters the class selection, we check if he's already logged in
if(!PlayerInfo[playerid][pLoggedIn])
{
//If not, we check if he got already an account
//We send a query and call a new callback
new query[128];
mysql_format(handle, query, sizeof(query), "SELECT id FROM users WHERE name = '%e'", PlayerInfo[playerid][pName]);
mysql_pquery(handle, query, "OnUserCheck", "d", playerid);
}
return 1;
}
public OnPlayerRequestSpawn(playerid)
{
if(!PlayerInfo[playerid][pLoggedIn])
{
SendClientMessage(playerid, -1, "{FF0000}You've to login before you're able to spawn yourself!");
return 0;
}
return 1;
}
public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
{
if(playertextid == PlayerLoginTextDraw[playerid][0])
{
if(pRegistered[playerid] == true)
{
ShowPlayerDialog(playerid, DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login", "Please login to your account:", "Ok", "Cancel");
}
else
{
ShowPlayerDialog(playerid, DIALOG_REGISTER,DIALOG_STYLE_PASSWORD,"Registration", "Please register yourself:", "Ok", "Cancel");
}
}
return 0;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(dialogid == DIALOG_REGISTER)
{
//The player has selected cancel
if(!response) return Kick(playerid);
//If the player has typed nothing or a short password
if(strlen(inputtext) < 3) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration", "Please register yourself:\n{FF0000}At least 3 character!", "Ok", "Cancel");
//If everything is alright, we insert the user into the databse
//We encode the password with the MD5 hash
new query[256];
mysql_format(handle, query, sizeof(query), "INSERT INTO users (name, password) VALUES ('%e', MD5('%e'))", PlayerInfo[playerid][pName], inputtext);
//The query is sent and we call OnUserRegister
mysql_pquery(handle, query, "OnUserRegister", "d", playerid);
return 1;
}
if(dialogid == DIALOG_LOGIN)
{
//The player has selected cancel
if(!response) return Kick(playerid);
//If the player has typed nothing or a short password
if(strlen(inputtext) < 3) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Please login to your account:\n{FF0000}At least 3 charachter!", "Ok", "Cancel");
//If everything is alright, we read from the database
new query[256];
mysql_format(handle, query, sizeof(query), "SELECT * FROM users WHERE name = '%e' AND password = MD5('%e')", PlayerInfo[playerid][pName], inputtext);
//The query is sent and we call OnUserLogin
mysql_pquery(handle, query, "OnUserLogin", "d", playerid);
return 1;
}
return 0;
}
public OnPlayerDisconnect(playerid, reason)
{
//We save the player when he leave the server
SaveUserStats(playerid);
DestroyPlayerLoginTextDraws(playerid);
return 1;
}
forward OnUserCheck(playerid);
public OnUserCheck(playerid)
{
new rows;
cache_get_row_count(rows);
if(rows == 0)
{
//The player was not found, he has to register
PlayerIsNotRegistered(playerid);
ShowLoginTextDraws(playerid);
}
else
{
//The account exist, he has to login
PlayerIsRegistered(playerid);
ShowLoginTextDraws(playerid);
}
return 1;
}
forward OnUserRegister(playerid);
public OnUserRegister(playerid)
{
PlayerInfo[playerid][p_id] = cache_insert_id();
PlayerInfo[playerid][pLoggedIn] = true;
SendClientMessage(playerid, 0x00FF00FF, "[Account] Registration succesful.");
PlayerPlaySound(playerid, 1057 , 0.0, 0.0, 0.0);
HideLoginTextDraws(playerid);
return 1;
}
forward OnUserLogin(playerid);
public OnUserLogin(playerid)
{
new rows;
cache_get_row_count(rows);
if(rows == 0)
{
//The player has typed in a wrong password
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Please login to your account:\n{FF0000}Wrong password!", "Ok", "Cancel");
}
else
{
//The player has typed in his correct password
cache_get_value_name_int(0, "id", PlayerInfo[playerid][p_id]);
cache_get_value_name_int(0, "level", PlayerInfo[playerid][pLevel]);
cache_get_value_name_int(0, "money", PlayerInfo[playerid][pMoney]);
cache_get_value_name_int(0, "kills", PlayerInfo[playerid][pKills]);
cache_get_value_name_int(0, "deaths", PlayerInfo[playerid][pDeaths]);
PlayerInfo[playerid][pLoggedIn] = true;
SendClientMessage(playerid, 0x00FF00FF, "[Account] Signed in.");
PlayerPlaySound(playerid, 1057 , 0.0, 0.0, 0.0);
GivePlayerMoney(playerid, PlayerInfo[playerid][pMoney]);
HideLoginTextDraws(playerid);
}
return 1;
}
public OnPlayerDeath(playerid, killerid, reason)
{
//Example Code
if(killerid != INVALID_PLAYER_ID)
{
PlayerInfo[killerid][pKills]++;
GivePlayerMoney(killerid, 10);
PlayerInfo[killerid][pMoney] += 10;
if(PlayerInfo[killerid][pKills] > 3)
{
PlayerInfo[killerid][pLevel] = 1;
}
}
PlayerInfo[playerid][pDeaths]++;
return 1;
}
stock SaveUserStats(playerid)
{
//If the player is not logged in, we don't save his account
if(!PlayerInfo[playerid][pLoggedIn]) return 1;
//If he has an account, we save it
new query[256];
mysql_format(handle, query, sizeof(query), "UPDATE users SET level = '%d', money = '%d', kills = '%d', deaths = '%d' WHERE id = '%d'",
PlayerInfo[playerid][pLevel], PlayerInfo[playerid][pMoney], PlayerInfo[playerid][pKills], PlayerInfo[playerid][pDeaths], PlayerInfo[playerid][p_id]);
//Query sent
mysql_pquery(handle, query);
return 1;
}
stock MySQL_SetupConnection(ttl = 3)
{
print("[MySQL] Connecting to the database...");
//mysql_log(); //<- Remove the // to activate the Debug Mode
handle = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DBSE);
if(mysql_errno(handle) != 0)
{
if(ttl > 1)
{
//We try again to connect to the database
print("[MySQL] The connection to the databse was not succesful.");
printf("[MySQL] Try again (TTL: %d).", ttl-1);
return MySQL_SetupConnection(ttl-1);
}
else
{
//Cancel and close the server
print("[MySQL] The connection to the database was not succesful.");
print("[MySQL] Please check the MySQL Login Information.");
print("[MySQL] Shutting server down");
return SendRconCommand("exit");
}
}
printf("[MySQL] The connection to the database was succesful! Handle: %d", _:handle);
return 1;
}